Last active
January 22, 2018 21:51
-
-
Save bogundersen/e9bac02779e1c4a089dc to your computer and use it in GitHub Desktop.
This file contains 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
# Remove old data | |
curl -XDELETE "http://localhost:9200/test" | |
# Create index with settings | |
curl -XPOST "http://localhost:9200/test/" -d ' | |
{ | |
"settings":{ | |
"index":{ | |
} | |
} | |
} | |
' | |
# Define mapping | |
curl -XPOST "http://localhost:9200/test/item/_mapping" -d ' | |
{ | |
"item":{ | |
"properties":{ | |
"title":{ | |
"type":"string", | |
"index":"analyzed" | |
}, | |
"language":{ | |
"type":"string", | |
"index":"not_analyzed" | |
}, | |
"year":{ | |
"type":"long" | |
}, | |
"author":{ | |
"type":"string", | |
"index":"not_analyzed" | |
} | |
} | |
} | |
} | |
' | |
# Create Document | |
curl -XPOST "http://localhost:9200/_bulk" -d ' | |
{ "index" : { "_index" : "test", "_type" : "item" } } | |
{ "title":"Item 1", "language":"en_GB", year:2013, "author": ["John","Paul"] } | |
{ "index" : { "_index" : "test", "_type" : "item" } } | |
{ "title":"Item 2", "language":"en_GB", year:2012, "author": ["John","George"] } | |
{ "index" : { "_index" : "test", "_type" : "item" } } | |
{ "title":"Item 3", "language":"da_DK", year:2012, "author": ["Ringo"] } | |
' | |
# Wait for ES to be synced (aka refresh indices) | |
curl -XPOST "http://localhost:9200/test/_refresh" | |
# Search 1: All items without limits | |
curl -XPOST "http://localhost:9200/test/item/_search?pretty=true" -d ' | |
{ | |
"query":{ | |
"bool" : { } | |
}, | |
"aggregations" : { | |
"language" : { | |
"terms" : { | |
"field" : "language", | |
"size" : 10 | |
} | |
}, | |
"author" : { | |
"terms" : { | |
"field" : "author", | |
"size" : 10 | |
} | |
}, | |
"year" : { | |
"terms" : { | |
"field" : "year", | |
"size" : 10 | |
} | |
} | |
} | |
} | |
' | |
# Search 2: Limited to year 2012 | |
# Problem: Years aggregation does not show numbers for other years | |
# Result: | |
# year | |
# 2012 : 2 | |
# author | |
# George 1 | |
# John 1 | |
# Ringo 1 | |
# language | |
# da_DK 1 | |
# en_GB 1 | |
curl -XPOST "http://localhost:9200/test/item/_search?pretty=true" -d ' | |
{ | |
"query":{ | |
"bool" : { | |
"must" : { | |
"term" : { | |
"year" : 2012 | |
} | |
} | |
} | |
}, | |
"aggregations" : { | |
"language" : { | |
"terms" : { | |
"field" : "language", | |
"size" : 10 | |
} | |
}, | |
"author" : { | |
"terms" : { | |
"field" : "author", | |
"size" : 10 | |
} | |
}, | |
"year" : { | |
"terms" : { | |
"field" : "year", | |
"size" : 10 | |
} | |
} | |
} | |
} | |
' | |
# Search 3: Post limited to year 2012 | |
# Problem: language and author facets does not show the correct numbers | |
# Result: | |
# year | |
# 2012 : 2 | |
# 2013 : 1 | |
# author | |
# John 2 | |
# George 1 | |
# Paul 1 | |
# Ringo 1 | |
# language | |
# en_GB 2 | |
# da_DK 1 | |
curl -XPOST "http://localhost:9200/test/item/_search?pretty=true" -d ' | |
{ | |
"query":{ | |
"bool" : { } | |
}, | |
"post_filter" : { | |
"bool" : { | |
"must" : [ { | |
"term" : { | |
"year" : 2012 | |
} | |
} ] | |
} | |
}, | |
"aggregations" : { | |
"language" : { | |
"terms" : { | |
"field" : "language", | |
"size" : 10 | |
} | |
}, | |
"author" : { | |
"terms" : { | |
"field" : "author", | |
"size" : 10 | |
} | |
}, | |
"year" : { | |
"terms" : { | |
"field" : "year", | |
"size" : 10 | |
} | |
} | |
} | |
} | |
' | |
# Search 4: Combination of post filters and aggregation filters | |
# Problem: none :) | |
# Result: | |
# year | |
# 2012 : 2 | |
# 2013 : 1 | |
# author | |
# George 1 | |
# John 1 | |
# Ringo 1 | |
# language | |
# en_GB 1 | |
# da_DK 1 | |
curl -XPOST "http://localhost:9200/test/item/_search?pretty=true" -d ' | |
{ | |
"query":{ | |
"bool" : { } | |
}, | |
"post_filter" : { | |
"bool" : { | |
"must" : [ { | |
"term" : { | |
"year" : 2012 | |
} | |
} ] | |
} | |
}, | |
"aggs" : { | |
"language_outher" : { | |
"filter": { | |
"bool" : { | |
"must" : { | |
"term" : { | |
"year" : 2012 | |
} | |
} | |
} | |
}, | |
"aggs" : { | |
"language" : { | |
"terms" : { | |
"field" : "language", | |
"size" : 10 | |
} | |
} | |
} | |
}, | |
"author_outher" : { | |
"filter": { | |
"bool" : { | |
"must" : { | |
"term" : { | |
"year" : 2012 | |
} | |
} | |
} | |
}, | |
"aggs" : { | |
"author" : { | |
"terms" : { | |
"field" : "author", | |
"size" : 10 | |
} | |
} | |
} | |
}, | |
"year" : { | |
"terms" : { | |
"field" : "year", | |
"size" : 10 | |
} | |
} | |
} | |
} | |
' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment