Skip to content

Instantly share code, notes, and snippets.

@AnSavvides
Forked from markbirbeck/gist:1684506
Created November 30, 2012 17:02
Show Gist options
  • Save AnSavvides/4177014 to your computer and use it in GitHub Desktop.
Save AnSavvides/4177014 to your computer and use it in GitHub Desktop.
[ElasticSearch] Insert 6 records and query for a single record with facets: Failure when the number of records is greater than the shard size
# 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, as a child of articles:
#
curl -XPUT 'http://127.0.0.1:9200/articles/article/_mapping' -d '
{
"article": {
"properties": {
"title": {
"store": true,
"type": "string"
},
"actions": {
"type": "nested",
"properties": {
"action": {
"store": true,
"type": "string"
},
"updated": {
"store": true,
"type": "integer"
},
"date": {
"store": true,
"format": "dateOptionalTime",
"type": "date"
}
}
}
}
}
}
'
# Insert some articles:
#
for ((i=1; i<7; i++))
do
curl -XPUT 'http://127.0.0.1:9200/articles/article/'$i -d '
{
"title": "Article '$i'",
"actions": [
{"date": "2010-11-18", "updated": 1, "action": "updated"}
]
}
'
done
# Ensure the index is up-to-date:
#
curl -XPOST 'http://127.0.0.1:9200/articles/_refresh'
# Now search for some articles that have actions that took place
# on the 18th of November, 2010. Create facets for those actions:
#
curl -XGET 'http://127.0.0.1:9200/articles/_search?pretty=true' -d '
{
"query": {
"bool": {
"must": [
{ "query_string": {"query": "title:\"Article 1\""} },
{
"nested": {
"path": "actions",
"score_mode": "avg",
"_scope": "actions",
"query": {
"bool": {
"must": [
{
"range": {
"actions.date": {
"from": "2010-11-18",
"to": "2010-11-18"
}
}
}
]
}
}
}
}
]
}
},
"facets": {
"updated_facet": {
"date_histogram": {
"interval": "day",
"key_field": "actions.date",
"value_field": "actions.updated"
},
"scope": "actions"
}
},
"from": 0,
"size": 0
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment