Mary
Mary

Reputation: 1125

Why is my date query in Elasticsearch failing?

I'm currently following a web tutorial, and when I run:

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

I get a bunch of records including:

  {
    "_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"
    }
  },

When I run:

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

across the same data set, I get:

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

Why am I getting zero hits from my query? I understand the search is scanning for records in with a created date greater than 2010/01/01, therefore shouldn't it match at least id 366?

Edit:

Here are my mappings - it seems to be a date type:

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

Upvotes: 1

Views: 43

Answers (1)

Pierre Mallet
Pierre Mallet

Reputation: 7221

You have a typo in your properties name. You have an extra space after the "created":

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

To diagnos this you should call

GET /product/_mapping

and check that your field created is really mapped as a date. In your case it was a text.

Upvotes: 2

Related Questions