Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply Mapping to subfield in Elasticsearch

I am unable to create a custom mapping for "hashtags," which is a subfield of "twitter_entities" in elasticsearch. I tried to do it in the following ways:

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

This creates another root field called "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"   
                        }
                    }
                }
            }   
        }
    }
}

and

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

both just create another root field called "hashtags".

I am unable to find any documentation in the elasticsearch api or forums about doing this. Could anyone point me in the right direction?

like image 844
collinwr Avatar asked Sep 13 '13 20:09

collinwr


1 Answers

Have a look at the documentation for mapping, especially the page about the object type. You just have to define twitter_entitiesas an object and declare its fields under properties, same as you did for the root object (twitter_entities). You can omit the type object since any field that contains other fields under properties is detected as object anyway.

{
    "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"   
                                }
                            }
                        }
                    }
                }
            }   
        }
    }
}
like image 132
javanna Avatar answered Oct 17 '22 11:10

javanna