在ElasticSearch Java APi中组合QueryParameters

时间:2018-08-03 08:53:59

标签: 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]

1 个答案:

答案 0 :(得分: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);