i have ES version 2.1.1 i tried the below sample query as per the link https://www.elastic.co/guide/en/elasticsearch/guide/current/combining-filters.html
but i did not get any results, could anybody explain why, please
GET /my_store/products/_search
{
"query" : {
"constant_score" : {
"filter" : {
"bool" : {
"should" : [
{ "term" : {"productID" : "KDKE-B-9947-#kL5"}},
{ "bool" : {
"must" : [
{ "term" : {"productID" : "JODL-X-1937-#pV7"}},
{ "term" : {"price" : 30}}
]
}}
]
}
}
}
}
}
Hi @basharz,
just double checking: You stored these documents in Elasticsearch before issuing the query, didn't you?
If you look at the previous page in the docs, it shows you how to do it. Here's a complete example copy / pasted from the docs:
DELETE /my_store
PUT /my_store
{
"mappings" : {
"products" : {
"properties" : {
"productID" : {
"type" : "string",
"index" : "not_analyzed"
}
}
}
}
}
POST /my_store/products/_bulk
{ "index": { "_id": 1 }}
{ "price" : 10, "productID" : "XHDK-A-1293-#fJ3" }
{ "index": { "_id": 2 }}
{ "price" : 20, "productID" : "KDKE-B-9947-#kL5" }
{ "index": { "_id": 3 }}
{ "price" : 30, "productID" : "JODL-X-1937-#pV7" }
{ "index": { "_id": 4 }}
{ "price" : 30, "productID" : "QQPX-R-3956-#aD8" }
GET /my_store/products/_search
{
"query": {
"constant_score": {
"filter": {
"bool": {
"should": [
{
"term": {
"productID": "KDKE-B-9947-#kL5"
}
},
{
"bool": {
"must": [
{
"term": {
"productID": "JODL-X-1937-#pV7"
}
},
{
"term": {
"price": 30
}
}
]
}
}
]
}
}
}
}
}
Then the query returns two hits:
{
"took": 109,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "my_store",
"_type": "products",
"_id": "2",
"_score": 1,
"_source": {
"price": 20,
"productID": "KDKE-B-9947-#kL5"
}
},
{
"_index": "my_store",
"_type": "products",
"_id": "3",
"_score": 1,
"_source": {
"price": 30,
"productID": "JODL-X-1937-#pV7"
}
}
]
}
}
Daniel
thanks for your replay, actually i already inserted the sample json files to the index and when i try the normal search query it gives me all files
GET /my_store/products/_search{} , it seems the syntax has been changed
Hi @basharz,
the query syntax changed from 1.x to 2 but AFAIK there were no changes from 2.0 to 2.x in the query syntax. Even if there were, the example that I copy / pasted should also work for you. Can you please try the complete example that I pasted above?
Daniel