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