简体   繁体   中英

Elastic Search: Generating Elastic Search query using Elastic search API

I would like to know the difference between the hard corded query and the query generated through API's? I am using elastic search API to build a query. The resultant query that I am expecting is of the below format.

{
  "query": {
    "bool": {
      "must": [
    {"match": {
      "model": "IBC"
      }
    },{"match": {
      "company.name": "Samsung"
      }
    }
      ]
    }
  }
}

But the output that I get using ElasticSearch query builder API is of the below format. I have observed that the results obtained by both these queries are exactly similar for a small datasets. I am not sure, as to how it will behave for large datasets.

{
  "size": 100,
  "query": {
    "bool": {
      "must": [
    {
      "match": {
        "company.name": {
          "query": "Samsung",
          "operator": "OR",
          "prefix_length": 0,
          "max_expansions": 50,
          "fuzzy_transpositions": true,
          "lenient": false,
          "zero_terms_query": "NONE",
          "auto_generate_synonyms_phrase_query": true,
          "boost": 1.0
        }
      }
    },
    {
      "match": {
        "model": {
          "query": "IBC",
          "operator": "OR",
          "prefix_length": 0,
          "max_expansions": 50,
          "fuzzy_transpositions": true,
          "lenient": false,
          "zero_terms_query": "NONE",
          "auto_generate_synonyms_phrase_query": true,
          "boost": 1.0
        }
      }
    }
      ],
      "adjust_pure_negative": true,
      "boost": 1.0
    }
  }
}

The code that i have written for generating the query:

Map<String,String> parametersMap = new HashMap<String,String>();
parametersMap.put("model","IBC");
parametersMap.put("company.name","Samsung");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

for(String key: parametersMap){
    MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery(key,parametersMap.get(key);
        boolQueryBuilder = boolQueryBuilder.must(matchQueryBuilder);                    
}        
    searchSourceBuilder.size(100).query(boolQueryBuilder);
    String result = searchSourceBuilder.toString();

The queries generated by the elasticsearch spring-data library are with default values documented in the current documentation of the spring-data-elasticsearch and it really depends on what elasticsearch version you are running. If you really want to use a pure query created by you, you can take a look at the Query annotation here

Here is some useful documentation regarding the extra properties added in the generated query:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM