【问题标题】:ElasticSearch RANGE query doesn't work as expectedElasticSearch RANGE 查询无法按预期工作
【发布时间】:2020-05-28 03:22:13
【问题描述】:

我正在尝试使用 ES 7.7 Java API 对嵌套文档运行 RANGE 查询。我注意到它没有按预期过滤数据。当我将调试语句放在 ES 运行的实际查询上时,我无法获得适当的结果,主要是针对范围查询的“to”部分。我注意到 LT 的 GT 或 GTE 工作正常,而 LTE 也没有按预期工作。

下面是这个范围查询的简单 Java 代码。请注意,嵌套文档中的权重定义为浮点数,如 ""weight": {"type": "float"},"

query.must(QueryBuilders.nestedQuery(fields[0], QueryBuilders.rangeQuery(c.getField()).from(c.getValues().get(0)).to(c.getValues().get(1)),ScoreMode.None).innerHit(innerHitBuilder));

以下是 LT 的 Java API 代码示例。

query.must(QueryBuilders.nestedQuery(fields[0], QueryBuilders.rangeQuery(c.getField()).lt(c.getValue()), ScoreMode.None).innerHit(innerHitBuilder));

下面是ES对上面语句RANGE查询语句实际运行的查询。

{
   "from":0,
   "size":50,
   "query":{
      "bool":{
         "must":[
            {
               "match_phrase":{
                  "fundsponsor":{
                     "query":"vanguard group inc",
                     "slop":0,
                     "zero_terms_query":"NONE",
                     "boost":1.0
                  }
               }
            },
            {
               "nested":{
                  "query":{
                     "match_phrase":{
                        "holdings.componentticker":{
                           "query":"baba",
                           "slop":0,
                           "zero_terms_query":"NONE",
                           "boost":1.0
                        }
                     }
                  },
                  "path":"holdings",
                  "ignore_unmapped":false,
                  "score_mode":"none",
                  "boost":1.0,
                  "inner_hits":{
                     "name":"holdings.componenttickerbaba",
                     "ignore_unmapped":false,
                     "from":0,
                     "size":100,
                     "version":false,
                     "seq_no_primary_term":false,
                     "explain":false,
                     "track_scores":false
                  }
               }
            },
            {
               "nested":{
                  "query":{
                     "range":{
                        "holdings.weight":{
                           "from":1.0,
                           "to":2.0,
                           "include_lower":true,
                           "include_upper":true,
                           "boost":1.0
                        }
                     }
                  },
                  "path":"holdings",
                  "ignore_unmapped":false,
                  "score_mode":"none",
                  "boost":1.0,
                  "inner_hits":{
                     "name":"holdings.weightnull",
                     "ignore_unmapped":false,
                     "from":0,
                     "size":100,
                     "version":false,
                     "seq_no_primary_term":false,
                     "explain":false,
                     "track_scores":false
                  }
               }
            }
         ],
         "adjust_pure_negative":true,
         "boost":1.0
      }
   },
   "_source":{
      "includes":[
         "fundsymbol",
         "fundsponsor",
         "fundname",
         "componentcount",
         "holdings.componentticker",
         "holdings.weight"
      ],
      "excludes":[



      ]
   }
}

请注意以下部分,这是这里的罪魁祸首。其他一切都按预期工作。正如我上面提到的,“from”按预期工作,它按预期转换为 GTE,但“to”不起作用,“LT”或“LTE”也不起作用,出于某种奇怪的原因。

               "nested":{
                  "query":{
                     "range":{
                        "holdings.weight":{
                           "from":1.0,
                           "to":2.0,
                           "include_lower":true,
                           "include_upper":true,
                           "boost":1.0
                        }
                     }
                  },

【问题讨论】:

    标签: elasticsearch elasticsearch-dsl elasticsearch-java-api


    【解决方案1】:

    首先,fromto 不是范围查询属性。范围查询需要ltelegtege。查询应如下所示:

    {
        "query": {
            "nested": {
                "path": "holdings",
                "query": {
                    "range": {
                        "holdings.weight": {
                            "gte": 1,
                            "lte": 2
                        }
                    }
                }
            }
        }
    }
    

    其次,检查您的索引映射并确保 holdings 映射为 nested,并且 weight 映射为 float

    【讨论】:

    • 我不确定这是真的,因为 ES 文档说 from 和 to 是范围查询的正确属性。 elastic.co/guide/en/elasticsearch/client/java-api/current/…
    • 你说得对,这也是一种选择。你检查过映射吗?
    • 是的。它被定义为浮动。不知何故,我尝试了一个不同的索引,它工作得很好,所以我假设它与映射或加载数据时的其他东西有关。
    • 您是否在索引某些文档后更改了映射?这可能是一个很好的理由。
    猜你喜欢
    • 2018-10-13
    • 1970-01-01
    • 2019-10-24
    • 2016-11-01
    • 2019-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多