在数组项上指定多字段 - elasticsearch

时间:2015-06-08 18:35:12

标签: elasticsearch

如何指定以数组内部属性为目标的多字段索引映射?

例如

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

如何指定一个索引映射,该索引映射将SubDocuments中的Title作为触摸和未触摸的变体?

由于

1 个答案:

答案 0 :(得分:0)

Elasticsearch支持

Arrays,它们不需要特定的映射。您只需将SubDocuments字段映射为object(或nested对象),然后将Title字段映射为该对象中的多字段。

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

值得注意的是,如果您想要将其他字段添加到SubDocuments并且您需要查询这些SubDocuments字段,那么将SubDocuments映射为{{3}可能更明智一些像这样:

{
    "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",
                            }
                        }
                    }
                }
            }
        }
    }
}

然后,在您的文档中,SubDocuments.Title将是包含已分析变体的字段,而SubDocuments.Title.raw将是包含精确和未分析字符串值的另一个字段。