Elasticsearch Chinese API search (vi)

Source: Internet
Author: User

Search API

The search API allows developers to execute a search query that returns search information that satisfies the query criteria. It can be executed across indexes and across types. Queries can be either Java query API or Java filtering API. The request body of the query is SearchSourceBuilder built by.

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 (" type2 "). Setsearchtype ( Searchtype.dfs_query_then_fetch). Setquery (Querybuilders.termquery ( "multi", //Query. Setpostfilter (Filterbuilders.rangefilter 12). to (18)) //Filter. Setfrom (0). SetSize (60) . Setexplain (true). Execute (). Actionget ();        

Note that all parameters are optional. The following is the most concise form.

// MatchAll on the whole cluster with all default optionsSearchResponse response = client.prepareSearch().execute().actionGet();
Using Scrolls in Java
ImportStatic org.elasticsearch.index.query.filterbuilders.*;ImportStatic 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 (); //100 hits per shard would be returned for each scroll//scroll until no hits is returned while(true) {
                 
                  for (Searchhit hit:scrollResp.getHits ()) { 
                  //handle the hit ...} SCROLLRESP = Client.preparesearchscroll (scrollresp . Getscrollid ()). Setscroll (new TimeValue (600000)). Execute (). Actionget (); //break condition:no Hits is returned if (Scrollresp.gethits (). Gethits (). length = = 0) {break ;}} 
                 
Multi-Search 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().actionGet();// You will get all individual responses from MultiSearchResponse#getResponses()long nbHits = 0;for (MultiSearchResponse.Item item : sr.getResponses()) { SearchResponse response = item.getResponse(); nbHits += response.getHits().getTotalHits();}
Using aggregations

The following example shows how to add two aggregations to your search.

SearchResponse sr = node.client().prepareSearch()    .setQuery(QueryBuilders.matchAllQuery())    .addAggregation(            AggregationBuilders.terms("agg1").field("field")    )    .addAggregation(            AggregationBuilders.dateHistogram("agg2")                    .field("birth")                    .interval(DateHistogram.Interval.YEAR)    )    .execute().actionGet();// Get your facet resultsTerms agg1 = sr.getAggregations().get("agg1");DateHistogram agg2 = sr.getAggregations().get("agg2");
Using the search template

Define your template parameters asMap<String,String>

new HashMap<>();template_params.put("param_gender", "male");

You can use the template that you saved in config/scripts the directory. For example, you have the following fileconfig/scripts/template_gender.mustache

{    "template" : {        "query" : {            "match" : {                "gender" : "{{param_gender}}"            }        }    }}

This can be done in the following ways:

SearchResponse sr = client.prepareSearch()        .setTemplateName("template_gender")        .setTemplateType(ScriptService.ScriptType.FILE)        .setTemplateParams(template_params)        .get();

You can also store the template in a special index named.scripts

client.preparePutIndexedScript("mustache", "template_gender",        "{\n" +        "    \"template\" : {\n" +        " \"query\" : {\n" + " \"match\" : {\n" + " \"gender\" : \"{{param_gender}}\"\n" + " }\n" + " }\n" + " }\n" + "}").get();

In order to use this indexed template, you need to use ScriptService.ScriptType.INDEXED :

SearchResponse sr = client.prepareSearch()        .setTemplateName("template_gender")        .setTemplateType(ScriptService.ScriptType.INDEXED)        .setTemplateParams(template_params)        .get();

Elasticsearch Chinese API search (vi)

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.