Skip to content

Instantly share code, notes, and snippets.

@btiernay
Last active December 20, 2015 04:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save btiernay/6073932 to your computer and use it in GitHub Desktop.
Save btiernay/6073932 to your computer and use it in GitHub Desktop.
Example that shows how applying a `match_all` `nested` query will exclude documents that do not have nested documents.
#!/bin/bash
#
# Description:
# Example that shows how applying a `match_all` `nested` query will exclude hits that do not have nested documents.
# This may be related to ElasticSearch's `NestedQueryParser` and it's use of Lucene's `ToParentBlockJoinQuery`
#
# See:
# - https://github.com/elasticsearch/elasticsearch/blob/0.90/src/main/java/org/elasticsearch/index/query/NestedQueryParser.java
# - http://lucene.apache.org/core/4_3_0/join/org/apache/lucene/search/join/package-summary.html
# - http://lucene.apache.org/core/4_3_0/join/org/apache/lucene/search/join/ToParentBlockJoinQuery.html
# Clean
curl -XDELETE http://localhost:9200/nested
# Create index
curl -XPOST http://localhost:9200/nested
# Create nested mapping
curl -XPOST http://localhost:9200/nested/type/_mapping -d '
{
"type":{
"properties":{
"list":{
"type":"nested",
"properties":{}
}
}
}
}'
# Insert document with a nested doc
curl -XPOST http://localhost:9200/nested/type/ -d '
{
"list":[
{
x : 1
}
]
}'
# Insert document without a nested doc
curl -XPOST http://localhost:9200/nested/type/ -d '{}'
# Assert that there are 2 documents
curl http://localhost:9200/nested/type/_count
# Only returns 1 hit, but expected 2
curl -XGET http://localhost:9200/nested/type/_search -d '
{
"size":0,
"fields":[],
"query":{
"nested":{
"query":{
"match_all":{}
},
"path":"list"
}
}
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment