0

How do I specify a multifield index mapping that targets a property inside an array?

For example

 Document
 --------
 ID: 1
 Title: Some title goes here
 Content: Some content goes here
 SubDocuments:[{"Title:"Some title"},{"Title:"Some title"}]

How can I specify an index mapping that targets Title inside SubDocuments to have touched and untouched variations?

Thanks

Imran Azad
  • 1,008
  • 2
  • 12
  • 30

1 Answers1

0

Arrays are supported out of the box by Elasticsearch and they don't need a specific mapping. You simply need to map the SubDocuments field as an object (or nested object) and then the Title field as a multi-field in that object.

{
    "your_type" : {
        "properties" : {
            "ID" : {"type" : "long"},
            "Title" : {"type" : "string"},
            "Content" : {"type" : "string"},
            "SubDocuments" : {
                "properties" : {
                    "Title" : {
                        "type" : "string",
                        "fields": {
                            "raw": {
                                "type" : "string",
                                "index" : "not_analyzed",
                            }
                        }
                    }
                }
            }
        }
    }
}

It is worth noting that if you envision to add other fields to SubDocuments and you need to query on those SubDocuments fields, it might be wiser to map SubDocuments as nested type, like this:

{
    "your_type" : {
        "properties" : {
            "ID" : {"type" : "long"},
            "Title" : {"type" : "string"},
            "Content" : {"type" : "string"},
            "SubDocuments" : {
                "type": "nested",           <---- add this
                "properties" : {
                    "Title" : {
                        "type" : "string",
                        "fields": {
                            "raw": {
                                "type" : "string",
                                "index" : "not_analyzed",
                            }
                        }
                    }
                }
            }
        }
    }
}

Then, in your documents, SubDocuments.Title will be the field containing the analyzed variation and SubDocuments.Title.raw will be another field containing the exact and non-analyzed string value.

Val
  • 207,596
  • 13
  • 358
  • 360