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>If you know the document's ID, you can use the "explain" api. 
<a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-explain.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/reference/current/search-explain.html</a></p>

<p>So assuming your documents id is <code>1234abc</code> you can do the following</p>

<pre><code>curl -XGET 'your_es_server:9200/your_index/your_mapping/1234abc/_explain' -d '{'query': {'bool': {
    'should': [{'bool': {
        'must': [{'term': {'price': 30}}, {'term': {'productID': '1937'}}]}},
        {'term': {'productID': '9947'}}]
}}}'
</code></pre>

<p>And this should return something like this. </p>

<pre><code>    {
  "_index": "your_index",
  "_type": "your_mapping",
  "_id": "1234abc",
  "matched": true,
  "explanation": {
    "value": 0.06100826,
    "description": "sum of:",
    "details": [.........
....
...
...
</code></pre>

<p>You can just check if the <code>matched</code> variable in the above response is true or not.</p>

python elasticsearch elasticsearch-2.0