Home > Software engineering >  Can we query on Field if its mapping is not defined in ES?
Can we query on Field if its mapping is not defined in ES?

Time:12-06

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)

CodePudding user response:

No, You can't query the fields if its not indexed in the Elasticsearch(as you define dynamic:false, it won't be index), you however can see the them as part of _source when you get a document using _search or by document id.

Either change the mapping from dynamic:false to dynamic:true or add the field explicitly in the mapping(if you want to have dynamic:false), if you want to query the field.

CodePudding user response:

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

CodePudding user response:

You can't query on fields which are not specified in mapping and dynamic is set to false . You can only store those fields in _source. https://www.elastic.co/guide/en/elasticsearch/reference/7.4/dynamic.html

  • Related