简体   繁体   English

弹性搜索:使用弹性搜索 API 生成弹性搜索查询

[英]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?我想知道硬线查询和通过 API 生成的查询之间的区别? I am using elastic search API to build a query.我正在使用弹性搜索 API 来构建查询。 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.但是我使用 ElasticSearch 查询生成器 API 获得的 output 具有以下格式。 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. elasticsearch spring-data 库生成的查询具有 spring-data-elasticsearch 的当前文档中记录的默认值,它实际上取决于您正在运行的 elasticsearch 版本。 If you really want to use a pure query created by you, you can take a look at the Query annotation here如果你真的想使用自己创建的纯查询,可以看一下这里Query注解

Here is some useful documentation regarding the extra properties added in the generated query:以下是一些关于在生成的查询中添加的额外属性的有用文档:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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