-
-
Save geekpete/5d0bc29fa0cfb0dbc7d1 to your computer and use it in GitHub Desktop.
This file contains 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
curl -XDELETE localhost:9200/nested_aggs | |
curl -XPUT localhost:9200/nested_aggs | |
curl -XPUT localhost:9200/nested_aggs/user/_mapping -d ' | |
{ | |
"_id" : {"index": "not_analyzed", "path" : "userId"}, | |
"properties": { | |
"userId": {"type": "string", "index": "not_analyzed"}, | |
"groups": { | |
"type": "nested", | |
"properties": { | |
"groupId": {"type": "string", "index": "not_analyzed"}, | |
"groupRole": {"type": "string", "index": "not_analyzed"}, | |
"groupAdmin": {"type": "string", "index": "not_analyzed"} | |
} | |
} | |
} | |
} | |
' | |
curl -XPUT localhost:9200/nested_aggs/user/User1 -d ' | |
{ | |
"userId": "User1", | |
"groups": [ | |
{ | |
"groupId": "Group1", | |
"groupRole": "Role1", | |
"groupAdmin": "User2" | |
}, | |
{ | |
"groupId": "Group2", | |
"groupRole": "Role1", | |
"groupAdmin": "User2" | |
}, | |
{ | |
"groupId": "Group3", | |
"groupRole": "Role1", | |
"groupAdmin": "User3" | |
}, | |
{ | |
"groupId": "Group4", | |
"groupRole": "Role2", | |
"groupAdmin": "User3" | |
} | |
] | |
} | |
' | |
curl localhost:9200/nested_aggs/_search -d ' | |
{ | |
"size": 0, | |
"query": { | |
"filtered": { | |
"query": { | |
"match_all": {} | |
}, | |
"filter": { | |
"nested": { | |
"path": "groups", | |
"query": { | |
"filtered": { | |
"query": { | |
"match_all": {} | |
}, | |
"filter": { | |
"bool": { | |
"must": [ | |
{ | |
"term": { | |
"groups.groupAdmin": "User2" | |
} | |
} | |
] | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
}, | |
"aggs": { | |
"nested_groups": { | |
"nested": { | |
"path": "groups" | |
}, | |
"aggs": { | |
"admin_groups": { | |
"filter": { | |
"bool": { | |
"must": [ | |
{ | |
"term": { | |
"groups.groupAdmin": "User2" | |
} | |
} | |
] | |
} | |
}, | |
"aggs": { | |
"groupRoles": { | |
"terms": { | |
"field": "groupRole" | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
' |
This file contains 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
// counting at nested level, not at parent... | |
{ | |
"took" : 2, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 5, | |
"successful" : 5, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : 1, | |
"max_score" : 0.0, | |
"hits" : [ ] | |
}, | |
"aggregations" : { | |
"nested_groups" : { | |
"doc_count" : 4, | |
"admin_groups" : { | |
"doc_count" : 2, | |
"groupRoles" : { | |
"buckets" : [ { | |
"key" : "Role1", | |
"doc_count" : 2 | |
} ] | |
} | |
} | |
} | |
} | |
} |
This file contains 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
curl -XDELETE localhost:9200/nested_include | |
curl -XPUT localhost:9200/nested_include | |
curl -XPUT localhost:9200/nested_include/user/_mapping -d ' | |
{ | |
"_id" : {"index": "not_analyzed", "path" : "userId"}, | |
"properties": { | |
"userId": {"type": "string", "index": "not_analyzed"}, | |
"groups": { | |
"type": "nested", | |
"include_in_parent": true, | |
"properties": { | |
"groupId": {"type": "string", "index": "not_analyzed"}, | |
"groupRole": {"type": "string", "index": "not_analyzed"}, | |
"groupAdmin": {"type": "string", "index": "not_analyzed"} | |
} | |
} | |
} | |
} | |
' | |
curl -XPUT localhost:9200/nested_include/user/User1 -d ' | |
{ | |
"userId": "User1", | |
"groups": [ | |
{ | |
"groupId": "Group1", | |
"groupRole": "Role1", | |
"groupAdmin": "User2" | |
}, | |
{ | |
"groupId": "Group2", | |
"groupRole": "Role1", | |
"groupAdmin": "User2" | |
}, | |
{ | |
"groupId": "Group3", | |
"groupRole": "Role1", | |
"groupAdmin": "User3" | |
}, | |
{ | |
"groupId": "Group4", | |
"groupRole": "Role2", | |
"groupAdmin": "User3" | |
} | |
] | |
} | |
' | |
curl localhost:9200/nested_include/_search -d ' | |
{ | |
"size": 0, | |
"query": { | |
"filtered": { | |
"query": { | |
"match_all": {} | |
}, | |
"filter": { | |
"nested": { | |
"path": "groups", | |
"query": { | |
"filtered": { | |
"query": { | |
"match_all": {} | |
}, | |
"filter": { | |
"bool": { | |
"must": [ | |
{ | |
"term": { | |
"groups.groupAdmin": "User2" | |
} | |
} | |
] | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
}, | |
"aggs": { | |
"roles": { | |
"terms": { | |
"field": "groups.groupRole" | |
} | |
} | |
} | |
} | |
' |
This file contains 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
curl -XDELETE localhost:9200/nested_include_key | |
curl -XPUT localhost:9200/nested_include_key | |
curl -XPUT localhost:9200/nested_include_key/user/_mapping -d ' | |
{ | |
"_id" : {"index": "not_analyzed", "path" : "userId"}, | |
"properties": { | |
"userId": {"type": "string", "index": "not_analyzed"}, | |
"groups": { | |
"type": "nested", | |
"include_in_parent": true, | |
"properties": { | |
"groupId": {"type": "string", "index": "not_analyzed"}, | |
"groupRole": {"type": "string", "index": "not_analyzed"}, | |
"groupAdmin": {"type": "string", "index": "not_analyzed"}, | |
"roleAdminKey": {"type": "string", "index": "not_analyzed"} | |
} | |
} | |
} | |
} | |
' | |
curl -XPUT localhost:9200/nested_include_key/user/User1 -d ' | |
{ | |
"userId": "User1", | |
"groups": [ | |
{ | |
"groupId": "Group1", | |
"groupRole": "Role1", | |
"groupAdmin": "User2", | |
"roleAdminKey": "Role1-User2" | |
}, | |
{ | |
"groupId": "Group2", | |
"groupRole": "Role1", | |
"groupAdmin": "User2", | |
"roleAdminKey": "Role1-User2" | |
}, | |
{ | |
"groupId": "Group3", | |
"groupRole": "Role1", | |
"groupAdmin": "User3", | |
"roleAdminKey": "Role1-User3" | |
}, | |
{ | |
"groupId": "Group4", | |
"groupRole": "Role2", | |
"groupAdmin": "User3", | |
"roleAdminKey": "Role2-User3" | |
} | |
] | |
} | |
' | |
curl localhost:9200/nested_include_key/_search -d ' | |
{ | |
"size": 0, | |
"query": { | |
"filtered": { | |
"query": { | |
"match_all": {} | |
}, | |
"filter": { | |
"nested": { | |
"path": "groups", | |
"query": { | |
"filtered": { | |
"query": { | |
"match_all": {} | |
}, | |
"filter": { | |
"bool": { | |
"must": [ | |
{ | |
"term": { | |
"groups.groupAdmin": "User2" | |
} | |
} | |
] | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
}, | |
"aggs": { | |
"roles": { | |
"terms": { | |
"field": "groups.roleAdminKey", | |
"include": ".*User2.*" | |
} | |
} | |
} | |
} | |
' |
This file contains 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
// workaround, add "*Key" fields to docs and aggregate on that at parent level. | |
// Apply filter using regular expression in "include" parameter instead. | |
{ | |
"took" : 3, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 5, | |
"successful" : 5, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : 1, | |
"max_score" : 0.0, | |
"hits" : [ ] | |
}, | |
"aggregations" : { | |
"roles" : { | |
"buckets" : [ { | |
"key" : "Role1-User2", | |
"doc_count" : 1 | |
} ] | |
} | |
} | |
} |
This file contains 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
// counts at parent level, but can't apply nested filter | |
{ | |
"took" : 3, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 5, | |
"successful" : 5, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : 1, | |
"max_score" : 0.0, | |
"hits" : [ ] | |
}, | |
"aggregations" : { | |
"roles" : { | |
"buckets" : [ { | |
"key" : "Role1", | |
"doc_count" : 1 | |
}, { | |
"key" : "Role2", | |
"doc_count" : 1 | |
} ] | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment