知识点
相关文章
更多最近更新
更多Solrj的使用
2019-03-27 01:12|来源: 网路
http://wiki.apache.org/solr/Solrj
Solrj is a java client to access solr. It offers a java interface to add, update, and query the solr index.
CommonsHttpSolrServer
The CommonsHttpSolrServer uses the Apache Commons HTTP Client to connect to solr.
String url = "http://localhost:8983/solr"; /* CommonsHttpSolrServer is thread-safe and if you are using the following constructor, you *MUST* re-use the same instance for all requests. If instances are created on the fly, it can cause a connection leak. The recommended practice is to keep a static instance of CommonsHttpSolrServer per solr server url and share it for all requests. See https://issues.apache.org/jira/browse/SOLR-861 for more details */ SolrServer server = new CommonsHttpSolrServer( url );
Setting XMLResponseParser
SolrJ uses a binary format as the default format now. For users with Solr 1.2 or older versions of Solr 1.3 must explicitly ask SolrJ to use XML format.
server.setParser(new XMLResponseParser());
Changing other Connection Settings
CommonsHttpSolrServer allows setting connection properties.
String url = "http://localhost:8983/solr" CommonsHttpSolrServer server = new CommonsHttpSolrServer( url ); server.setSoTimeout(1000); // socket read timeout server.setConnectionTimeout(100); server.setDefaultMaxConnectionsPerHost(100); server.setMaxTotalConnections(100); server.setFollowRedirects(false); // defaults to false // allowCompression defaults to false. // Server side must support gzip or deflate for this to have any effect. server.setAllowCompression(true); server.setMaxRetries(1); // defaults to 0. > 1 not recommended.
EmbeddedSolrServer
The EmbeddedSolrServer provides the same interface without requiring an HTTP connection.
// Note that the following property could be set through JVM level arguments too System.setProperty("solr.solr.home", "/home/shalinsmangar/work/oss/branch-1.3/example/solr"); CoreContainer.Initializer initializer = new CoreContainer.Initializer(); CoreContainer coreContainer = initializer.initialize(); EmbeddedSolrServer server = new EmbeddedSolrServer(coreContainer, "");
If you want to use MultiCore features, then you should use this:
File home = new File( "/path/to/solr/home" ); File f = new File( home, "solr.xml" ); CoreContainer container = new CoreContainer(); container.load( "/path/to/solr/home", f ); EmbeddedSolrServer server = new EmbeddedSolrServer( container, "core name as defined in solr.xml" ); ...
If you need to use solr in an embedded application, this is the recommended approach. It allows you to work with the same interface whether or not you have access to HTTP.
Note -- EmbeddedSolrServer works only with handlers registered in solrconfig.xml. A RequestHandler must be mapped to /update for a request to /update to function.
Usage
Solrj is designed as an extendable framework to pass SolrRequest to the SolrServer and return a SolrResponse.
For simplicity, the most common commands are modeled in the SolrServer:
Adding Data to Solr
-
Get an instance of server first
SolrServer server = getSolrServer();
The getSolrServer() method body can be as follows if you use a remote server,
public SolrServer getSolrServer(){ //the instance can be reused return new CommonsHttpSolrServer(); }
if it is a local server use the following,
public SolrServer getSolrServer(){ //the instance can be reused return new EmbeddedSolrServer(); }
-
If you wish to clean up the index before adding data do this
server.deleteByQuery( "*:*" );// delete everything!
-
Construct a document
SolrInputDocument doc1 = new SolrInputDocument(); doc1.addField( "id", "id1", 1.0f ); doc1.addField( "name", "doc1", 1.0f ); doc1.addField( "price", 10 );
-
Construct another document. Each document can be independently be added but it is more efficient to do a batch update. Every call to SolrServer is an Http Call (This is not true for EmbeddedSolrServer).
SolrInputDocument doc2 = new SolrInputDocument(); doc2.addField( "id", "id2", 1.0f ); doc2.addField( "name", "doc2", 1.0f ); doc2.addField( "price", 20 );
-
Create a collection of documents
Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>(); docs.add( doc1 ); docs.add( doc2 );
-
Add the documents to Solr
server.add( docs );
-
Do a commit
server.commit();
-
To immediately commit after adding documents, you could use:
UpdateRequest req = new UpdateRequest(); req.setAction( UpdateRequest.ACTION.COMMIT, false, false ); req.add( docs ); UpdateResponse rsp = req.process( server );
Streaming documents for an update
This is the most optimal way of updating all your docs in one http request.
CommonsHttpSolrServer server = new CommonsHttpSolrServer(); Iterator<SolrInputDocument> iter = new Iterator<SolrInputDocument>(){ public boolean hasNext() { boolean result ; // set the result to true false to say if you have more documensts return result; } public SolrInputDocument next() { SolrInputDocument result = null; // construct a new document here and set it to result return result; } }; server.add(iter);
you may also use the addBeans(Iterator<?> beansIter) method to write pojos
Directly adding POJOs to Solr
-
Create a Java bean with annotations. The @Field annotation can be applied to a field or a setter method. If the field name is different from the bean field name give the aliased name in the annotation itself as shown in the categories field.
import org.apache.solr.client.solrj.beans.Field; public class Item { @Field String id; @Field("cat") String[] categories; @Field List<String> features; }
The @Field annotation can be applied on setter methods as well example:
@Field("cat") public void setCategory(String[] c){ this.categories = c; }
There should be a corresponding getter method (without annotation) for reading attributes
-
Get an instance of server
SolrServer server = getSolrServer();
-
Create the bean instances
Item item = new Item(); item.id = "one"; item.categories = new String[] { "aaa", "bbb", "ccc" };
-
Add to Solr
server.addBean(item);
-
Adding multiple beans together
List<Item> beans ; //add Item objects to the list server.addBeans(beans);
Note -- Reuse the instance of SolrServer if you are using this feature (for performance )
Setting the RequestWriter
Solr1.4 SolrJ lets you upload content in XML and Binary format. Default is set to be XML. Use the following to upload using Binary format. this is the same format which SolrJ uses to fetch results.
server.setRequestWriter(new BinaryRequestWriter());
Reading Data from Solr
-
Get an instance of server first
SolrServer server = getSolrServer();
-
Construct a SolrQuery
SolrQuery query = new SolrQuery(); query.setQuery( "*:*" ); query.addSortField( "price", SolrQuery.ORDER.asc );
-
Query the server
QueryResponse rsp = server.query( query );
-
Get the results
SolrDocumentList docs = rsp.getResults();
-
To read Documents as beans, the bean must be annotated as given in the example.
List<Item> beans = rsp.getBeans(Item.class);
Advanced usage
SolrJ provides a APIs to create queries instead of hand coding the query . Following is an example of a faceted query.
SolrServer server = getSolrServer(); SolrQuery solrQuery = new SolrQuery(). setQuery("ipod"). setFacet(true). setFacetMinCount(1). setFacetLimit(8). addFacetField("category"). addFacetField("inStock"); QueryResponse rsp = server.query(solrQuery);
All the setter/add methods return its instance . Hence these calls can be chained
Highlighting
Highlighting parameters are set like other common parameters.
SolrQuery query = new SolrQuery(); query.setQuery("foo"); query.setHighlight(true).setHighlightSnippets(1); //set other params as needed query.setParam("hl.fl", "content"); QueryResponse queryResponse = getSolrServer().query(query);
Then to get back the highlight results you need something like this:
Iterator<SolrDocument> iter = queryResponse.getResults(); while (iter.hasNext()) { SolrDocument resultDoc = iter.next(); String content = (String) resultDoc.getFieldValue("content")); String id = (String) resultDoc.getFieldValue("id"); //id is the uniqueKey field if (queryResponse.getHighlighting().get(id) != null) { List<String> highightSnippets = queryResponse.getHighlighting().get(id).get("content"); } }
Setting the classpath
Ant
SolrJ is a part of the 1.3.0 release. The jars required in the classpath for SolrJ are,
-
commons-io-1.3.1.jar
-
commons-httpclient-3.1.jar
-
commons-codec-1.3.jar
-
commons-logging-1.0.4.jar
-
apache-solr-common-1.3.0.jar
-
apache-solr-solrj-1.3.0.jar
If you are using an Solr 1.2, add the stax jars too. Do not forget to set the XML parser
-
geronimo-stax-api_1.0_spec-1.0.1.jar
-
wstx-asl-3.2.7.jar
-
stax-utils.jar
The above jars can be found in the dist/solrj-lib directory of the Solr 1.3.0 download.
Maven
Solrj is available in the official Maven repository. Add the following dependency to your pom.xml to use SolrJ
<dependency> <artifactId>solr-solrj</artifactId> <groupId>org.apache.solr</groupId> <version>1.3.0</version> <type>jar</type> <scope>compile</scope> </dependency>
If you need to use the EmbeddedSolrServer, you need to add the solr-core dependency too.
<dependency> <artifactId>solr-core</artifactId> <groupId>org.apache.solr</groupId> <version>1.3.0</version> <type>jar</type> <scope>compile</scope> </dependency>
last edited 2009-09-12 17:09:14 by JayHill
转自:http://www.cnblogs.com/cy163/archive/2009/09/18/1569723
相关问答
更多-
索引db通过SolrJ(index db via SolrJ)[2022-08-12]
如果您只需要能够打开与Solr服务器的连接以进行索引(并且不需要将配置文件与SolrJ项目实际集成),那么这很简单。 首先,您需要打开SolrJ连接,这样做是这样的: HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8983/solr"); 另一个选择是利用Spring Data Solr的solr模式,并使Solr服务器bean执行以下操作:solrj RealTime得到(solrj RealTime get)[2023-07-14]
由于RealTime Get是使用备用requestHandler实现的,因此您只需要在SolrQuery上使用setRequestHandler()方法,将"/get"作为处理程序名称。 有关完整示例,请参阅Solr源中此SolrExampleTests.java文件中的testRealTimeGet()方法。 这是该文件的片段: SolrQuery q = new SolrQuery(); q.setRequestHandler("/get"); q.set("id", "DOCID"); ...在Eclipse上运行SolrJ(Running SolrJ on Eclipse)[2023-09-20]
好的,这意味着另类: A)你正在从错误的类中“运行 - - >> Java应用程序”。 打开包含main的类,右键单击它并选择“Run as Java Application” B)你的main方法有一个错误的签名(因此不是一个有效的主方法) - Ok so that means in alternative: A) your're doing "run as -->> Java application" from the wrong class. Open then class that contains ...您可以在SolrQuery类上使用addFilterQuery()方法 。 只需按原样传递过滤器查询(“ x:[10 TO 100] AND y:[20 TO 300] ”) You can use the addFilterQuery() method on the SolrQuery class. Just pass the filter query as is ("x:[10 TO 100] AND y:[20 TO 300]")SolrJ POJO注释(SolrJ POJO Annotations)[2022-08-14]
根据SOLR-1945,从Solr 5.1开始,通过在@Field注释中使用child属性,可以在Java文档中看到这一点。 在你的情况下,它会是: class Content { @Field("uniqueId") String id; @Field("timeStamp") Long timeStamp; @Field(child = true) // You should use this annotation Author author ...import org.apache.solr.client.solrj.impl.CloudSolrServer; import org.apache.solr.common.SolrInputDocument; CloudSolrServer server = new CloudSolrServer("localhost:9983"); server.setDefaultCollection("collection1"); SolrInputDocument doc = new SolrInputDoc ...Solrj查询响应(Solrj query Response)[2022-04-10]
谢谢Perikli,但它已经整理好了。 代码如下: SolrServer solrServer = new HttpSolrServer("http://localhost:8983/solr"); QueryResponse qResponse = solrServer.query(queryForFecthing); 实际上在创建QueryResponse对象时得到异常,即qResponse ,我认为这是因为我作为参数传递的查询(即上面是' queryForFecthing ')。 但这是错误的假设 ...您可以通过以下方式指定所有参 query.setParam("spellcheck", "true"); query.setParam("spellcheck.q", query); You can specify all parameters via: query.setParam("spellcheck", "true"); query.setParam("spellcheck.q", query);使用SolrJ索引文档(Indexing document with SolrJ)[2023-01-07]
您需要在文档索引过程完成后提交。 因此,如果您重新启动solr,它将执行自动提交。 如果你不想重启Solr。 你可以试试 重新加载核心(请参阅下面的URL,根据您更改URL名称) http://127.0.0.1:8080/solr/admin/cores?action=RELOAD&core=core1 要么 使用solr.commit(); 在你的代码中 You need to commit after document indexing process is done. so if you resta ...Solr / Solrj分页(Solr/Solrj pagination)[2022-06-13]
在服务器上执行查询之前,客户端不会知道你在服务器端设置了什么,对吧? 所以他们全都是空的并不奇怪。 要实现分页,您需要客户端的两个参数 - 页码和每页的项目数量。 一旦你得到了这两个,你可以在客户端构建你的SolrQuery,如下所示: SolrQuery query = new SolrQuery(searchTerm); query.setStart((pageNum - 1) * numItemsPerPage); query.setRows(numItemsPerPage); // execute ...