Search code examples
elasticsearchelasticsearch-mappingelasticsearch-indices

elasticsearch: multifield mapping of multiple fields


i will have a document with multiple fields. let's say 'title', 'meta1', 'meta2', 'full_body'. each of them i want to index in a few different ways (raw, stemming without stop-words, shingles, synonyms etc.). therefore i will have fields like: title.stemming, title.shingles, meta1.stemming, meta1.shingles etc.

do i have to copy paste the mapping definition for each field? or is it possible to create one definition of all ways of indexing/analysing and then only apply it to each of 4 top level fields? if so, how?

  mappings: 
    my_type: 
      properties: 
        title: 
          type: string
          fields: 
            shingles: 
              type: string
              analyzer: my_shingle_analyzer
            stemming:
              type: string
              analyzer: my_stemming_analyzer
        meta1:
           ...                   <-- do i have to repeat everything here?
        meta2:
           ...                   <-- and here?
        full_body:
           ...                   <-- and here?

Solution

  • In your case, you could use dynamic templates with the match_mapping_type setting so that you can apply the same setting to all your string fields:

    {
      "mappings": {
        "my_type": {
          "dynamic_templates": [
            {
              "strings": {
                "match_mapping_type": "string",
                "mapping": {
                  "type": "string",
                  "fields": {
                    "shingles": {
                      "type": "string",
                      "analyzer": "my_shingle_analyzer"
                    },
                    "stemming": {
                      "type": "string",
                      "analyzer": "my_stemming_analyzer"
                    }
                    , ... other sub-fields and analyzers
                  }
                }
              }
            }
          ]
        }
      }
    }
    

    As a result, whenever you index a string field, its mapping will be created according to the defined template. You can also use the match setting, to restrict the mapping to specific field names, only.