SOLR configuration and simple demo

Source: Internet
Author: User
Tags apache solr

Introduction:

SOLR is an enterprise-level full-text search engine based on Lucene Java Search Library. It is currently a project of Apache. Its official website is in http://lucene.apache.org/solr. SOLR needs to run in a servlet container, such as Tomcat. SOLR provides HTTP/XML-based Web Services at the upper layer of Lucene. Our applications need to interact with SOLR through this service.

 

 

Premise: Download tomcat. Omitted.

Step 1: Download SOLR, http://www.apache.org/dyn/closer.cgi/lucene/solr/

I downloaded version 3.5 and decompressed It To The edisk. E:/apache-solr-3.5.0

 

Step 2: Modify conf \ Server. xml and modify the line of port 8080 as follows:

 

<Connector port="8080" protocol="HTTP/1.1"  
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8"/>

The following is also useful.

<Connector port="8080" URLEncoder="UTF-8" redirectPort="8443" connectionTimeout="20000" protocol="HTTP/1.1"/>

In fact, it is added the uriencoding = "UTF-8" configuration. The first method is recommended. The two differences are unknown for the moment.

 

Step 3: Configure SOLR

Add the Catalina \ localhost \ SOLR. xml file in the conf directory. If the conf Folder does not contain Catalina, create it.

SOLR. XML content:

<Context docBase="E:/apache-solr-3.5.0/dist/apache-solr-3.5.0.war" debug="0" crossContext="true" >
<Environment name="solr/home" type="java.lang.String" value="E:/apache-solr-3.5.0/example/solr" override="true" /></Context>


Step 4: start Tomcat. Enter http: // localhost: 8080/SOLR/to display the welcome page, indicating that the interface is successful.

 

Step 5: simple Java API operations

Refer to the http://www.iteye.com/topic/315330 to write in detail.

The complete code for reading and writing data is as follows:

Package com. weishangye. Test. SOLR;

Import java. util. arraylist;
Import java. util. collection;
Import java. util. iterator;
Import org. Apache. SOLR. Client. solrj. solrquery;
Import org. Apache. SOLR. Client. solrj. solrserver;
Import org. Apache. SOLR. Client. solrj. impl. commonshttpsolrserver;
Import org. Apache. SOLR. Client. solrj. response. queryresponse;
Import org. Apache. SOLR. Common. solrdocument;
Import org. Apache. SOLR. Common. solrdocumentlist;
Import org. Apache. SOLR. Common. solrinputdocument;

Public class solrtest1 {

Public static void main (string [] ARGs) throws exception {
String url = "http: // localhost: 8080/SOLR ";
Solrserver Server = new commonshttpsolrserver (URL );
Server. deletebyquery ("*: *"); // delete everything!
Solrinputdocument doc1 = new solrinputdocument ();
Doc1.addfield ("ID", "id1", 1.0f );
Doc1.addfield ("name", "doc1", 1.0f );
Doc1.addfield ("price", 10 );

Solrinputdocument doc2 = new solrinputdocument ();
Doc2.addfield ("ID", "ID2", 1.0f );
Doc2.addfield ("name", "", 1.0f );
Doc2.addfield ("price", 20 );

Collection <solrinputdocument> docs = new arraylist <solrinputdocument> ();
Docs. Add (doc1 );
Docs. Add (doc2 );
Server. Add (DOCS );
Server. Commit ();

Solrquery query = new solrquery ();
Query. setquery ("*:*");
Query. addsortfield ("price", solrquery. Order. DESC );
Queryresponse RSp = server. Query (query );
Solrdocumentlist docslist = RSP. getresults ();
For (iterator <solrdocument> Doc = docslist. iterator (); Doc. hasnext ();){
Solrdocument d = Doc. Next ();
System. Out. Print (D. getfieldvalue ("ID") + "-> ");
System. Out. println (D. getfieldvalue ("name "));
}
}

}

 

Resource:

Http://www.ibm.com/search/csass/search? Sn = DW & lang = ZH & CC = cn & en = UTF & HPP = 20 & DWS = cndw & Lo = ZH & Q = SOLR & search = % E6 % 90% 9C % e7 % B4 % A2 IBM community

Http://www.oschina.net/question/12_9398 uses open source Apache SOLR search engine to build restful basic storage service

Http://blog.chenlb.com/tag/solr

SOLR configuration and simple demo

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.