Establish if a document satisifies a query (without actually performing the query) in ElasticSearch
<p>I have a document (that I know is in the index), and a query. Is there a way of knowing if the document satisfies the query, without actually querying the index and looking into the results.</p>

<p>So for example, I'd like to know if the document</p>

<pre><code>{ "price" : 30, "productID" : "1937" }
</code></pre>

<p>satisifes the query </p>

<pre><code>{'query': {'bool': {
        'should': [{'bool': {
            'must': [{'term': {'price': 30}}, {'term': {'productID': '1937'}}]}},
            {'term': {'productID': '9947'}}]
    }}}
</code></pre>

<p>which is basically</p>

<pre><code>productID      = 9947
OR (productID = 1937 AND price = 30 )
</code></pre>

<p>This is an easy case, but I need something for arbitrary queries.</p>

<p>Unfortunately, I don't think there's a direct way of doing this. You could either:</p>

<p>A) Issue a query, filtering by the document's ID using the <code>ids</code> query to avoid querying all of your documents:</p>

<pre><code>{
    "ids" : {
        "type" : "my_type",
        "values" : ["1", "4", "100"]
    }
}
</code></pre>

<p>Example from: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-ids-query.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-ids-query.html</a></p>

<p>B) Use ES's percolator to index the <em>query</em>, then query <em>by document</em>:</p>

<p>Index the query:</p>

<pre><code>curl -XPUT 'localhost:9200/my-index/.percolator/1' -d '{
    "query" : {
        "match" : {
            "message" : "bonsai tree"
        }
    }
}'
</code></pre>

<p>Query by document:</p>

<pre><code>curl -XGET 'localhost:9200/my-index/my-type/_percolate' -d '{
    "doc" : {
        "message" : "A new bonsai tree in the office"
    }
}'
</code></pre>

<p>Response:</p>

<pre><code>{
    "took" : 19,
    "_shards" : {
        "total" : 5,
        "successful" : 5,
        "failed" : 0
    },
    "total" : 1,
    "matches" : [ 
        {
          "_index" : "my-index",
          "_id" : "1"
        }
    ]
}
</code></pre>

<p>Examples from: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-percolate.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/reference/current/search-percolate.html</a></p>

<p>If the response has your query's ID, then your query applies to the document. The upside to this approach is that you can run it multiple times for different documents.</p>

python elasticsearch elasticsearch-2.0