Last active
June 12, 2018 09:32
-
-
Save djptek/bc9afe7962170cce18e4abb27ba73b84 to your computer and use it in GitHub Desktop.
Parent Child example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DELETE chat | |
PUT chat | |
{ | |
"settings": { | |
"index": { | |
"number_of_shards": "1", | |
"number_of_replicas": "1" | |
} | |
}, | |
"mappings": { | |
"doc": { | |
"properties": { | |
"user_comment_relation": { | |
"type": "join", | |
"relations": { | |
"user": "comment" | |
} | |
} | |
} | |
} | |
} | |
} | |
GET chat | |
PUT chat/doc/chat_01 | |
{ | |
"user": "foo", | |
"appid": "foos_app", | |
"user_comment_relation": { | |
"name": "user" | |
} | |
} | |
PUT chat/doc/chat_02 | |
{ | |
"user": "bar", | |
"appid": "bars_app", | |
"user_comment_relation": { | |
"name": "user" | |
} | |
} | |
PUT chat/doc/comment_01_01?routing=chat_01 | |
{ | |
"text": "hello", | |
"user_comment_relation": { | |
"name": "comment", | |
"parent": "chat_01" | |
} | |
} | |
PUT chat/doc/comment_01_02?routing=chat_01 | |
{ | |
"text": "world", | |
"user_comment_relation": { | |
"name": "comment", | |
"parent": "chat_01" | |
} | |
} | |
PUT chat/doc/comment_02_01?routing=chat_02 | |
{ | |
"text": "world", | |
"user_comment_relation": { | |
"name": "comment", | |
"parent": "chat_02" | |
} | |
} | |
GET chat/_search | |
{ | |
"query": { | |
"match": { | |
"text": "world" | |
} | |
} | |
} | |
# The only case where the join field makes sense is if your data contains a one-to-many relationship where one entity significantly outnumbers the other entity | |
# https://www.elastic.co/guide/en/elasticsearch/reference/6.2/parent-join.html | |
# https://www.elastic.co/guide/en/elasticsearch/reference/6.2/search-request-inner-hits.html#parent-child-inner-hits | |
POST chat/_search | |
{ | |
"query": { | |
"has_child": { | |
"type": "comment", | |
"query": { | |
"match": { | |
"text": "world" | |
} | |
}, | |
"inner_hits": {} | |
} | |
} | |
} | |
POST chat/_search | |
{ | |
"query": { | |
"bool": { | |
"filter": { | |
"term": { | |
"appid.keyword": "foos_app" | |
} | |
} | |
} | |
} | |
} | |
POST chat/_search | |
{ | |
"query": { | |
"bool": { | |
"filter": { | |
"term": { | |
"appid.keyword": "foos_app" | |
} | |
}, | |
"must": { | |
"has_child": { | |
"type": "comment", | |
"query": { | |
"match": { | |
"text": "world" | |
} | |
}, | |
"inner_hits": {} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment