curl -XDELETE 'http://localhost:9200/test-idx' | |
echo | |
curl -XPUT 'http://localhost:9200/test-idx' -d '{ | |
"settings": { | |
"index.number_of_shards" : 1 | |
}, | |
"mappings" : { | |
"document" : { | |
"properties" : { | |
"tags" : { | |
"type" : "nested", | |
"properties" : { | |
"tag" : { "type" : "string", "index": "not_analyzed" }, | |
"active": {"type": "boolean"} | |
} | |
}, | |
"title" : { "type" : "string" }, | |
"category": {"type": "string", "index": "not_analyzed" } | |
} | |
} | |
} | |
} | |
' | |
echo | |
curl -XPUT 'http://localhost:9200/test-idx/document/1' -d '{ | |
"title": "search engine", | |
"category": "page", | |
"tags": [{ | |
"tag": "AAA", | |
"active": true | |
}, { | |
"tag": "111", | |
"active": false | |
} | |
] | |
}' | |
echo | |
curl -XPUT 'http://localhost:9200/test-idx/document/2' -d '{ | |
"title": "elastic search", | |
"category": "blog", | |
"tags": [{ | |
"tag": "BBB", | |
"active": true | |
}, { | |
"tag": "222", | |
"active": false | |
}] | |
}' | |
echo | |
curl -XPUT 'http://localhost:9200/test-idx/document/3' -d '{ | |
"title": "lucene", | |
"category": "post", | |
"tags": [{ | |
"tag": "CCC", | |
"active": true | |
}, { | |
"tag": "333", | |
"active": false | |
}] | |
}' | |
echo | |
curl -XPOST 'http://localhost:9200/test-idx/_refresh' | |
echo | |
echo "boolean: nested first" | |
curl -XGET 'http://localhost:9200/test-idx/document/_search?pretty=true' -d '{ | |
"query": { | |
"bool": { | |
"must": [{ | |
"nested" : { | |
"_scope": "my_scope", | |
"path": "tags", | |
"query": { | |
"term": { | |
"tags.active": true | |
} | |
} | |
} | |
}, { | |
"text" : { | |
"title": "search" | |
} | |
}] | |
} | |
}, | |
"facets": { | |
"test" : { | |
"terms" : { | |
"field" : "tags.tag" | |
}, | |
"scope": "my_scope" | |
} | |
}, | |
"fields": ["title"] | |
}' | |
echo | |
echo "boolean: nested last" | |
curl -XGET 'http://localhost:9200/test-idx/document/_search?pretty=true' -d '{ | |
"query": { | |
"bool": { | |
"must": [{ | |
"text" : { | |
"title": "search" | |
} | |
}, { | |
"nested" : { | |
"_scope": "my_scope", | |
"path": "tags", | |
"query": { | |
"term": { | |
"tags.active": true | |
} | |
} | |
} | |
}] | |
} | |
}, | |
"facets": { | |
"test" : { | |
"terms" : { | |
"field" : "tags.tag" | |
}, | |
"scope": "my_scope" | |
} | |
}, | |
"fields": ["title"] | |
}' | |
echo | |
echo "boolean: nested in the middle" | |
curl -XGET 'http://localhost:9200/test-idx/document/_search?pretty=true' -d '{ | |
"query": { | |
"bool": { | |
"must": [{ | |
"text" : { | |
"title": "search" | |
} | |
}, { | |
"nested" : { | |
"_scope": "my_scope", | |
"path": "tags", | |
"query": { | |
"term": { | |
"tags.active": true | |
} | |
} | |
} | |
}, { | |
"term" : { | |
"category": "blog" | |
} | |
}] | |
} | |
}, | |
"facets": { | |
"test" : { | |
"terms" : { | |
"field" : "tags.tag" | |
}, | |
"scope": "my_scope" | |
} | |
}, | |
"fields": ["title"] | |
}' | |
echo | |
echo "filtered: nested as filter" | |
curl -XGET 'http://localhost:9200/test-idx/document/_search?pretty=true' -d '{ | |
"query" : { | |
"filtered" : { | |
"query": { | |
"text" : { | |
"title": "search" | |
} | |
}, | |
"filter": { | |
"nested" : { | |
"_scope": "my_scope", | |
"path": "tags", | |
"query": { | |
"term": { | |
"tags.active": true | |
} | |
} | |
} | |
} | |
} | |
}, | |
"facets": { | |
"test" : { | |
"terms" : { | |
"field" : "tags.tag" | |
}, | |
"scope": "my_scope" | |
} | |
}, | |
"fields": ["title"] | |
}' | |
echo | |
echo "filtered: nested as query" | |
curl -XGET 'http://localhost:9200/test-idx/document/_search?pretty=true' -d '{ | |
"query" : { | |
"filtered" : { | |
"query": { | |
"nested" : { | |
"_scope": "my_scope", | |
"path": "tags", | |
"query": { | |
"term": { | |
"tags.active": true | |
} | |
} | |
} | |
}, | |
"filter": { | |
"query": { | |
"term": { | |
"title": "search" | |
} | |
} | |
} | |
} | |
}, | |
"facets": { | |
"test" : { | |
"terms" : { | |
"field" : "tags.tag" | |
}, | |
"scope": "my_scope" | |
} | |
}, | |
"fields": ["title"] | |
}' | |
echo |
{"ok":true,"acknowledged":true} | |
{"ok":true,"acknowledged":true} | |
{"ok":true,"_index":"test-idx","_type":"document","_id":"1","_version":1} | |
{"ok":true,"_index":"test-idx","_type":"document","_id":"2","_version":1} | |
{"ok":true,"_index":"test-idx","_type":"document","_id":"3","_version":1} | |
{"ok":true,"_shards":{"total":2,"successful":1,"failed":0}} | |
boolean: nested first | |
{ | |
"took" : 3, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 1, | |
"successful" : 1, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : 2, | |
"max_score" : 2.1761222, | |
"hits" : [ { | |
"_index" : "test-idx", | |
"_type" : "document", | |
"_id" : "1", | |
"_score" : 2.1761222, | |
"fields" : { | |
"title" : "search engine" | |
} | |
}, { | |
"_index" : "test-idx", | |
"_type" : "document", | |
"_id" : "2", | |
"_score" : 2.1761222, | |
"fields" : { | |
"title" : "elastic search" | |
} | |
} ] | |
}, | |
"facets" : { | |
"test" : { | |
"_type" : "terms", | |
"missing" : 0, | |
"total" : 2, | |
"other" : 0, | |
"terms" : [ { | |
"term" : "BBB", | |
"count" : 1 | |
}, { | |
"term" : "AAA", | |
"count" : 1 | |
} ] | |
} | |
} | |
} | |
boolean: nested last | |
{ | |
"took" : 2, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 1, | |
"successful" : 1, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : 2, | |
"max_score" : 2.1761222, | |
"hits" : [ { | |
"_index" : "test-idx", | |
"_type" : "document", | |
"_id" : "1", | |
"_score" : 2.1761222, | |
"fields" : { | |
"title" : "search engine" | |
} | |
}, { | |
"_index" : "test-idx", | |
"_type" : "document", | |
"_id" : "2", | |
"_score" : 2.1761222, | |
"fields" : { | |
"title" : "elastic search" | |
} | |
} ] | |
}, | |
"facets" : { | |
"test" : { | |
"_type" : "terms", | |
"missing" : 0, | |
"total" : 3, | |
"other" : 0, | |
"terms" : [ { | |
"term" : "CCC", | |
"count" : 1 | |
}, { | |
"term" : "BBB", | |
"count" : 1 | |
}, { | |
"term" : "AAA", | |
"count" : 1 | |
} ] | |
} | |
} | |
} | |
boolean: nested in the middle | |
{ | |
"took" : 1, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 1, | |
"successful" : 1, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : 1, | |
"max_score" : 3.293386, | |
"hits" : [ { | |
"_index" : "test-idx", | |
"_type" : "document", | |
"_id" : "2", | |
"_score" : 3.293386, | |
"fields" : { | |
"title" : "elastic search" | |
} | |
} ] | |
}, | |
"facets" : { | |
"test" : { | |
"_type" : "terms", | |
"missing" : 0, | |
"total" : 2, | |
"other" : 0, | |
"terms" : [ { | |
"term" : "BBB", | |
"count" : 1 | |
}, { | |
"term" : "AAA", | |
"count" : 1 | |
} ] | |
} | |
} | |
} | |
filtered: nested as filter | |
{ | |
"took" : 1, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 1, | |
"successful" : 1, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : 2, | |
"max_score" : 1.3116325, | |
"hits" : [ { | |
"_index" : "test-idx", | |
"_type" : "document", | |
"_id" : "1", | |
"_score" : 1.3116325, | |
"fields" : { | |
"title" : "search engine" | |
} | |
}, { | |
"_index" : "test-idx", | |
"_type" : "document", | |
"_id" : "2", | |
"_score" : 1.3116325, | |
"fields" : { | |
"title" : "elastic search" | |
} | |
} ] | |
}, | |
"facets" : { | |
"test" : { | |
"_type" : "terms", | |
"missing" : 0, | |
"total" : 3, | |
"other" : 0, | |
"terms" : [ { | |
"term" : "CCC", | |
"count" : 1 | |
}, { | |
"term" : "BBB", | |
"count" : 1 | |
}, { | |
"term" : "AAA", | |
"count" : 1 | |
} ] | |
} | |
} | |
} | |
filtered: nested as query | |
{ | |
"took" : 1, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 1, | |
"successful" : 1, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : 2, | |
"max_score" : 1.8109303, | |
"hits" : [ { | |
"_index" : "test-idx", | |
"_type" : "document", | |
"_id" : "1", | |
"_score" : 1.8109303, | |
"fields" : { | |
"title" : "search engine" | |
} | |
}, { | |
"_index" : "test-idx", | |
"_type" : "document", | |
"_id" : "2", | |
"_score" : 1.8109303, | |
"fields" : { | |
"title" : "elastic search" | |
} | |
} ] | |
}, | |
"facets" : { | |
"test" : { | |
"_type" : "terms", | |
"missing" : 0, | |
"total" : 2, | |
"other" : 0, | |
"terms" : [ { | |
"term" : "BBB", | |
"count" : 1 | |
}, { | |
"term" : "AAA", | |
"count" : 1 | |
} ] | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment