Distributed search elasticsearch Java API (5) -- search

Source: Internet
Author: User

Elasticsearch queries query conditions in JSON format. in Java APIs, Query Builder objects are constructed. elasticsearch fully supports querydsl-style queries. The construction class of querybuilder is querybuilders, the 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 ")). shocould (termquery ("content", "test3"); querybuilder qb3 = filteredquery (termquery ("name. first "," Shay "), rangefilter (" Age "). from (23 ). to (54 ). includelower (true ). includeupper (false ));

QB1 constructs a termquery to search for the name field, which is the smallest index segment. This query corresponds to the termquery of Lucene. Qb2 constructs a Combined Query (boolquery), which corresponds to the booleanquery of Lucene. It can combine querybuilder through the must, shoshould, and mustnot methods to form a multi-condition query. Qb3 constructs a filter query, that is, adds a filter condition rangefilter Based on termquery. This range filter restricts the query results of age fields greater than or equal to 23 and less than or equal to 54. In addition to these three methods, elasticsearch also supports many types of query methods, which are described later.

After a query is constructed, it must be passed to elasticsearch for query. The following is an example:

 
Searchresponse response = client. preparesearch ("test"). setquery (query). setfrom (0). setsize (60). setexplain (true). Execute (). actionget ();

The query condition is query. A maximum of 60 records are returned starting from 0th records. The returned result is searchresponse, Which is parsed as follows:

 
Searchhits hits = searchresponse. hits (); For (INT I = 0; I <60; I ++) {system. out. println (hits. getat (I ). getsource (). get ("field "));}

Obtain the searchhits in searchresponse, and then hits. getat (I). getsource (). Get ("field") to obtain the field value.

Address: http://blog.csdn.net/laigood12345/article/details/7606011

Related Article

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.