Spring integrates SOLR and tests

Source: Internet
Author: User
Tags solr solr query xmlns

1, first introduce the SOLR query, the meaning of the key words

Parameters

Describe

Example

Q

queries that are searched for in SOLR. For a complete description of the syntax, see "Lucene queryparser Syntax" in resources. You can include sorting information by appending a semicolon and the name of a field that is indexed and not broken down (explained below). The default sort is score desc, which refers to descending sort by score.

Q=myfield:java and Otherfield:developerworks; Date ASC

This query searches for the specified two fields and sorts the results based on a date field.

Start

Assigns the initial offset to the result set. can be used to page out the results. The default value is 0.

Start=15

Returns the result from the beginning of the 15th result.

Rows

Returns the maximum number of documents. The default value is 10.

Rows=25

Fq

Provides an optional filter query. The query results are limited to the results returned by the search filter query only. The filtered query is cached by SOLR. They are useful for increasing the speed of complex queries.

Any valid query that can be passed with the Q parameter, except for sort information.

hl

When Hl=true, the fragment is displayed prominently in the query response. The default is False. See the SOLR Wiki section on the highlighting parameters for more options (see Resources).

Hl=true

Fl

A comma-delimited list specifies the Field set that should be returned in the document results. The default is "*", which refers to all fields. "Score" means that the score should also be returned.

*,score


2. SOLR Link Parameters

Solr.url=http://ip:port/solrname
Solr.maxretries=1  


3. SOLR and Spring integration

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:x Si= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:mvc= "Http://www.springframework.org/schema/mvc" Xsi:sche malocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.2.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc-3.2.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-3.2.xsd "> <!--define SOLR's server--> <bean id=" Httpsolrserver "class=" org.apache.s Olr.client.solrj.impl.HttpSolrServer "> <constructor-arg index=" 0 "value=" ${solr.url} "/> <!-- Set Response resolver-<property name= "parser"> <bean class=" org.apache.solr.client.solrj.impl.XMLResponseParser "/> </property> <!--Set the retry count-<property name= "maxretries" value= "${solr.maxretries}"/> <!-- Maximum time to establish a connection--<property name= "ConnectionTimeout" value= "${solr.connectiontimeout}"/> </bean&gt  
;   </beans>

4. Test class

Package com.vip.search.test;
Import Java.util.Iterator;

Import Java.util.Set;
Import Org.apache.solr.client.solrj.SolrQuery;
Import Org.apache.solr.client.solrj.impl.HttpSolrServer;
Import Org.apache.solr.client.solrj.response.QueryResponse;
Import org.apache.solr.common.SolrDocumentList;
Import Org.junit.Test;
Import Org.junit.runner.RunWith;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.test.context.ContextConfiguration;

Import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner; JUnit initiates the related configuration of Spring @RunWith (Springjunit4classrunner.class) @ContextConfiguration (locations = {"classpath*:/ Spring/applicationcontext.xml "," Classpath*:springmvc-servlet.xml "}) public class solrtest{//Get SOLR service @Autowired pri
	
	Vate Httpsolrserver Httpsolrserver;
             @Test public void query () {try {//create SOLR queries solrquery query = new Solrquery ();  
        Set the query condition Query.set ("Q", "* *");     Query.set ("Fq", "* *");  
             Query.set ("FL", "* *");  
             Query.set ("Start", "0");  
             Query.set ("WT", "XML"); 
             Queryresponse RSP = httpsolrserver.query (query);
             Solrdocumentlist list = Rsp.getresults (); 
             System.out.println ("Hit Quantity:" +rsp.getresults (). Getnumfound ());
                 for (int i = 0; i < list.size (); i++) {set<string> KeySet = List.get (i). KeySet ();
                 iterator<string> Iterator = Keyset.iterator ();
                 System.out.println ();
 					while (Iterator.hasnext ()) {String key = Iterator.next ();
 				System.out.println (key+ ": ===========:" +list.get (i). GetFieldValue (key));
		}}} catch (Exception e) {e.printstacktrace ();
 }  
	}
}

5. Add SOLR Client Dependency

		<!--SOLR Client SOLRJ dependency--
		<dependency>
			<groupId>org.apache.solr</groupId>
			<artifactId>solr-solrj</artifactId>
			<version>4.10.3</version>
			<exclusions >
				<exclusion>
					<artifactId>httpclient</artifactId>
					<groupId> org.apache.httpcomponents</groupid>
				</exclusion>
				<exclusion>
					<artifactid >httpcore</artifactId>
					<groupId>org.apache.httpcomponents</groupId>
				</ exclusion>
			</exclusions>
		</dependency>
It is important to note that this dependency is possible and
    		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId> httpclient</artifactid>
			<version>4.5.3</version>
		</dependency>

Create a conflict If you add httpclient dependencies to your project, you should remove the httpclient from SOLR



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.