Search code examples
elasticsearchelasticsearch-percolate

Find a specific query in Elasticsearch percolator index


I am trying to find a specific query saved in the Percolator Index. Haven't seen any documentation related to it. One of the SOF question helps get all the queries persisted in the index.

One of my sample query persisted in the Percolator

curl -XPUT "http://localhost:9200/notifications/.percolator/1" -d'
{
  "query" : {
      "match" : {
        "tags" : "elasticsearch"
      }}
}'

And other sample query like

curl -XPUT "http://localhost:9200/notifications/.percolator/4" -d'
{
"query" : {
    "bool": {
        "should": [
           { "term": {
              "tags": "Hbase"
                }
           },
            {  "term": {
                 "user": "abc"
              }
           }
        ]
        , "minimum_number_should_match": 1
    }
}
}'

Is there any way I can pull a specific query? Based on the examples above, I would like to find a query which matches to user: "abc"


Solution

  • So with your example if we just updated minimum_should_match to 2 instead of 1, we would be unable to use the percolator API with

    {
        "doc" : {
            "user" : "abc"
        }
    }
    

    to get the query you want back.

    Here's one idea:

    Based on the Percolator Docs, the default mapping for the queries stored in the percolator index is:

    {
        ".percolator" : {
            "properties" : {
                "query" : {
                    "type" : "object",
                    "enabled" : false
                }
            }
        }
    }
    

    You could potentially update this mapping with a new field with an appropriate analyzer that would allow you to search for queries the way you want without providing a doc to find queries that would return the doc. This new field, let's say custom_query_rep, could contain some kind of representation of the query (something like JSON.stringify(query) version of the query) that you specify when you index the query with the percolator. This would allow you to query the percolator index with a query like:

    {
        "query" : {
            "bool" : {
                "must" : [
                    { "match" : { "custom_query_rep" : "user" } },
                    { "match" : { "custom_query_rep" : "abc" } }
                ]
            }
        }
    }
    

    Of course this is a really simple case. The solution is seems somewhat limited and would require a lot more thought for more complex queries.