"Solr exploration" SOLR installation, startup, query, index

Source: Internet
Author: User
Tags solr solr query

1. Installation & Startup

Official website: http://lucene.apache.org/solr/

Download source code, unzip, go to root directory (I put SOLR under/USR/LOCAL/SOLR)

In/usr/local/solr/example, there are some examples

Launch example

BIN/SOLR start–e cloud–noprompt-e <example>  Name of the example to run; Available examples:      cloud:         Sol Rcloud example      default:       SOLR Default Example      dih:           Data Import Handler schemaless      :    schema-less example      multicore:     multicore-noprompt    do not prompt for input, accept all default input

  

Another way to start an example

CD Examplejava–jar Start.jar All the logs will be printed on the console

Open the SOLR admin interface by entering the following address in the browser

Http://localhost:8983/solr

2. Quickly index documents using tools

The following commands are completed on the command line and need to be specified-classpath, that is, the path of the specified Solr-core-version.jar

(My/usr/local/solr/example directory is executed

Java-classpath dist/solr-c-dauto-drecursive org.apache.solr.util.SimplePostTool docs/

Index all files under the Docs directory (current directory/usr/local/solr/example)

Java-dauto-drecursive Org.apache.solr.util.SimplePostTool docs/

Index XML, JSON

When you index the JSON, you need to specify the auto parameter because the Simpleposttool default index is the XML document

Because the Simpleposttool defaults to assuming files be in SOLR XML format, The-dauto switch was used to post JSON files So, it uses the appropriate content type.

Java Org.apache.solr.util.SimplePostTool example/exampledocs/*.xml
Java-dauto Org.apache.solr.util.SimplePostTool Example/exampledocs/books.csv
Java-dauto Org.apache.solr.util.SimplePostTool Example/exampledocs/books.json

Using the Post.jar index file

Java-jar Example/exampledocs/post.jar Example/exampledocs/hd.xml

3. Stop SOLR and empty the node data
BIN/SOLR Stop-all; RM-RF node1/node2/

4. Install the dependency package using the SOLRJ operation solr4.1

SOLRJ is a suite of Java API packages that operate SOLR, as follows:

Reference directory:

Http://wiki.apache.org/solr/Solrj

Http://wiki.apache.org/solr/SolrQuerySyntax

Before using SOLRJ, the official website indicates that the following packages are required:

Apache-solr-solrj-*.jar Commons-codec-1.3.jar Commons-httpclient-3.1.jar Commons-io-1.4.jar Jcl-over-slf4j-1.5.5.jar Slf4j-api-1.5.5.jar Slf4j-jdk14-1.5.5.jar

In practice, in IntelliJ idea, writing the code that operates SOLR, requires more than these packages, my jar package is as follows:

Solr-solrj-4.10.2.jar commons-codec-1.3.jar commons-io-1.4.jar jcl-over-slf4j.1.7.7.jar Slf4j-api-1.5.6.jar Slf4j-jdk14-1.5.6.jar Httpclient-4.3.1.jar Httpcore-4.3.jar Httpmime-4.3.1.jar Noggit-0.5.jar

There are two pits in this:

One: When using Maven's pom.xml to manage packages, the downloaded packages are often unavailable (corrupted), but Maven does not detect them. This is what I came across, and finally, in the SOLR installation directory, search to find the corresponding jar package, manually add the project.

Second: The official website to the dependency package is not too full, may not be for the independent project, this I also do not quite understand, I own test is that there are several packages on it enough.

Errors that may be used when trying to use SOLRJ:

Exception in thread "main" Java.lang.noclassdeffounderror:org/apache/http/client/methods/httpurirequestat SolrX.IndexDataTools.main (indexdatatools.java:115) caused by:java.lang.ClassNotFoundException: Org.apache.http.client.methods.HttpUriRequestat Java.net.urlclassloader$1.run (urlclassloader.java:366) at Java.net.urlclassloader$1.run (urlclassloader.java:355) at java.security.AccessController.doPrivileged (Native Method) at Java.net.URLClassLoader.findClass (urlclassloader.java:354) at Java.lang.ClassLoader.loadClass ( classloader.java:425) at Sun.misc.launcher$appclassloader.loadclass (launcher.java:308) at Java.lang.ClassLoader.loadClass (classloader.java:358) ... 1 more

The above solution: add Httpclient-4.1.4.jar

Exception in thread "main" java.lang.noclassdeffounderror:org/apache/http/entity/mime/content/contentbodycaused by: Java.lang.ClassNotFoundException:org.apache.http.entity.mime.content.ContentBody

The above solution: Add Httpmime-4.2.3.jar

Exception in thread "main" java.lang.IllegalAccessError:tried to access field Org.slf4j.impl.StaticLoggerBinder.SINGLETON from class Org.slf4j.LoggerFactoryat Org.slf4j.LoggerFactory.staticInitialize (loggerfactory.java:83) at org.slf4j.loggerfactory.<clinit> ( loggerfactory.java:73) at org.apache.solr.client.solrj.impl.httpsolrserver.<clinit> (HttpSolrServer.java:91 ) at SolrX.IndexDataTools.main (indexdatatools.java:115)

The solution above:Slf4j-api-1.5.6.jar

Exception in thread "main" Java.lang.noclassdeffounderror:org/noggit/chararrat Org.apache.solr.common.util.javabincodec.<init> (javabincodec.java:590) at Org.apache.solr.client.solrj.impl.BinaryResponseParser.processResponse (binaryresponseparser.java:43) at Org.apache.solr.client.solrj.impl.HttpSolrServer.executeMethod (httpsolrserver.java:528) at Org.apache.solr.client.solrj.impl.HttpSolrServer.request (httpsolrserver.java:210) at Org.apache.solr.client.solrj.impl.HttpSolrServer.request (httpsolrserver.java:206) at Org.apache.solr.client.solrj.request.AbstractUpdateRequest.process (abstractupdaterequest.java:124) at Org.apache.solr.client.solrj.SolrServer.add (solrserver.java:68) at Org.apache.solr.client.solrj.SolrServer.add ( solrserver.java:54) at Solr.data.tools.IndexDataTools.main (indexdatatools.java:41) at SUN.REFLECT.NATIVEMETHODACCESSORIMPL.INVOKE0 (Native Method) at Sun.reflect.NativeMethodAccessorImpl.invoke ( nativemethodaccessorimpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.inVoke (delegatingmethodaccessorimpl.java:43) at Java.lang.reflect.Method.invoke (method.java:606) at Com.intellij.rt.execution.application.AppMain.main (appmain.java:134) caused by:java.lang.ClassNotFoundException: Org.noggit.CharArrat Java.net.urlclassloader$1.run (urlclassloader.java:366) at Java.net.urlclassloader$1.run ( urlclassloader.java:355) at java.security.AccessController.doPrivileged (Native Method) at Java.net.URLClassLoader.findClass (urlclassloader.java:354) at Java.lang.ClassLoader.loadClass (Classloader.java : 425) at Sun.misc.launcher$appclassloader.loadclass (launcher.java:308) at Java.lang.ClassLoader.loadClass ( classloader.java:358) ... More

The above solution: add Noggit-0.5.jar

Please refer to the above exception at the same time: http://blog.sina.com.cn/s/blog_5ddc071f0101mtpf.html

4.2 Solrj Adding an index
Solrinputdocument: Constructing SOLR Document objects
In the case of large data indexing, the most basic approach is to construct the document object in turn and then add it to SOLR.

public static void Main (string[] args) throwsIOException, solrserverexception {String solrurl = "HTTP://LOCALHOST:8983/SOLR"; Httpsolrserver Server = newHttpsolrserver (Solrurl); Server.setmaxretries (1); Server.setconnectiontimeout (5000); Server.setsotimeout (10000); Server.setdefaultmaxconnectionsperhost (100); Server.setmaxtotalconnections (100); Server.setfollowredirects (false ); Server.setallowcompression (true );//String URL = server.getbaseurl (); Solrinputdocument Doc1 = new  solrinputdocument () Doc1.addfield ("id", "id1", 1.0f ), Doc1.addfield ("name", " Doc1 ", 1.0f ); Doc1.addfield ("Price", ten ); Solrinputdocument doc2 = new  solrinputdocument () Doc2.addfield ("id", "id2", 1.0f ), Doc2.addfield ("name", " Doc3 ", 1.0f ); Doc2.addfield ("Price", ); collection<solrinputdocument> docs = new Arraylist<solrinputdocument>  (); Docs.add (Doc1); Docs.add ( DOC2); Server.add (Docs); Set Auto Commit docs//updaterequest req = new Updaterequest ();//Req.setaction (updaterequest. ACTION. COMMIT, False, false);//Req.add (docs);//Updateresponse RSP = req.process (server);  server.commit ();}           
4.3 Queries Solr

Solrquery: Building SOLR Query object, setting properties of the query

QUERYRESPONSE:SOLR Query Result Object

SOLRDOCUMENTLIST:SOLR The result object that the query result object contains

Private String Solrurl = "HTTP://LOCALHOST:8983/SOLR"; public void Querysolr (String querystr) throwssolrserverexception {httpsolrserver Server = newHttpsolrserver (Solrurl); Solrquery query = newSolrquery (); Query.setrequesthandler ("/select"); Query.setquery ("name:doc*"); Query.setsort ("id", SOLRQUERY.ORDER.ASC); Queryresponse RSP =Server.query (query); Iterator<solrdocument> iter =Rsp.getresults (). iterator (); Solrdocumentlist docList = rsp.getresults (); list<booksitem> beans = Rsp.getbeans (Booksitem.class ); while  (Iter.hasnext ()) {Solrdocument doc =  Iter.next (); String id = (string) doc.getfieldvalue ("id" ); String name = (string) doc.getfieldvalue ("name" ); System.out.print ("---ID:" +id+ "--name:" +  name); collection<string> names =  doc.getfieldnames (); for  (String fname:names) {java.lang.Object Fvalue = Doc.getfieldvalue (fName); Boolean isstring = Fvalue instanceof  String; Boolean islist = Fvalue instanceof arraylist<?> ; if  ( isstring) {fvalue =  (String) Fvalue; System.out.print ("FieldName:" +fname+ "Filedvalue:" +fvalue+ "\ n" );} if  (Islist) {for (String s: (arraylist& Lt String> ) fvalue) {System.out.print ("FieldName:" +fname+ "Filedvalue:" +string.valueof (s) + "\ n" );}}} } }  

Blog Sync Address: http://www.cnblogs.com/SEEKTHINKING/p/4228935.html

  

  

"Solr exploration" SOLR installation, startup, query, index

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.