Skip to content

Instantly share code, notes, and snippets.

@markbirbeck
Created December 5, 2011 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save markbirbeck/1433393 to your computer and use it in GitHub Desktop.
Save markbirbeck/1433393 to your computer and use it in GitHub Desktop.
[ElasticSearch] Using facets when querying parent/child documents
# Create an index:
#
curl -XDELETE 'http://127.0.0.1:9200/articles'
curl -XPUT 'http://127.0.0.1:9200/articles'
# Insert the action mapping, so that actions are children of articles:
#
curl -XPUT 'http://127.0.0.1:9200/articles/action/_mapping' -d '
{
"action": {
"_parent": {
"type": "article"
}
}
}
'
# Insert some articles:
#
curl -XPUT 'http://127.0.0.1:9200/articles/article/1' -d '{"title": "One"}'
curl -XPUT 'http://127.0.0.1:9200/articles/article/2' -d '{"title": "Two"}'
curl -XPUT 'http://127.0.0.1:9200/articles/article/3' -d '{"title": "Three"}'
# Insert some actions that happened to those articles:
#
curl -XPOST 'http://127.0.0.1:9200/articles/action?parent=1' -d '{"date": "2011-11-18", "created": 1}'
curl -XPOST 'http://127.0.0.1:9200/articles/action?parent=2' -d '{"date": "2011-11-18", "created": 1}'
curl -XPOST 'http://127.0.0.1:9200/articles/action?parent=2' -d '{"date": "2011-11-23", "published": 1}'
curl -XPOST 'http://127.0.0.1:9200/articles/action?parent=3' -d '{"date": "2011-11-23", "created": 1, "published": 1}'
# Ensure the index is up-to-date:
#
curl -XPOST 'http://127.0.0.1:9200/articles/_refresh'
# Now search for all actions that took place between 15th and 30th of November, on an article that has 'One'
# in the title. Create facets for those actions:
#
curl -XPOST 'http://127.0.0.1:9200/articles/action/_search?pretty=true' -d '{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"and": [
{
"range": {
"date": {
"to": "2011-11-30",
"from": "2011-11-15"
}
}
},
{
"has_parent": {
"type": "article",
"query": {
"text": {
"title": "One or Three"
}
}
}
}
]
}
}
},
"facets": {
"published_facet": {
"date_histogram": {
"key_field": "date",
"value_script": "1",
"interval": "day"
},
"facet_filter": {
"term": {
"published": 1
}
}
},
"created_facet": {
"date_histogram": {
"key_field": "date",
"value_script": "1",
"interval": "day"
},
"facet_filter": {
"term": {
"created": 1
}
}
}
}
}'
# This time search for all actions where the article was created and published on the same day:
#
curl -XPOST 'http://127.0.0.1:9200/articles/action/_search?pretty=true' -d '{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"and": [
{
"range": {
"date": {
"to": "2011-11-30",
"from": "2011-11-15"
}
}
},
{
"script": {
"script": "doc[\"created\"].value == 1 && doc[\"published\"].value == 1"
}
},
{
"has_parent": {
"type": "article",
"query": {
"text": {
"title": "One or Three"
}
}
}
}
]
}
}
}
}'
@markbirbeck
Copy link
Author

Just discovered that people are referencing this Gist, so I should say that it does not give the results you'd expect!

@markbirbeck
Copy link
Author

Ok...now it works properly. The key thing is to use has_parent to limit the records over which the facets are calculated. Note also that the value_script construct I was trying to use before (doc['action'] == 'published' ? 1 : 0) doesn't work. The solution to that is to usefacet_filter`.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment