【问题标题】:Number ranges / filters in ElasticsearchElasticsearch 中的数字范围/过滤器
【发布时间】:2015-07-03 23:34:11
【问题描述】:

我正在尝试在 elasticsearch 中建立索引,然后搜索数字字段。结果集是空的,即使逻辑结果是有 1 条记录的结果集。

下面的动作重现(使用感觉)

创建索引

PUT playground

创建文档

POST playground/doc
{
   "value": {
      "textlabel": "Lorem Ipsum",
      "numerlabel": 37.0,
      "datelabel":"1978-10-26T00:00:00+02:00"
   }
}

自动生成的映射文件似乎提供了正确的数据类型

{
   "playground": {
      "mappings": {
         "doc": {
            "properties": {
               "value": {
                  "properties": {
                     "datelabel": {
                        "type": "date",
                        "format": "dateOptionalTime"
                     },
                     "numerlabel": {
                        "type": "double"
                     },
                     "textlabel": {
                        "type": "string"
                     }
                  }
               }
            }
         }
      }
   }
}

搜索日期范围可以正常工作,返回预期数据

POST playground/doc/_search
{
    "query": {         
        "filtered": {           
           "filter": {
               "range" : {
                    "value.datelabel" : {                        
                        "lte": "now-28y" 
                    }
                }
           }
        }
    }
}

但数值范围不返回任何结果

POST playground/doc/_search
{
    "query": {
      "filtered": {           
           "filter": {
               "range":  {
                    "value.numberlabel" : {             
                        "lte": 100
                    }
                }
           }
        }
    }
}

结果

{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 0,
      "max_score": null,
      "hits": []
   }
}

有什么建议吗?

【问题讨论】:

    标签: search elasticsearch numeric


    【解决方案1】:

    您有一个错字:numerlabel - numberlabel。鉴于映射是正确的查询:

    {
      "query": {
        "filtered": {
          "filter": {
            "range": {
              "value.numerlabel": {
                "lte": 100
              }
            }
          }
        }
      }
    }
    

    【讨论】:

    • 谢谢 Andrei - 我检查了查询的拼写 100 次...但没有查看文档创建。非常感谢!
    【解决方案2】:

    你只是拼错了。 "numerlabel" 在您的文档中,但在您的查询中 "value.numberlabel"

    在我运行了你的设置代码之后,它就可以工作了:

    POST playground/doc/_search
    {
        "query": {
          "filtered": {           
               "filter": {
                   "range":  {
                        "value.numerlabel" : {             
                            "lte": 100
                        }
                    }
               }
            }
        }
    }
    ...
    {
       "took": 3,
       "timed_out": false,
       "_shards": {
          "total": 5,
          "successful": 5,
          "failed": 0
       },
       "hits": {
          "total": 1,
          "max_score": 1,
          "hits": [
             {
                "_index": "playground",
                "_type": "doc",
                "_id": "AU5UiwngQAg_uFp56nys",
                "_score": 1,
                "_source": {
                   "value": {
                      "textlabel": "Lorem Ipsum",
                      "numerlabel": 37,
                      "datelabel": "1978-10-26T00:00:00+02:00"
                   }
                }
             }
          ]
       }
    }
    

    【讨论】:

    • 感谢 Sloan,我检查了查询的拼写 100 次...但没有查看文档创建。非常感谢!由于 Andrei 稍早一点,我会接受他的回答 - 你的回答同样正确 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-04
    相关资源
    最近更新 更多