Elasticsearch's JAVAAPI Query

Source: Internet
Author: User

Elasticsearch's Javaapi query API

The search API allows you to execute a query that returns a result that matches the query (hits). It can be executed across one or more index, or one or more types. Queries can use the provided query Java API or the filter Java API. The body of the search request is established for use SearchSourceBuilder上 . Here's an example:

Import Org.elasticsearch.action.search.searchresponse;import Org.elasticsearch.action.search.searchtype;import Org.elasticsearch.index.query.filterbuilders.*;import org.elasticsearch.index.query.querybuilders.*;
SearchResponse response = Client.preparesearch ("index1", "Index2"). Settypes ("Type1", "type2"). Setsearchtyp        E (Searchtype.dfs_query_then_fetch). Setquery (Querybuilders.termquery ("Multi", "test")//QUERY . Setpostfilter (Filterbuilders.rangefilter ("age"). from (a) to (+))//Filter. Setfrom (0). SetSize. Setexplain (tr UE). Execute (). Actionget ();

Please note that all parameters are optional. This is the smallest search you can write:

MatchAll on the whole cluster with all default Optionssearchresponse response = Client.preparesearch (). Execute (). Action Get ();
using scrolls in Java

Scroll documentation

A 搜索 request returns something like a "page" of result, and the scroll API can use a search statement to retrieve a large number of results (or even all of the results), as with the traditional query database using cursors.

Scrolling is not used for real-time requests, but instead handles large amounts of data, such as rebuilding the contents of an index for different configurations

Import static Org.elasticsearch.index.query.filterbuilders.*;import static org.elasticsearch.index.query.querybuilders.*; QueryBuilder QB = Termquery ("Multi", "test"); SearchResponse Scrollresp = client.preparesearch (test)         Setsearchtype (Searchtype.scan)          Setscroll (new TimeValue (60000))         Setquery (QB)        . SetSize. Execute (). Actionget (); Hits per shard would be returned for each scroll//scroll until no hits is Returnedwhile (true) {    for (S Earchhit hit:scrollResp.getHits ()) {       //handle the hit...   }    SCROLLRE SP = Client.preparesearchscroll (Scrollresp.getscrollid ()). Setscroll (New TimeValue (600000)). Execute (). Actionget ();     //break condition:no Hits is returned    if (Scrollresp.gethits (). Gethits (). Length = = 0) {        break;   }}
Threading Operations

The search API allows you to set up threads to perform operations so that the actual ginger execution API executes on the same node (the API executes on a shard that is assigned on the same server).

There are three kinds of threading modes, the NO_THREADS mode means that the query operation will be performed zaicalling thread. The SINGLE_THREAD pattern means that the search operation will be performed on the local shards, which is the single different thread. THREAD_PER_SHARDmeans that the search operation will be performed on a different thread, for each local shard.

The default mode is THREAD_PER_SHARD .

Multisearch API
Searchrequestbuilder SRB1 = Node.client (). Preparesearch (). Setquery (Querybuilders.querystring ("Elasticsearch")). SetSize (1); Searchrequestbuilder SRB2 = Node.client (). Preparesearch (). Setquery (Querybuilders.matchquery ("name", "Kimchy")). SetSize (1); multisearchresponse sr = node.client (). Preparemultisearch (). Add (SRB1). Add (SRB2). Execute (). Actionge t ();//You'll get all individual responses from multisearchresponse#getresponses () long nbhits = 0;for (multisearchrespon Se.    Item item:sr.getResponses ()) {SearchResponse response = Item.getresponse (); Nbhits + = Response.gethits (). Gettotalhits ();}
Using Facets

The following code shows how to add two facets to your search:

SearchResponse sr = node.client (). Preparesearch (). Setquery (Querybuilders.matchallquery ()). Addfacet (FacetBuilders. Termsfacet ("F1"). Field ("Field")). Addfacet (Facetbuilders.datehistogramfacet ("F2"). Field ("Birth"). Interval ("Year "). Execute (). Actionget ();//Get your facet resultstermsfacet f1 = (termsfacet) sr.getfacets (). Facetsasmap (). Get (" F1 ") ;D atehistogramfacet F2 = (datehistogramfacet) sr.getfacets (). Facetsasmap (). Get ("F2");
The example is facets, but elasticsearch aggregations is a good choice.
Original: Http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/search.html#msearch
I hope that the translation is not misleading.

Elasticsearch's JAVAAPI Query

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.