Skip to content

Instantly share code, notes, and snippets.

@d2kagw
Last active August 29, 2015 14:08
Show Gist options
  • Save d2kagw/1c9d4ef486b7a2450d95 to your computer and use it in GitHub Desktop.
Save d2kagw/1c9d4ef486b7a2450d95 to your computer and use it in GitHub Desktop.
Example of Nesting Filters and them using OR vs. AND
#!/bin/sh
echo "--- delete index"
curl -X DELETE 'http://localhost:9200/nesting-test'
echo "\n\n --- create index and put mapping into place"
curl -X POST 'http://localhost:9200/nesting-test/' -d '{
"mappings" : {
"mention" : {
"properties" : {
"props": {
"type": "nested",
"include_in_parent": false,
"properties": {
"client_id": {"type" : "integer"},
"name": {"type" : "string"}
}
}
}
}
},
"settings" : {
"number_of_shards" : 1,
"number_of_replicas" : 0
}
}'
echo "\n\n --- index data"
curl -X PUT 'http://localhost:9200/nesting-test/mention/1' -d '
{
"props" : [
{ "client_id": 1, "name": "carlover" }
]
}'
curl -X PUT 'http://localhost:9200/nesting-test/mention/2' -d '
{
"props" : [
{ "client_id": null, "name": "petlover" },
{ "client_id": 2, "name": "premiumshopper" }
]
}'
curl -X PUT 'http://localhost:9200/nesting-test/mention/3' -d '
{
"props" : [
{ "client_id": 1, "name": "carlover" },
{ "client_id": null, "name": "petlover" }
]
}'
curl -X PUT 'http://localhost:9200/nesting-test/mention/4' -d '
{
"props" : [
{ "client_id": 2, "name": "premiumshopper" }
]
}'
echo "\n\n --- optimize"
curl -X POST 'http://localhost:9200/_optimize'
echo "\n\n --- query - 1"
echo " --- should (and does) return zero results"
curl -X GET 'http://localhost:9200/nesting-test/_search?pretty=true' -d '
{
"query": {
"nested": {
"path": "props",
"filter": {
"and": [
{ "query": { "query_string": { "query": "props.name:((carlover AND premiumshopper) NOT petlover)" } } },
{ "or": [
{ "query": { "match": { "props.client_id": 1 } } },
{ "missing": { "field": "props.client_id" } }
]}
]
}
}
}
}'
# OUTPUT
# {
# "took": 1,
# "timed_out": false,
# "_shards": {
# "total": 1,
# "successful": 1,
# "failed": 0
# },
# "hits": {
# "total": 0,
# "max_score": null,
# "hits": []
# }
# }
echo "\n\n --- query - 2"
echo " --- should (but doesn't) return just mention 1 (it returns 1 & 3)"
curl -X GET 'http://localhost:9200/nesting-test/_search?pretty=true' -d '
{
"query": {
"nested": {
"path": "props",
"filter": {
"and": [
{ "query": { "query_string": { "query": "props.name:(carlover NOT petlover)" } } },
{ "or": [
{ "query": { "match": { "props.client_id": 1 } } },
{ "missing": { "field": "props.client_id" } }
]}
]
}
}
}
}'
# OUTPUT
# {
# "took": 1,
# "timed_out": false,
# "_shards": {
# "total": 1,
# "successful": 1,
# "failed": 0
# },
# "hits": {
# "total": 2,
# "max_score": 1,
# "hits": [
# {
# "_index": "nesting-test",
# "_type": "mention",
# "_id": "1",
# "_score": 1,
# "_source": {
# "props": [
# {
# "client_id": 1,
# "name": "carlover"
# }
# ]
# }
# },
# {
# "_index": "nesting-test",
# "_type": "mention",
# "_id": "3",
# "_score": 1,
# "_source": {
# "props": [
# {
# "client_id": 1,
# "name": "carlover"
# },
# {
# "client_id": null,
# "name": "petlover"
# }
# ]
# }
# }
# ]
# }
# }
echo "\n\n --- query - 3"
echo " --- should (but doesn't) return just mention 2 (it returns 2 & 3)"
curl -X GET 'http://localhost:9200/nesting-test/_search?pretty=true' -d '
{
"query": {
"nested": {
"path": "props",
"filter": {
"and": [
{ "query": { "query_string": { "query": "props.name:(* NOT carlover)" } } },
{ "or": [
{ "query": { "match": { "props.client_id": 1 } } },
{ "missing": { "field": "props.client_id" } }
]}
]
}
}
}
}'
# OUTPUT
# {
# "took": 1,
# "timed_out": false,
# "_shards": {
# "total": 1,
# "successful": 1,
# "failed": 0
# },
# "hits": {
# "total": 2,
# "max_score": 1,
# "hits": [
# {
# "_index": "nesting-test",
# "_type": "mention",
# "_id": "2",
# "_score": 1,
# "_source": {
# "props": [
# {
# "client_id": null,
# "name": "petlover"
# },
# {
# "client_id": 2,
# "name": "premiumshopper"
# }
# ]
# }
# },
# {
# "_index": "nesting-test",
# "_type": "mention",
# "_id": "3",
# "_score": 1,
# "_source": {
# "props": [
# {
# "client_id": 1,
# "name": "carlover"
# },
# {
# "client_id": null,
# "name": "petlover"
# }
# ]
# }
# }
# ]
# }
# }
echo "\n\n --- done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment