Distributed Search Elasticsearch Java API (v)------Search

Source: Internet
Author: User

The Elasticsearch query is to construct the QueryBuilder object in the Java API by executing the query criteria in JSON format, Elasticsearch fully supports the QUERYDSL style query, The QueryBuilder build class is the Querybuilders,filter build class is filterbuilders. The following is an example of constructing QueryBuilder:

Import static org.elasticsearch.index.query.filterbuilders.*; 
		Import static org.elasticsearch.index.query.querybuilders.*; 
		 
		QueryBuilder QB1 = termquery ("name", "Kimchy"); 
		 
		QueryBuilder QB2 = Boolquery () 
		                    . Must (Termquery ("Content", "Test1")) 
		                    . Must (Termquery ("Content", "test4")) 
		                    . Mustnot (Termquery ("Content", "Test2")) 
		                    . Should (Termquery ("Content", "test3")); 
		 
		QueryBuilder Qb3 = filteredquery ( 
		            termquery ("Name.first", "Shay"),  
		            rangefilter ("Age") 
		                . From ( 
		                ) . to () 
		                . Includelower (True) 
		                . Includeupper (false) 
		            ;

Where QB1 constructs a termquery that searches for the name field, which is the smallest index fragment, which corresponds to the termquery of the Lucene itself. QB2 constructs a combination query (boolquery), which corresponds to the booleanquery of Lucene itself, can be combined with should by must, Mustnot and QueryBuilder methods to form a multiple condition query. Qb3 constructs a filter query that adds a filter condition based on Termquery Rangefilter, which restricts the query age field to a result that is greater than or equal to 23, or less than or equal to 54. In addition to these three, Elasticsearch also supports many kinds of inquiries, later write an introduction.

The construction of query will be sent to the elasticsearch inside for inquiries, the following is an example:

SearchResponse response = Client.preparesearch ("Test"). 
        setquery (query). 
        setfrom (0). SetSize (60). Setexplain (True) 
        . Execute () 
        . Actionget ();

The query Test index, query criteria is queried, starting from the No. 0 record, returns up to 60 records. The return result is SearchResponse, and the following resolves SearchResponse:

Searchhits hits = Searchresponse.hits ();
		for (int i = 0; i < i++) {System.out.println (
			hits.getat (i)-getsource (). Get ("field"));
		

The

Obtains the searchhits in SearchResponse, and then Hits.getat (i). GetSource (). Get ("field") to obtain the value of the field fields.

This article address: http://blog.csdn.net/laigood12345/article/details/7606011

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.