【问题标题】:Range Query not returning results in Elasticsearch范围查询未在 Elasticsearch 中返回结果
【发布时间】:2017-05-04 00:41:44
【问题描述】:

我正在尝试获取已通过 elasticsearch 索引的文档的以下部分:

temporal: {
begin: "2016-11-30T00:00:00",
end: "2016-12-08T13:55:02"
},

我在 CURL 上使用的查询,因为我目前只是在 localhost 上测试查询:

curl -XGET 'localhost:9201/_search?pretty' -H 'Content-Type: application/json' -d'
{
    "query": {
        "range" : {
            "timestamp" : {
                "gte": "2015-01-01 00:00:00", 
                "lte": "now"
            }
        }
    }
}
'

映射

temporal: {
properties: {
begin: {
type: "date"
},
end: {
type: "date"
}
}
}

但是上面提到的查询没有返回任何成功的命中,尽管它应该至少返回上面提到的文档。

【问题讨论】:

  • 您的文档顶部没有任何timestamp 字段。
  • @val 我尝试将查询中的时间戳字段更改为临时字段,但它仍然没有生成任何结果
  • 您需要将其更改为temporal.begintemporal.end,具体取决于您要检查的数据字段。
  • @Val 所以我想检查 temporal.begin 并相应地编辑了查询,但结果仍然没有出来
  • 你能展示你的映射吗? curl -XGET localhost:9200/your_index ?

标签: json elasticsearch range-query


【解决方案1】:

假设您想在发布时进行搜索(请注意,我已经格式化了日期时间输入),这是时间戳 - 映射将是。

{
  "temporal" : {
    "mappings" : {
      "doc" : {
        "properties" : {
          "temporal" : {
            "properties" : {
              "begin" : {
                "type" : "date",
                "format" : "strict_date_optional_time||epoch_millis"
              },
              "end" : {
                "type" : "date",
                "format" : "strict_date_optional_time||epoch_millis"
              }
            }
          }
        }
      }
    }
  }
}

索引/查询文档:

curl -XPOST  localhost:9200/temporal/doc/1 -d '
{"temporal": {
"begin": "2016-11-30T00:00:00",
"end": "2016-12-08T13:55:02"
}}';


curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d'
    {
        "query": {
            "range" : {
                "temporal.begin" : {
                    "gte": "2015-01-01T00:00:00", 
                    "lte": "now"
                }
            }
        }
    }'
    {
      "took" : 6,
      "timed_out" : false,
      "_shards" : {
        "total" : 108,
        "successful" : 108,
        "failed" : 0
      },
      "hits" : {
        "total" : 1,
        "max_score" : 1.0,
        "hits" : [ {
          "_index" : "temporal",
          "_type" : "doc",
          "_id" : "1",
          "_score" : 1.0,
          "_source" : {
            "temporal" : {
              "begin" : "2016-11-30T00:00:00",
              "end" : "2016-12-08T13:55:02"
            }
          }
        } ]
      }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-04
    • 2016-08-14
    • 1970-01-01
    相关资源
    最近更新 更多