Jonas
Jonas

Reputation: 784

Elasticsearch doesn't find value in range query

I launch following query:

GET archive-bp/_search
{
  "query": {
  "bool" : {
    "filter" : [ {
      "bool" : {
        "should" : [ {
          "terms" : {
            "naDataOwnerCode" : [ "ACME-FinServ", "ACME-FinServ CA", "ACME-FinServ NY", "ACME-FinServ TX", "ACME-Shipping APA", "ACME-Shipping Eur", "ACME-Shipping LATAM", "ACME-Shipping ME", "ACME-TelCo-CN", "ACME-TelCo-ESAT", "ACME-TelCo-NL", "ACME-TelCo-PL", "ACME-TelCo-RO", "ACME-TelCo-SA", "ACME-TelCo-Treasury", "Default" ]
          }
        },
        {
          "bool" : {
            "must_not" : {
              "exists" : {
                "field" : "naDataOwnerCode"
              }
            }
          }
        } ]
      }
    }, {
      "range" : {
        "bankCommunicationStatusDate" : {
          "from" : "2006-02-27T06:45:47.000Z",
          "to" : null,
          "time_zone" : "+02:00",
          "include_lower" : true,
          "include_upper" : true
        }
      }
    } ]
  }
}
}

And I receive no results, but the field exists in my index. When I strip off the data owner part, I still have no results. When I strip off the bankCommunicationDate, I get 10 results, so there is the problem. The query of only the bankCommunicationDate:

GET archive-bp/_search 
{
  "query" :
     {
      "range" : {
        "bankCommunicationStatusDate" : {
          "from" : "2016-04-27T09:45:43.000Z",
          "to" : "2026-04-27T09:45:43.000Z",
          "time_zone" : "+02:00",
          "include_lower" : true,
          "include_upper" : true
        }
      }
  }
}

The mapping of my index contains the following bankCommunicationStatusDate field:

"bankCommunicationStatusDate": {
                "type": "date",
                "format": "strict_date_optional_time||epoch_millis"
              }

And there are values for the field bankCommunicationStatusDate in elasticsearch:

What is wrong?

Upvotes: 1

Views: 664

Answers (1)

Andrzej Martyna
Andrzej Martyna

Reputation: 475

What version of Elastic Search do you use? I guess the reason is that you should use "gte/lte" instead of "from/to/include_lower/include_upper".

According to documentation to version 0.90.4 https://www.elastic.co/guide/en/elasticsearch/reference/0.90/query-dsl-range-query.html

Deprecated in 0.90.4.
The from, to, include_lower and include_upper parameters have been deprecated in favour of gt,gte,lt,lte.

The strange thing is that I have tried your example on elastic search version 1.7 and it returns data! I guess real depreciation took place much later - between 1.7 and maybe newer version you have.

BTW. You can isolate the problem even further using Sense plugin for Chrome and this code:

DELETE /test
PUT /test
{
      "mappings": {
          "myData" : {
              "properties": { 
                "bankCommunicationStatusDate": {
                "type": "date"
              }
        }
    }
    }
}

PUT test/myData/1
{
    "bankCommunicationStatusDate":"2016-04-27T09:45:43.000Z"
}
PUT test/myData/2
{
    "bankCommunicationStatusDate":"2016-04-27T09:45:47.000Z"
}


GET test/_search
{
  "query" :
     {
      "range" : {
        "bankCommunicationStatusDate" : {
          "gte" : "2016-04-27T09:45:43.000Z",
          "lte" : "2026-04-27T09:45:43.000Z"
        }
      }
  }
}

Upvotes: 2

Related Questions