2
votes

I'm trying to simply disable dynamic mapping for any fields not explicitly defined in the mapping at index creation time. Nothing would work, so I even tried the example in their docs

PUT my_index
{
  "mappings": {
    "my_type": {
      "dynamic": false,
      "properties": {
        "user": {
          "type": "text"
        }
      }
    }
  }
}

Made a test insert:

POST my_index/my_type
{
  "user": "tester",
  "some_unknown_field": "lsdkfjsd"
}

Then searching the index shows:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "my_index",
        "_type": "my_type",
        "_id": "AViPrfwVko8c8Q3co8Qz",
        "_score": 1,
        "_source": {
          "user": "tester",
          "some_unknown_field": "lsdkfjsd"
        }
      }
    ]
  }
}

I'm expecting "some_unknown_field" to not be indexed, since it was not defined in the mapping. So why is it still being indexed? Am I missing something?

UPDATE

It turns out that it isn't currently possible in version 5.0.0 to do what I wanted, so I removed the fields in my app before sending to elasticsearch and achieved the same end result.

1

1 Answers

0
votes

What mapping does is to have your field as the type which you mention, when you create the index using the mapping. So for a field which you haven't mentioned anything during the mapping and then trying to insert values, ES will always consider it as a new field and will add it to the index with a default mapping. So if you don't want to see a particular field within your _source you could do some source filtering.

Work arounds:

  1. If that's not the case try disabling the default mapping when you're creating the index.

  2. Try making the property dynamic into strict:

    PUT /test
        {
          "settings": {
            "index.mapper.dynamic": false
          },
          "mappings": {
            "testing_type": {
              "dynamic":"strict",
              "properties": {
                "field1": {
                  "type": "string"
                }
              }
            }
          }
        }

If the above two doesn't work out, try making index_mapper_dynamicto false. This SO could be handy. Hope it helps.