Skip to content

Instantly share code, notes, and snippets.

@bogundersen
Last active January 22, 2018 21:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bogundersen/e9bac02779e1c4a089dc to your computer and use it in GitHub Desktop.
Save bogundersen/e9bac02779e1c4a089dc to your computer and use it in GitHub Desktop.
# 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