范围查询不返回Elasticsearch中的结果

时间:2017-05-03 11:49:49

标签: json elasticsearch range-query

我正在尝试获取我通过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"
}
}
}

但上述查询并未返回任何成功的匹配,但它至少应返回上述文档。

1 个答案:

答案 0 :(得分:0)

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

{
  "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"
            }
          }
        } ]
      }
    }