Skip to content

Instantly share code, notes, and snippets.

@gr0
Created September 3, 2013 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gr0/6422143 to your computer and use it in GitHub Desktop.
Save gr0/6422143 to your computer and use it in GitHub Desktop.
Simple usage of AND and OR filters combined in ElasticSearch.
curl -XPOST 'localhost:9200/docs/doc/1' -d '{"name":"test 1","age":10, "year": 1998}'
curl -XPOST 'localhost:9200/docs/doc/2' -d '{"name":"test 2","age":30, "year": 2012}'
curl -XPOST 'localhost:9200/docs/doc/3' -d '{"name":"test 3","age":40, "year": 1998}'
curl -XPOST 'localhost:9200/docs/doc/4' -d '{"name":"test 4","age":40, "year": 2013}'
curl -XGET 'localhost:9200/_search?pretty' -d '{
"query" : {
"match_all" : {}
},
"filter" : {
"and" : [
{
"or" : [
{
"term" : {"age" : 30}
},
{
"term" : {"age" : 40}
}
]
},
{
"or" : [
{
"term" : {"year" : 2012}
},
{
"term" : {"year" : 2013}
}
]
}
]
}
}'
{
"took" : 55,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 1.0,
"hits" : [ {
"_index" : "docs",
"_type" : "doc",
"_id" : "4",
"_score" : 1.0, "_source" : {"name":"test 4","age":40, "year": 2013}
}, {
"_index" : "docs",
"_type" : "doc",
"_id" : "2",
"_score" : 1.0, "_source" : {"name":"test 2","age":30, "year": 2012}
} ]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment