Skip to content

Instantly share code, notes, and snippets.

@geekpete
Last active February 8, 2017 07:13
Show Gist options
  • Save geekpete/f5c00f3427ebe2aa96ad639e92c25bd7 to your computer and use it in GitHub Desktop.
Save geekpete/f5c00f3427ebe2aa96ad639e92c25bd7 to your computer and use it in GitHub Desktop.
# ensure library index is removed if doing repeated testing
DELETE /library
# add the sample data
PUT library/book/1
{
"title": "demian",
"writer" : "Hermann Hesse",
"edition" : 1,
"contents" : "We have been playing with adding new facets to search on the Marketplaces"
}
PUT library/book/2
{
"title": "demian",
"writer" : "Hermann Hesse",
"edition" : 2,
"contents" : "We have been playing with adding new facets to search on the Marketplaces"
}
PUT library/book/3
{
"title": "demian",
"writer" : "Hermann Hesse",
"edition" : 3,
"contents" : "We have been playing with adding new facets to search on the Marketplaces"
}
PUT library/book/4
{
"title": "demian",
"writer" : "Hermann Hesse",
"edition" : 4,
"contents" : "We have been playing with adding new facets to search on the Marketplaces"
}
PUT library/book/5
{
"title": "The Old Man and the Sea",
"writer" : "Ernest Hemingway",
"edition" : 1,
"contents" : "We have been playing with adding new facets to search on the Marketplaces"
}
PUT library/book/6
{
"title": "The Old Man and the Sea",
"writer" : "Ernest Hemingway",
"edition" : 2,
"contents" : "We have been playing with adding new facets to search on the Theater"
}
PUT library/book/7
{
"title": "Anna Karenina",
"writer" : "Leo Tolstoy",
"edition" : 1,
"contents" : "We have been playing with adding new facets to search on the Marketplaces"
}
PUT library/book/8
{
"title": "Crime and Punishment",
"writer" : "Leo Tolstoy",
"edition" : 1,
"contents" : "We have been playing with adding new facets to search on the Marketplaces"
}
PUT library/book/9
{
"title": "Crime and Punishment",
"writer" : "Leo Tolstoy",
"edition" : 2,
"contents" : "We have been playing with adding new facets to search on the Marketplaces"
}
PUT library/book/10
{
"title": "Crime and Punishment",
"writer" : "Leo Tolstoy",
"edition" : 3,
"contents" : "We have been playing with adding new facets to search on the Theater"
}
# We want to filter only docs with the highest edition number for their given title, THEN see which of those match "marketplaces" in the content field.
# So first filter would leave docs with the latest edition (4,6,7,10)
# then the search would only return docs matching marketplace (4,7)
# Can this be done in one query? Guessing not since this is essentially filtering using an aggregation which cannot be done.
# closest thing was a top_hits agg but it incorrectly includes docs that do not have the highest edition number for their given title, these need to be filtered out, which is what the aggregation would do if run before the search.
GET library/_search
{
"query": {
"match": {
"contents": "marketplaces"
}
},
"aggregations": {
"titles": {
"terms": {
"field": "title.keyword"
},
"aggs": {
"latest_editions_matching_marketplaces": {
"top_hits": {
"sort": [
{
"edition": {
"order": "desc"
}
}
],
"_source": {
"includes": [
"title",
"writer",
"edition",
"contents"
]
},
"size": 1
}
}
}
}
},
"size": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment