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