Search code examples
elasticsearchelasticsearch-7

Can we query on Field if its mapping is not defined in ES?


Is it possible to Query on field which is not mapped with order?? Using Elastic search 7.4

I've created a index with with only 1 mapping

Index name - test_date_mapping_with_null Dynamic mapping - False properties - city -> text.

{
  "settings" : {
    "number_of_shards" : 2,
    "number_of_replicas" : 1
  },
  "mappings" : {
    "dynamic":false,
    "properties" : {
        "city" : { "type" : "text" }
    }
  }
}

Inserting documents with published_at field

POST test_date_mapping_with_null/_doc/1
{
  "city": "NY",
  "published_at": "2022-01-01T06:58:27.000Z"
}
POST test_date_mapping_with_null/_doc/2
{
  "city": "Paris",
  "published_at": "2022-01-02T06:58:27.000Z"
}
POST test_date_mapping_with_null/_doc/3
    {
      "city": "Mumbai",
      "published_at": "2022-01-03T06:58:27.000Z"
    }
POST test_date_mapping_with_null/_doc/4
    {
      "city": "Tokyo",
      "published_at": "2022-01-04T06:58:27.000Z"
    }

Mapping looks like this

"mappings": {
      "_doc": {
        "dynamic": "false",
        "properties": {
          "city": {
            "type": "text"
          }
        }
      }
    }

Now Upon Search Query

GET test_date_mapping_with_null/_search
{
  "query": {
    "range": {
      "published_at": {
        "gte": "2022-01-01T00:58:27.000Z",
        "lte": "2022-01-03T23:58:27.000Z",
        "boost": 2.0
      }
    }
  }
}

Actual - ES returns all the docs. Expected - ES should return only Doc 1, 2 and 3 (i.e City -> NY, Paris and Mumbai Doc)


Solution

  • Your index mapping, currently only includes mapping for the city field, it does not have mapping for the published_at field as you have set "dynamic": "false" in your index mapping.

    This means that published_at is stored in Elasticsearch, but this field is not indexed in Elasticsearch. In simple terms, this means that you cannot perform any search on the published_at field