SOLR Combat 1

Source: Internet
Author: User

Implement user data indexing and querying
1. Start SOLRSOLR Start
2. Create CollectionSOLR create-c User
3. Add field in SchemaAdd in 3.1 solr-5.2.1/server/solr/user/conf/managed-schema
                <!--define IK parts-to-               <fieldtype name= "Text_ik" class= "SOLR. TextField ">                   <!--the word breaker at index time--                   <analyzer type=" index "ismaxwordlength=" false "class=" Org.wltea.analyzer.lucene.IKAnalyzer "/>                   <!--word breaker--                   <analyzer type=" Query " Ismaxwordlength= "true" class= "Org.wltea.analyzer.lucene.IKAnalyzer"/>               </fieldType>               <field Name= "id" type= "string" indexed= "true" stored= "true" required= "true" multivalued= "false"/>                <field name= " Username "type=" Text_ik "indexed=" true "stored=" true "multivalued=" true "/>               <field name=" age "type=" Text_ik "Indexed=" true "stored=" true "/>               <field name=" keywords "type=" Text_ik "indexed=" true "stored=" true "/>


3.2 Adding the IK word breakers libraryfiled is used in the definition of Ikanalyzer, need to enter the relevant configuration reference word breakerA. Adding Ikanalyzer3.2.8.jar:http://download.csdn.net/detail/buyaor in Solr-5.2.1/contrib/analysis-extras/lib e_wo/8946777B. Add the Library Reference configuration in Solrconfig.xml (/solr-5.2.1/server/solr/user/conf), as follows                    
<lib dir= "${solr.install.dir: /.. /.. /..} /contrib/analysis-extras/lib "regex=". *\.jar "/>

    4. Adding index data using SOLRJ      
/** * Add document */@Testpublic void Adddoc () {solrinputdocument doc = new solrinputdocument ();d Oc.addfield ("id", "page");d OC.ADDF Ield ("username", "haha");d Oc.addfield ("keywords", "haha hello");d Oc.addfield ("Age", "18"); Updateresponse Response;try {response = Httpsolrclient.add (/* "User", */doc);//Submit httpsolrclient.commit ();// Logger.info ("########## Query Time:" + response.getqtime ()); System.out.println ("########## Query Time:" + response.getqtime ());//Logger.info ("########## Elapsed Time:" +//respon Se.getelapsedtime ()); System.out.println ("########## Elapsed Time:" + response.getelapsedtime ());//Logger.info ("########## Status:" + Response.getstatus ()); System.out.println ("########## Status:" + response.getstatus ());} catch (Solrserverexception e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo Auto -generated catch Blocke.printstacktrace ();}}

5. Querying Data   
@Testpublic void Testquery () {solrquery solrquery = new Solrquery ("keywords:* good");//Solrquery.setfilterqueries (" Resourcename:*analytics* "),//Solrquery solrquery = new Solrquery (" *:* ");//Solrquery.setfields (" id "," title "); Solrquery.setstart (0). Setrows (5); try {queryresponse queryresponse = httpsolrclient.query (/* "user", */solrquery);// Logger.info ("Results:" +//queryresponse.getresults (). Getnumfound ()); System.out.println ("Results:" + queryresponse.getresults (). Getnumfound ()); Solrdocumentlist solrdocumentlist = Queryresponse.getresults (); for (Solrdocument solrdocument:solrdocumentlist) { collection<string> FieldNames = Solrdocument.getfieldnames ();//Logger.info ("=============================== ==========="); System.out.println ("=========================================="); for (String field:fieldnames) {//Logger.info ( Field + ":" +//solrdocument.getfieldvalue (field)); System.out.println (Field + ":" + solrdocument.getfieldvalue (field));}}} catch (Solrserverexception e) {//TODO Auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}


You may encounter the following exception Org.apache.solr.client.solrj.impl.httpsolrclient$remotesolrexception:error from server at http:// 192.168.0.12:8983/solr:expected MIME type application/xml but got text/html. <meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>error 404 Not Found</title>
<body><p>problem accessing/solr/update. Reason:
<pre> not found</pre></p></body>

The reason is that collection_name is not specified

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

SOLR Combat 1

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.