代码之家  ›  专栏  ›  技术社区  ›  valik

在ElasticSearch Java APi中组合查询参数

  •  1
  • valik  · 技术社区  · 8 年前

    我有一个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 回复  |  直到 8 年前
        1
  •  2
  •   Val    8 年前

    正确的查询如下所示:

        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);