将映射应用于Elasticsearch中的子字段

时间:2013-09-13 20:05:56

标签: mapping elasticsearch

我无法为“hashtags”创建自定义映射,“hashtags”是elasticsearch中“twitter_entities”的子字段。我尝试通过以下方式实现:

 {
    "mappings": {
        "tweet" : {
            "properties": {
                "twitter_entities.hashtags" : {
                    "type" : "multi_field",
                    "fields" : {
                        "hashtag" : {
                            "type" : "string",
                            "analyzer" : "hashtag"
                        },                        
                        "autocomplete" : {
                            "type" : "string",
                            "index_analyzer" : "hashtag_autocomplete",
                            "search_analyzer" : "hashtag"   
                        }
                    }
                }
            }   
        }
    }
}

这会创建另一个名为“twitter_entities.hashtags”的根域

 {
    "mappings": {
        "tweet" : {
            "properties": {
                "hashtags" : {
                    "type" : "multi_field",
                    "fields" : {
                        "hashtag" : {
                            "type" : "string",
                            "analyzer" : "hashtag"
                        },                        
                        "autocomplete" : {
                            "type" : "string",
                            "index_analyzer" : "hashtag_autocomplete",
                            "search_analyzer" : "hashtag"   
                        }
                    }
                }
            }   
        }
    }
}

{
    "mappings": {
        "tweet" : {
            "properties": {
                "_parent" : {"type" : "twitter_entities" },
                "hashtags" : {
                    "type" : "multi_field",
                    "fields" : {
                        "hashtag" : {
                            "type" : "string",
                            "analyzer" : "hashtag"
                        },                        
                        "autocomplete" : {
                            "type" : "string",
                            "index_analyzer" : "hashtag_autocomplete",
                            "search_analyzer" : "hashtag"   
                        }
                    }
                }
            }   
        }
    }
}

两者都只创建另一个名为“hashtags”的根域。

我无法在elasticsearch api或论坛中找到有关此操作的任何文档。有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:10)

查看mapping的文档,尤其是有关object type的页面。 您只需将twitter_entities定义为对象,并在properties下声明其字段,与根对象(twitter_entities)相同。您可以省略类型object,因为包含properties下的其他字段的任何字段都会被检测为对象。

{
    "mappings": {
        "tweet" : {
            "properties": {
                "twitter_entities" : {
                    "type": "object",
                    "properties" : {
                        "hashtag" : {
                            "type" : "multi_field",
                            "fields" : {
                                "hashtag" : {
                                "type" : "string",
                                "analyzer" : "hashtag"
                                },                        
                                "autocomplete" : {
                                    "type" : "string",
                                    "index_analyzer" : "hashtag_autocomplete",
                                    "search_analyzer" : "hashtag"   
                                }
                            }
                        }
                    }
                }
            }   
        }
    }
}