为什么我在Elasticsearch中的日期查询失败?

时间:2019-03-03 23:14:22

标签: elasticsearch

我目前正在关注网络教程,并且在我运行时:

GET /product/_doc/_search
{
  "query": {
    "range": {
      "in_stock": {
        "gte": 1,
        "lte": 5
      }
    }
  }
}

我得到了一堆记录,包括:

  {
    "_index" : "product",
    "_type" : "_doc",
    "_id" : "366",
    "_score" : 1.0,
    "_source" : {
      "name" : "Eggplant - Baby",
      "price" : 58,
      "in_stock" : 1,
      "sold" : 187,
      "tags" : [ ],
      "description" : "Mauris sit amet eros. Suspendisse accumsan tortor quis turpis. Sed ante. Vivamus tortor. Duis mattis egestas metus. Aenean fermentum. Donec ut mauris eget massa tempor convallis. Nulla neque libero, convallis eget, eleifend luctus, ultricies eu, nibh.",
      "is_active" : false,
      "created" : "2016/01/03"
    }
  },

当我跑步时:

GET /product/_doc/_search
{
  "query": {
    "range": {
      "created": {
        "gte": "2010/01/01"
      }
    }
  }
}

在同一数据集上,我得到:

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

为什么我的查询中的点击量为零?我了解搜索正在扫描创建日期大于2010/01/01的记录,因此它是否至少不匹配id 366?

编辑:

这是我的映射-似乎是日期类型:

{
  "product" : {
    "aliases" : { },
    "mappings" : {
      "_doc" : {
        "dynamic" : "false",
        "properties" : {
          "created " : {
            "type" : "date",
            "format" : "yyyy/MM/dd HH:mm:ss||yyyy/MM/dd"
          },
          "description" : {
            "type" : "text"
          },
          "discount" : {
            "type" : "integer"
          },
          "in_stock" : {
            "type" : "integer"
          },
          "is_active" : {
            "type" : "boolean"
          },
          "name" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword"
              }
            }
          },
          "price" : {
            "type" : "integer"
          },
          "sold" : {
            "type" : "long"
          },
          "tags" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword"
              }
            }
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1551151769380",
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "uuid" : "3YTYcG-9TPeT_3jXfX5IMA",
        "version" : {
          "created" : "6060199"
        },
        "provided_name" : "product"
      }
    }
  }
}

1 个答案:

答案 0 :(得分:2)

您的属性名称中有一个错字。在“创建”之后,您还有多余的空间:

"properties" : {
          "created " : { <-- here
            "type" : "date",
            "format" : "yyyy/MM/dd HH:mm:ss||yyyy/MM/dd"
          },

要对此进行诊断,您应该致电

GET /product/_mapping

,并检查您的字段created是否确实映射为日期。在您的情况下,这是文本。