elasticsearch - 在ElasticSearch Java APi中组合QueryParameters

标签 elasticsearch

我有要搜索的json对象,看起来像这样
我目前能够进行一系列价格搜索,但我想按城市添加搜索,这是出发对象的属性,因此我决定进行嵌套查询,但出现错误

我的ES日志显示

e_synonyms_phrase_query":true,"boost":1.0}}}],"adjust_pure_negative":true,"boost":1.0}},"path":"departure","ignore_unmapped":false,"score_mode":"avg","boost":1.0}}],"adjust_pure_negative":true,"boost":1.0}}}}] lastShard [true]
org.elasticsearch.transport.RemoteTransportException: [AoBfpnE][127.0.0.1:9300][indices:data/read/search[phase/query]]
Caused by: org.elasticsearch.index.query.QueryShardException: failed to create query: {
  "bool" : {
    "filter" : [
      {
        "range" : {
          "price" : {
            "from" : 1300,
            "to" : 5000,
            "include_lower" : true,
            "include_upper" : true,
            "boost" : 1.0
          }
        }
      },
      {
        "nested" : {
          "query" : {
            "bool" : {
              "must" : [
                {
                  "match" : {
                    "departure.city" : {
                      "query" : "minsk",
                      "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
            }
          },
          "path" : "departure",
          "ignore_unmapped" : false,
          "score_mode" : "avg",
          "boost" : 1.0
        }
      }
    ],
    "adjust_pure_negative" : true,
    "boost" : 1.0
  }
}
    at org.elasticsearch.index.query.QueryShardContext.toQuery(QueryShardContext.java:320) ~[elasticsearch-6.3.2.jar:6.3.2]
    at org.elasticsearch.index.query.QueryShardContext.toQuery(QueryShardContext.java:303) ~[elasticsearch-6.3.2.jar:6.3.2]

最佳答案

正确的查询如下所示:

    QueryBuilder range = QueryBuilders.rangeQuery("price")
            .from(minPrice)
            .to(maxPrice)
            .includeLower(true)
            .includeUpper(true);

    QueryBuilder city = QueryBuilders.matchQuery("departure.city", city);

    QueryBuilder query = QueryBuilders.boolQuery()
            .filter(range)
            .filter(city);

    ...

    searchSourceBuilder.query(query);

关于elasticsearch - 在ElasticSearch Java APi中组合QueryParameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51668825/

相关文章:

ruby-on-rails - 获得Tire::Results::Collection的随机结果

elasticsearch - 使用单个查询字符串和任意顺序的单词查询多个字段

elasticsearch - 如何从ElasticSearch中的文件导入GEO_TYPE

sql - Elasticsearch,查询字符串,需要匹配字符串

json - Elasticsearch查询检索所有ID的特定类型的特定_source值

c# - C#嵌套Elasticsearch地理点阵列索引未在Kibana中显示

elasticsearch - 术语查询返回 0 个结果

search - Elasticsearch 查询最大值聚合

Elasticsearch bool 搜索匹配不正确

sorting - Elasticsearch排序是在索引分析(扫描)之后还是之前进行的?