ElasticSearch 6, copy_to with dynamic index mappin

2020-07-10 08:40发布

问题:

Maybe I'm missing something simple, but still could not figure out the following thing:

As of ES 6.x the _all field is deprecated, and instead it's suggested to use the copy_to instruction (https://www.elastic.co/guide/en/elasticsearch/reference/current/copy-to.html).

However, I got an impression that you need to explicitly specify the fields which you want to copy to the custom _all field. But if I use dynamic mappings, I don't know the fields in advance, and therefore cannot use copy_to?

Any way I can tell ES to copy all encountered fields to the custom _all field so that I can search across all fields?

Thanks in advance!

回答1:

You could use Dynamic Templates. Basically create an index, add the custom catch_all field and then specify that particular property for all the fields that are strings. (Haven't done this before, but I believe this is the only way now. Since the field catch_all will be already present when you put the dynamic template, it will not match the catch_all - meaning that the catch_all will not copy to itself, but check it out yourself to make sure).

PUT my_index
{
  "mappings": {
    "_doc": {
      "dynamic_templates": [
        {
          "strings": {
            "match_mapping_type": "string",
            "mapping": {
              "type": "text",
              "copy_to": "catch_all"
            }
          }
        }
      ]
    }
  }
}