# Percolator Query design

**URL:** <https://discuss.elastic.co/t/percolator-query-design/41209>\
**Category:** Elasticsearch\
**Created:** [February 8, 2016, 5:03pm UTC](https://discuss.elastic.co/t/percolator-query-design/41209 "2016-02-08T17:03:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![KiranbPatil](https://avatars.discourse-cdn.com/v4/letter/k/ee59a6/32.png) [@KiranbPatil](https://discuss.elastic.co/u/KiranbPatil)\
**Post date:** [February 8, 2016, 5:03pm UTC](https://discuss.elastic.co/t/percolator-query-design/41209/1 "2016-02-08T17:03:44Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![abeyad](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@abeyad](https://discuss.elastic.co/u/abeyad)\
**Post date:** [February 9, 2016, 6:37am UTC](https://discuss.elastic.co/t/percolator-query-design/41209/2 "2016-02-09T06:37:29Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 11:18pm UTC](https://discuss.elastic.co/t/percolator-query-design/41209/3 "2017-07-05T23:18:05Z")

</div>

Closed on July 5, 2017, 11:18pm UTC


