Percolator Query design


(Kiran Patil) #1

Hi,

I have a simple elastic type called as books with the fields as age , year, country, discount. I am trying to use the percolator to build a decission service.
One of the percolator document is the following (which translates to : if age is between 40 & 60 and year >= 1995, and country = DE or US, discount for the book should be 75%)

"type": "books",
"query":{
"bool": {
"must": [
{
"range": {
"age": {
"from": 40,
"to": 60
}
}
},
{
"range": {
"year": {
"gte": 1995
}
}
},
{
"terms": {
"country": [
"de",
"us"
]
}
}
]
}
},
"result":{
"discount" : 75
}

Now when I percolate the following document:

curl -XGET "http://localhost:9200/dummy/books/_percolate" -d'
{
"doc":{
"age": 50,
"country": "de"
}
}'

I dont get the above mentioned percolator entry. I am not passing the year parameter in the percolate document.

Now how do I design a percolator query where in the parameters of a percolate "doc" is only considered and rest are ignored during the percolation. I dont want to use an OR (Should) perocolator query as the in that case even if a "doc" country is not 'de' or 'us', results would be fetched.

Any ideas over here?

Thanks! K


(Ali Beyad) #2

You could try a nested bool query, where you test that either the year field does not exist on the document, or if it does, it is >= 1995. Something like the following:

{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "age": {
              "gte": 40,
              "lte": 60
            }
          }
        },
        {
          "match": {
            "country": "de"
          }
        },
        {
          "bool": {
            "should": [
              {
                "range": {
                  "year": {
                     "gte": 1995
                  }
                }
              },
              {
                "bool": {
                  "must_not": {
                    "exists": {
                      "field": "year"
                    }
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

Does the above work for you?


(system) #3