Search code examples
typeselasticsearchrestrict

Restrict percolator call to a type in ElasticSearch


As explained in the doc, when I store a query with _percolator, eg:

curl -XPUT localhost:9200/_percolator/test/kuku -d '{
  "color" : "blue",
  "query" : {
      "term" : {
        "field1" : "value1"
      }
   }
}'

This query will be run when I percolate a document on the 'test' index, but if I want to restrict it to the type 'foo' of the 'test' index the only solution is to add a type in the query :

curl -XPUT localhost:9200/_percolator/test/kuku -d '{
   "type":"foo",
   "color" : "blue",
   "query" : {
       "term" : {
         "field1" : "value1"
       }
  }
}'

And when adding the document use

curl -XGET localhost:9200/test/type1/_percolate -d '{
   "doc" : {
      "field1" : "value1"
    },
    "query" : {
      "term" : {
         "type" : "foo"
      }
    } 
  }'

Is there another solution ? I tried

curl -XPUT localhost:9200/_percolator/test/foo/kuku

but it does not work.


Solution

  • An alternative approach is to wrap your original query into a filtered query and add a terms filter for _type:

    {
        "query": {
            "filtered":{
                "query": {
                    "term":{
                        field1" : "value1"
                    }
                 },
                 "filter": {
                     "term": {
                         "_type" : "type1"
                     }
                  }
              }
         }
    }