-
-
Save ivanionut/1cf70179c5827678a8a9e7dae805b2bb to your computer and use it in GitHub Desktop.
[ElasticSearch] Insert 5 records and query for a single record, with facets: Successfully returns a count of 1 in the facets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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<6; 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