Last active
June 16, 2020 22:07
-
-
Save FrankHassanabad/0d45132bf45c957c4072f1a12992e30c to your computer and use it in GitHub Desktop.
Tests around Nesting and KQL with references
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
| # | |
| # Create two mappings, one with nesting and one without: | |
| # | |
| # Optinally clean up any old index data | |
| DELETE /delme-test-categories | |
| DELETE /delme-test-categories2 | |
| # Without nesting | |
| PUT /delme-test-categories | |
| { | |
| "mappings": { | |
| "properties": { | |
| "category": { | |
| "properties": { | |
| "name": { | |
| "type": "keyword" | |
| }, | |
| "trusted": { | |
| "type": "boolean" | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| # With nesting | |
| PUT /delme-test-categories2 | |
| { | |
| "mappings": { | |
| "properties": { | |
| "category": { | |
| "type": "nested", | |
| "properties": { | |
| "name": { | |
| "type": "keyword" | |
| }, | |
| "trusted": { | |
| "type": "boolean" | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| # Add data to both as arrays (but this works also with single values) | |
| POST /delme-test-categories/_doc | |
| { | |
| "category" : [ | |
| { | |
| "name" : "Elastic", | |
| "trusted" : true | |
| }, | |
| { | |
| "name" : "Frank", | |
| "trusted" : false | |
| } | |
| ] | |
| } | |
| POST /delme-test-categories2/_doc | |
| { | |
| "category" : [ | |
| { | |
| "name" : "Elastic", | |
| "trusted" : true | |
| }, | |
| { | |
| "name" : "Frank", | |
| "trusted" : false | |
| } | |
| ] | |
| } | |
| # Query with false positive against the non nested. This returns a record when it should not | |
| POST /delme-test-categories/_search | |
| { | |
| "query": { | |
| "bool": { | |
| "must": [ | |
| { | |
| "match": { | |
| "category.name": "Frank" | |
| } | |
| }, | |
| { | |
| "match": { | |
| "category.trusted": true | |
| } | |
| } | |
| ] | |
| } | |
| } | |
| } | |
| # Query against nested ... Notice "path": "category" below. This does not return anything which is correct | |
| POST /delme-test-categories2/_search | |
| { | |
| "query": { | |
| "nested": { | |
| "path": "category", | |
| "query": { | |
| "bool": { | |
| "must": [ | |
| { | |
| "match": { | |
| "category.name": "Frank" | |
| } | |
| }, | |
| { | |
| "match": { | |
| "category.trusted": true | |
| } | |
| } | |
| ] | |
| } | |
| } | |
| } | |
| } | |
| } | |
| # Query against nested ... Notice "path": "category" below. This returns the correct record which is Elastic _and_ trusted: true | |
| POST /delme-test-categories2/_search | |
| { | |
| "query": { | |
| "nested": { | |
| "path": "category", | |
| "query": { | |
| "bool": { | |
| "must": [ | |
| { | |
| "match": { | |
| "category.name": "Elastic" | |
| } | |
| }, | |
| { | |
| "match": { | |
| "category.trusted": true | |
| } | |
| } | |
| ] | |
| } | |
| } | |
| } | |
| } | |
| } | |
| # KQL would be: 'category:{ name:Frank and trusted:true }' And the output result to run would be: | |
| POST /delme-test-categories2/_search | |
| { | |
| "query": { | |
| "bool": { | |
| "must": [], | |
| "filter": [ | |
| { | |
| "nested": { | |
| "path": "category", | |
| "query": { | |
| "bool": { | |
| "filter": [ | |
| { | |
| "bool": { | |
| "should": [ | |
| { | |
| "match": { | |
| "category.name": "Frank" | |
| } | |
| } | |
| ], | |
| "minimum_should_match": 1 | |
| } | |
| }, | |
| { | |
| "bool": { | |
| "should": [ | |
| { | |
| "match": { | |
| "category.trusted": true | |
| } | |
| } | |
| ], | |
| "minimum_should_match": 1 | |
| } | |
| } | |
| ] | |
| } | |
| }, | |
| "score_mode": "none" | |
| } | |
| } | |
| ], | |
| "should": [], | |
| "must_not": [] | |
| } | |
| } | |
| } | |
| # Refs: | |
| # - https://discuss.elastic.co/t/query-multi-value-field-to-match-all-values-passed-in/54483 | |
| # - https://www.elastic.co/guide/en/kibana/current/kuery-query.html | |
| # - https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html | |
| # - https://github.com/elastic/kibana/pull/47070 | |
| # - https://github.com/elastic/kibana/issues/44554 | |
| # - https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-mapping.html | |
| # | |
| # EOF | |
| # |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment