Skip to content

Instantly share code, notes, and snippets.

@Filirom1
Last active June 9, 2017 10:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Filirom1/a7e17cc5d6eb78458ec5 to your computer and use it in GitHub Desktop.
Save Filirom1/a7e17cc5d6eb78458ec5 to your computer and use it in GitHub Desktop.
ElasticSearch dynamic default mapping for nested objects

First start ES with /etc/elasticsearch/default-mapping.json, then index several documents with inner objects

curl -XPOST -d '{"key":{"subkey1":1, "subkey2":1}}' localhost:9200/toto/toto/1
curl -XPOST -d '{"key2":{"subkey1":1, "subkey2":1}}' localhost:9200/toto/toto/2
curl -XPOST -d '{"key3":{"subkey1":1, "subkey2":1}}' localhost:9200/toto/toto/3

Finally, launch the query

$ curl -XPOST -d @query.json localhost:9200/toto/_search
{"took":7,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":1.0,"hits":[{"_index":"toto","_type":"toto","_id":"2","_score":1.0,"_source":{"key2":{"subkey1":1, "subkey2":1}}}]}}
{
"_default_" : {
"dynamic_templates": [
{
"objects": {
"match": "*",
"match_mapping_type" : "object",
"mapping": {
"type": "nested",
"include_in_parent": true
}
}
}
]
}
}
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"nested": {
"path": "key2",
"filter": {
"bool": {
"must": [
{
"term": {
"key2.subkey1": 1
}
},
{
"term": {
"key2.subkey2": 1
}
}
]
}
}
}
}
}
}
}
@abhishek5678
Copy link

But if there is multiple nested objects then in this case how we can put the mapping file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment