# How to match non existant tags

**URL:** <https://discuss.elastic.co/t/how-to-match-non-existant-tags/9574>\
**Category:** Elasticsearch\
**Created:** [November 6, 2012, 6:39am UTC](https://discuss.elastic.co/t/how-to-match-non-existant-tags/9574 "2012-11-06T06:39:37Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![azitabh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/azitabh/32/2647_2.png) [@azitabh](https://discuss.elastic.co/u/azitabh)\
**Post date:** [November 6, 2012, 6:39am UTC](https://discuss.elastic.co/t/how-to-match-non-existant-tags/9574/1 "2012-11-06T06:39:37Z")

</div>

Lets say I have a set of document which have a-z as keys.  
And I am firing this term query:  
curl -X GET localhost:9200/memphis/pincode/\_search?pretty=json -d  
'{"query":{"term":{"a":"abcd", "b":"wxyz"}}}'

This will return me all the documents for which the value of 'a' is abcd  
and the value of 'b' is wxyz.  
But, what if I want all the such documents in return for which (the value  
of 'a' is abcd and the value of 'b' is wxyz) or (the value of 'a' is abcd  
and tag 'b' doesn't exist) or (tag a doesn't exist and the value of 'b' is  
wxyz).

Is this possible.

Thanks In Advance  
-Azitabh

--

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 6, 2012, 7:08am UTC](https://discuss.elastic.co/t/how-to-match-non-existant-tags/9574/2 "2012-11-06T07:08:41Z")

</div>

You can wrap 2 queries in a Bool Query with should clauses.

That means that the doc which hit the two queries will have an higher score.

See [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html)

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 6 nov. 2012 à 07:39, azi [azitabh@gmail.com](mailto:azitabh@gmail.com) a écrit :

> Lets say I have a set of document which have a-z as keys.  
> And I am firing this term query:  
> curl -X GET localhost:9200/memphis/pincode/\_search?pretty=json -d '{"query":{"term":{"a":"abcd", "b":"wxyz"}}}'
> 
> This will return me all the documents for which the value of 'a' is abcd and the value of 'b' is wxyz.  
> But, what if I want all the such documents in return for which (the value of 'a' is abcd and the value of 'b' is wxyz) or (the value of 'a' is abcd and tag 'b' doesn't exist) or (tag a doesn't exist and the value of 'b' is wxyz).
> 
> Is this possible.
> 
> ## Thanks In Advance -Azitabh

--

---

<div class="post-metadata">

**Author:** ![azitabh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/azitabh/32/2647_2.png) [@azitabh](https://discuss.elastic.co/u/azitabh)\
**Post date:** [November 6, 2012, 7:16am UTC](https://discuss.elastic.co/t/how-to-match-non-existant-tags/9574/3 "2012-11-06T07:16:33Z")

</div>

Thanks David.  
But I missed a condition in my earlier post.  
I also want documents in return which doesn't have any of the tag a & b.  
Which simply means that I want to match a & b only if those are there.  
This I don't think can be modeled in the bool query you have suggested.

-Azitabh

On Tuesday, 6 November 2012 12:38:55 UTC+5:30, David Pilato wrote:

> You can wrap 2 queries in a Bool Query with should clauses.
> 
> That means that the doc which hit the two queries will have an higher  
> score.
> 
> See [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html)
> 
> --  
> David 😉  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> Le 6 nov. 2012 à 07:39, azi \<[azi...@gmail.com](mailto:azi...@gmail.com) \<javascript:\>\> a écrit :
> 
> Lets say I have a set of document which have a-z as keys.  
> And I am firing this term query:  
> curl -X GET localhost:9200/memphis/pincode/\_search?pretty=json -d  
> '{"query":{"term":{"a":"abcd", "b":"wxyz"}}}'
> 
> This will return me all the documents for which the value of 'a' is abcd  
> and the value of 'b' is wxyz.  
> But, what if I want all the such documents in return for which (the value  
> of 'a' is abcd and the value of 'b' is wxyz) or (the value of 'a' is abcd  
> and tag 'b' doesn't exist) or (tag a doesn't exist and the value of 'b' is  
> wxyz).
> 
> Is this possible.
> 
> Thanks In Advance  
> -Azitabh
> 
> --

--

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 6, 2012, 7:25am UTC](https://discuss.elastic.co/t/how-to-match-non-existant-tags/9574/4 "2012-11-06T07:25:21Z")

</div>

"Should" means that the query is not mandatory.  
If you have a 3rd condition (mandatory or a MatchAll), you can create a bool query with :  
Must -\> Your Mandatory Query or MatchAll  
Should -\> TermQuery on "a"  
Should -\> TermQuery on "b"

It should work. Or I did not fully understand your use case.

HTH

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 6 nov. 2012 à 08:16, azi [azitabh@gmail.com](mailto:azitabh@gmail.com) a écrit :

> Thanks David.  
> But I missed a condition in my earlier post.  
> I also want documents in return which doesn't have any of the tag a & b.  
> Which simply means that I want to match a & b only if those are there.  
> This I don't think can be modeled in the bool query you have suggested.
> 
> -Azitabh
> 
> On Tuesday, 6 November 2012 12:38:55 UTC+5:30, David Pilato wrote:  
> You can wrap 2 queries in a Bool Query with should clauses.
> 
> That means that the doc which hit the two queries will have an higher score.
> 
> See [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html)
> 
> --  
> David 😉  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> Le 6 nov. 2012 à 07:39, azi [azi...@gmail.com](mailto:azi...@gmail.com) a écrit :
> 
> > Lets say I have a set of document which have a-z as keys.  
> > And I am firing this term query:  
> > curl -X GET localhost:9200/memphis/pincode/\_search?pretty=json -d '{"query":{"term":{"a":"abcd", "b":"wxyz"}}}'
> > 
> > This will return me all the documents for which the value of 'a' is abcd and the value of 'b' is wxyz.  
> > But, what if I want all the such documents in return for which (the value of 'a' is abcd and the value of 'b' is wxyz) or (the value of 'a' is abcd and tag 'b' doesn't exist) or (tag a doesn't exist and the value of 'b' is wxyz).
> > 
> > Is this possible.
> > 
> > ## Thanks In Advance -Azitabh
> 
> --

--

---

<div class="post-metadata">

**Author:** ![azitabh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/azitabh/32/2647_2.png) [@azitabh](https://discuss.elastic.co/u/azitabh)\
**Post date:** [November 6, 2012, 7:44am UTC](https://discuss.elastic.co/t/how-to-match-non-existant-tags/9574/5 "2012-11-06T07:44:39Z")

</div>

Should means at least one(or otherwise mentioned) of the conditions  
specified should match.  
So If I say:  
should a=abcd  
should b=wzyz

This will give me all documents which match the condition (the value of 'a'  
is abcd and the value of 'b' is wxyz) or (the value of 'a' is abcd and tag  
'b' doesn't exist) or (tag a doesn't exist and the value of 'b' is wxyz).

But what I want in return is all documents which match the condition (the  
value of 'a' is abcd and the value of 'b' is wxyz) or (the value of 'a' is  
abcd and tag 'b' doesn't exist) or (tag a doesn't exist and the value of  
'b' is wxyz) or (tag 'a' doesn't exist and tag 'b' doesn't exist).

Correct me if I am wrong.

-Azitabh

On Tuesday, 6 November 2012 12:55:36 UTC+5:30, David Pilato wrote:

> "Should" means that the query is not mandatory.  
> If you have a 3rd condition (mandatory or a MatchAll), you can create a  
> bool query with :  
> Must -\> Your Mandatory Query or MatchAll  
> Should -\> TermQuery on "a"  
> Should -\> TermQuery on "b"
> 
> It should work. Or I did not fully understand your use case.
> 
> HTH
> 
> --  
> David 😉  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> Le 6 nov. 2012 à 08:16, azi \<[azi...@gmail.com](mailto:azi...@gmail.com) \<javascript:\>\> a écrit :
> 
> Thanks David.  
> But I missed a condition in my earlier post.  
> I also want documents in return which doesn't have any of the tag a & b.  
> Which simply means that I want to match a & b only if those are there.  
> This I don't think can be modeled in the bool query you have suggested.
> 
> -Azitabh
> 
> On Tuesday, 6 November 2012 12:38:55 UTC+5:30, David Pilato wrote:
> 
> > You can wrap 2 queries in a Bool Query with should clauses.
> > 
> > That means that the doc which hit the two queries will have an higher  
> > score.
> > 
> > See  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html)
> > 
> > --  
> > David 😉  
> > Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> > 
> > Le 6 nov. 2012 à 07:39, azi [azi...@gmail.com](mailto:azi...@gmail.com) a écrit :
> > 
> > Lets say I have a set of document which have a-z as keys.  
> > And I am firing this term query:  
> > curl -X GET localhost:9200/memphis/pincode/\_search?pretty=json -d  
> > '{"query":{"term":{"a":"abcd", "b":"wxyz"}}}'
> > 
> > This will return me all the documents for which the value of 'a' is abcd  
> > and the value of 'b' is wxyz.  
> > But, what if I want all the such documents in return for which (the value  
> > of 'a' is abcd and the value of 'b' is wxyz) or (the value of 'a' is abcd  
> > and tag 'b' doesn't exist) or (tag a doesn't exist and the value of 'b' is  
> > wxyz).
> > 
> > Is this possible.
> > 
> > Thanks In Advance  
> > -Azitabh
> > 
> > --
> > 
> > --

--

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 6, 2012, 8:41am UTC](https://discuss.elastic.co/t/how-to-match-non-existant-tags/9574/6 "2012-11-06T08:41:51Z")

</div>

Ok. I think I understand now.  
You want to get docs that not contain a but you don't want to get docs when a is  
not abcd.

I think you should give a look at the MultiMatch Query:  
[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/multi-match-query.html)  
[http://www.elasticsearch.org/guide/reference/query-dsl/multi-match-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/multi-match-query.html)

David.

Le 6 novembre 2012 à 08:44, azi [azitabh@gmail.com](mailto:azitabh@gmail.com) a écrit :

> Should means at least one(or otherwise mentioned) of the conditions specified  
> should match.  
> So If I say:  
> should a=abcd  
> should b=wzyz
> 
> This will give me all documents which match the condition (the value of 'a'  
> is abcd and the value of 'b' is wxyz) or (the value of 'a' is abcd and tag 'b'  
> doesn't exist) or (tag a doesn't exist and the value of 'b' is wxyz).
> 
> But what I want in return is all documents which match the condition (the  
> value of 'a' is abcd and the value of 'b' is wxyz) or (the value of 'a' is  
> abcd and tag 'b' doesn't exist) or (tag a doesn't exist and the value of 'b'  
> is wxyz) or (tag 'a' doesn't exist and tag 'b' doesn't exist).
> 
> Correct me if I am wrong.
> 
> -Azitabh
> 
> On Tuesday, 6 November 2012 12:55:36 UTC+5:30, David Pilato wrote:
> 
> > > "Should" means that the query is not mandatory.  
> > > If you have a 3rd condition (mandatory or a MatchAll), you can create a  
> > > bool query with :  
> > > Must -\> Your Mandatory Query or MatchAll  
> > > Should -\> TermQuery on "a"  
> > > Should -\> TermQuery on "b"
> > 
> > It should work. Or I did not fully understand your use case.
> > 
> > HTH
> > 
> > --  
> > David 😉  
> > Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> > 
> > Le 6 nov. 2012 à 08:16, azi \< [azi...@gmail.com](mailto:azi...@gmail.com)\> a écrit :
> > 
> > ```
> > > > > Thanks David.
> > 
> > ```
> > 
> > > ```
> > > But I missed a condition in my earlier post.
> > > I also want documents in return which doesn't have any of the tag a
> > > 
> > > ```
> > > 
> > > & b.  
> > > Which simply means that I want to match a & b only if those are  
> > > there.  
> > > This I don't think can be modeled in the bool query you have  
> > > suggested.
> > > 
> > > ```
> > > -Azitabh
> > > 
> > > On Tuesday, 6 November 2012 12:38:55 UTC+5:30, David Pilato wrote:
> > > > > > > You can wrap 2 queries in a Bool Query with
> > > > > > > should clauses.
> > > 
> > > ```
> > > 
> > > > ```
> > > > That means that the doc which hit the two queries will have an
> > > > 
> > > > ```
> > > > 
> > > > higher score.
> > > > 
> > > > ```
> > > > See
> > > > 
> > > > ```
> > > > 
> > > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html)
> > > > 
> > > > ```
> > > > <http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html>
> > > > --
> > > > David ;-)
> > > > 
> > > > ```
> > > > 
> > > > [http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html)  
> > > > Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> > > > 
> > > > ```
> > > > Le 6 nov. 2012 à 07:39, azi <
> > > > 
> > > > ```
> > > > 
> > > > [http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html)  
> > > > [azi...@gmail.com](mailto:azi...@gmail.com)\> a écrit :
> > > > 
> > > > ```
> > > > > > > > > Lets say I have a set of document
> > > > > > > > > which have a-z as keys.
> > > > 
> > > > ```
> > > > 
> > > > > ```
> > > > > And I am firing this term query:
> > > > > curl -X GET
> > > > > 
> > > > > ```
> > > > > 
> > > > > localhost:9200/memphis/pincode/\_search?pretty=json -d  
> > > > > '{"query":{"term":{"a":"abcd", "b":"wxyz"}}}'
> > > > > 
> > > > > ```
> > > > > This will return me all the documents for which the value
> > > > > 
> > > > > ```
> > > > > 
> > > > > of 'a' is abcd and the value of 'b' is wxyz.  
> > > > > But, what if I want all the such documents in return for  
> > > > > which (the value of 'a' is abcd and the value of 'b' is wxyz) or (the  
> > > > > value of 'a' is abcd and tag 'b' doesn't exist) or (tag a doesn't  
> > > > > exist and the value of 'b' is wxyz).
> > > > > 
> > > > > ```
> > > > > Is this possible.
> > > > > 
> > > > > Thanks In Advance
> > > > > -Azitabh
> > > > > 
> > > > > --
> > > > > 
> > > > > > > > > > > > 
> > > > > 
> > > > > ```
> > > 
> > > ```
> > > --
> > > 
> > > ```
> > > 
> > > > > >
> 
> --

--  
David Pilato  
[http://www.scrutmydocs.org/](http://www.scrutmydocs.org/)  
[http://dev.david.pilato.fr/](http://dev.david.pilato.fr/)  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

--

---

<div class="post-metadata">

**Author:** ![azitabh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/azitabh/32/2647_2.png) [@azitabh](https://discuss.elastic.co/u/azitabh)\
**Post date:** [November 6, 2012, 2:59pm UTC](https://discuss.elastic.co/t/how-to-match-non-existant-tags/9574/7 "2012-11-06T14:59:38Z")

</div>

Thanks David!  
But multi\_search will only help me in searching a particular text in  
multiple fields.  
I don't think this will help me solve the problem I previously stated.  
First of all, I don't have a common text to search in the fields a & b. And  
second, It won;t match the documents where neither a nor b is present even  
if I had one.

On Tuesday, 6 November 2012 14:11:57 UTC+5:30, David Pilato wrote:

> Ok. I think I understand now.  
> You want to get docs that not contain a but you don't want to get docs  
> when a is not abcd.
> 
> I think you should give a look at the MultiMatch Query:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/multi-match-query.html)
> 
> David.
> 
> Le 6 novembre 2012 à 08:44, azi \<[azi...@gmail.com](mailto:azi...@gmail.com) \<javascript:\>\> a  
> écrit :
> 
> Should means at least one(or otherwise mentioned) of the conditions  
> specified should match.  
> So If I say:  
> should a=abcd  
> should b=wzyz
> 
> This will give me all documents which match the condition (the value of  
> 'a' is abcd and the value of 'b' is wxyz) or (the value of 'a' is abcd and  
> tag 'b' doesn't exist) or (tag a doesn't exist and the value of 'b' is  
> wxyz).
> 
> But what I want in return is all documents which match the condition (the  
> value of 'a' is abcd and the value of 'b' is wxyz) or (the value of 'a' is  
> abcd and tag 'b' doesn't exist) or (tag a doesn't exist and the value of  
> 'b' is wxyz) or (tag 'a' doesn't exist and tag 'b' doesn't exist).
> 
> Correct me if I am wrong.
> 
> -Azitabh
> 
> On Tuesday, 6 November 2012 12:55:36 UTC+5:30, David Pilato wrote:
> 
> "Should" means that the query is not mandatory.  
> If you have a 3rd condition (mandatory or a MatchAll), you can create a  
> bool query with :  
> Must -\> Your Mandatory Query or MatchAll  
> Should -\> TermQuery on "a"  
> Should -\> TermQuery on "b"
> 
> It should work. Or I did not fully understand your use case.
> 
> HTH
> 
> --  
> David 😉  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> Le 6 nov. 2012 à 08:16, azi \< [azi...@gmail.com](mailto:azi...@gmail.com)\> a écrit :
> 
> Thanks David.  
> But I missed a condition in my earlier post.  
> I also want documents in return which doesn't have any of the tag a & b.  
> Which simply means that I want to match a & b only if those are there.  
> This I don't think can be modeled in the bool query you have suggested.
> 
> -Azitabh
> 
> On Tuesday, 6 November 2012 12:38:55 UTC+5:30, David Pilato wrote:
> 
> You can wrap 2 queries in a Bool Query with should clauses.
> 
> That means that the doc which hit the two queries will have an higher  
> score.
> 
> See  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html)
> 
> --  
> David 😉[http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html)  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> Le 6 nov. 2012 à 07:39, azi \<[http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html)  
> [azi...@gmail.com](mailto:azi...@gmail.com)\> a écrit :
> 
> Lets say I have a set of document which have a-z as keys.  
> And I am firing this term query:  
> curl -X GET localhost:9200/memphis/pincode/\_search?pretty=json -d  
> '{"query":{"term":{"a":"abcd", "b":"wxyz"}}}'
> 
> This will return me all the documents for which the value of 'a' is abcd  
> and the value of 'b' is wxyz.  
> But, what if I want all the such documents in return for which (the value  
> of 'a' is abcd and the value of 'b' is wxyz) or (the value of 'a' is abcd  
> and tag 'b' doesn't exist) or (tag a doesn't exist and the value of 'b' is  
> wxyz).
> 
> Is this possible.
> 
> Thanks In Advance  
> -Azitabh
> 
> --
> 
> --
> 
> --
> 
> --  
> David Pilato  
> [http://www.scrutmydocs.org/](http://www.scrutmydocs.org/)  
> [http://dev.david.pilato.fr/](http://dev.david.pilato.fr/)  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

--

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [November 6, 2012, 11:40pm UTC](https://discuss.elastic.co/t/how-to-match-non-existant-tags/9574/8 "2012-11-06T23:40:12Z")

</div>

I think you can simplify your expression to (a=abcd OR a doesn't exist) AND  
(b=wxyz OR b doesn't exist), which can be translated into the following  
query:

{  
"query" : {  
"constant\_score" : {  
"filter" : {  
"and" : [  
{  
"or": [  
{  
"term": {"a" : "abcd"}  
},  
{  
"missing": {"field": "a"}  
}  
]  
},  
{  
"or": [  
{  
"term": {"b" : "wxyz"}  
},  
{  
"missing": {"field": "b"}  
}  
]  
}  
]  
}  
}  
}  
}

On Tuesday, November 6, 2012 9:59:38 AM UTC-5, azi wrote:

> Thanks David!  
> But multi\_search will only help me in searching a particular text in  
> multiple fields.  
> I don't think this will help me solve the problem I previously stated.  
> First of all, I don't have a common text to search in the fields a & b.  
> And second, It won;t match the documents where neither a nor b is present  
> even if I had one.
> 
> On Tuesday, 6 November 2012 14:11:57 UTC+5:30, David Pilato wrote:
> 
> > Ok. I think I understand now.  
> > You want to get docs that not contain a but you don't want to get docs  
> > when a is not abcd.
> > 
> > I think you should give a look at the MultiMatch Query:  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/multi-match-query.html)
> > 
> > David.
> > 
> > Le 6 novembre 2012 à 08:44, azi [azi...@gmail.com](mailto:azi...@gmail.com) a écrit :
> > 
> > Should means at least one(or otherwise mentioned) of the conditions  
> > specified should match.  
> > So If I say:  
> > should a=abcd  
> > should b=wzyz
> > 
> > This will give me all documents which match the condition (the value of  
> > 'a' is abcd and the value of 'b' is wxyz) or (the value of 'a' is abcd and  
> > tag 'b' doesn't exist) or (tag a doesn't exist and the value of 'b' is  
> > wxyz).
> > 
> > But what I want in return is all documents which match the condition (the  
> > value of 'a' is abcd and the value of 'b' is wxyz) or (the value of 'a' is  
> > abcd and tag 'b' doesn't exist) or (tag a doesn't exist and the value of  
> > 'b' is wxyz) or (tag 'a' doesn't exist and tag 'b' doesn't exist).
> > 
> > Correct me if I am wrong.
> > 
> > -Azitabh
> > 
> > On Tuesday, 6 November 2012 12:55:36 UTC+5:30, David Pilato wrote:
> > 
> > "Should" means that the query is not mandatory.  
> > If you have a 3rd condition (mandatory or a MatchAll), you can create a  
> > bool query with :  
> > Must -\> Your Mandatory Query or MatchAll  
> > Should -\> TermQuery on "a"  
> > Should -\> TermQuery on "b"
> > 
> > It should work. Or I did not fully understand your use case.
> > 
> > HTH
> > 
> > --  
> > David 😉  
> > Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> > 
> > Le 6 nov. 2012 à 08:16, azi \< [azi...@gmail.com](mailto:azi...@gmail.com)\> a écrit :
> > 
> > Thanks David.  
> > But I missed a condition in my earlier post.  
> > I also want documents in return which doesn't have any of the tag a & b.  
> > Which simply means that I want to match a & b only if those are there.  
> > This I don't think can be modeled in the bool query you have suggested.
> > 
> > -Azitabh
> > 
> > On Tuesday, 6 November 2012 12:38:55 UTC+5:30, David Pilato wrote:
> > 
> > You can wrap 2 queries in a Bool Query with should clauses.
> > 
> > That means that the doc which hit the two queries will have an higher  
> > score.
> > 
> > See  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html)
> > 
> > --  
> > David 😉[http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html)  
> > Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> > 
> > Le 6 nov. 2012 à 07:39, azi \<[http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html)  
> > [azi...@gmail.com](mailto:azi...@gmail.com)\> a écrit :
> > 
> > Lets say I have a set of document which have a-z as keys.  
> > And I am firing this term query:  
> > curl -X GET localhost:9200/memphis/pincode/\_search?pretty=json -d  
> > '{"query":{"term":{"a":"abcd", "b":"wxyz"}}}'
> > 
> > This will return me all the documents for which the value of 'a' is abcd  
> > and the value of 'b' is wxyz.  
> > But, what if I want all the such documents in return for which (the value  
> > of 'a' is abcd and the value of 'b' is wxyz) or (the value of 'a' is abcd  
> > and tag 'b' doesn't exist) or (tag a doesn't exist and the value of 'b' is  
> > wxyz).
> > 
> > Is this possible.
> > 
> > Thanks In Advance  
> > -Azitabh
> > 
> > --
> > 
> > --
> > 
> > --
> > 
> > --  
> > David Pilato  
> > [http://www.scrutmydocs.org/](http://www.scrutmydocs.org/)  
> > [http://dev.david.pilato.fr/](http://dev.david.pilato.fr/)  
> > Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

--

---

<div class="post-metadata">

**Author:** ![azitabh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/azitabh/32/2647_2.png) [@azitabh](https://discuss.elastic.co/u/azitabh)\
**Post date:** [November 7, 2012, 5:09am UTC](https://discuss.elastic.co/t/how-to-match-non-existant-tags/9574/9 "2012-11-07T05:09:43Z")

</div>

Thanks a ton Igor!  
This is what I was looking for. Restructuring isn't a problem. I just  
didn't know about the missing filter.

Thanks Again  
-Azitabh

On Wednesday, 7 November 2012 05:10:12 UTC+5:30, Igor Motov wrote:

> I think you can simplify your expression to (a=abcd OR a doesn't exist)  
> AND (b=wxyz OR b doesn't exist), which can be translated into the following  
> query:
> 
> {  
> "query" : {  
> "constant\_score" : {  
> "filter" : {  
> "and" : [  
> {  
> "or": [  
> {  
> "term": {"a" : "abcd"}  
> },  
> {  
> "missing": {"field": "a"}  
> }  
> ]  
> },  
> {  
> "or": [  
> {  
> "term": {"b" : "wxyz"}  
> },  
> {  
> "missing": {"field": "b"}  
> }  
> ]  
> }  
> ]  
> }  
> }  
> }  
> }
> 
> On Tuesday, November 6, 2012 9:59:38 AM UTC-5, azi wrote:
> 
> > Thanks David!  
> > But multi\_search will only help me in searching a particular text in  
> > multiple fields.  
> > I don't think this will help me solve the problem I previously stated.  
> > First of all, I don't have a common text to search in the fields a & b.  
> > And second, It won;t match the documents where neither a nor b is present  
> > even if I had one.
> > 
> > On Tuesday, 6 November 2012 14:11:57 UTC+5:30, David Pilato wrote:
> > 
> > > Ok. I think I understand now.  
> > > You want to get docs that not contain a but you don't want to get docs  
> > > when a is not abcd.
> > > 
> > > I think you should give a look at the MultiMatch Query:  
> > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/multi-match-query.html)
> > > 
> > > David.
> > > 
> > > Le 6 novembre 2012 à 08:44, azi [azi...@gmail.com](mailto:azi...@gmail.com) a écrit :
> > > 
> > > Should means at least one(or otherwise mentioned) of the conditions  
> > > specified should match.  
> > > So If I say:  
> > > should a=abcd  
> > > should b=wzyz
> > > 
> > > This will give me all documents which match the condition (the value of  
> > > 'a' is abcd and the value of 'b' is wxyz) or (the value of 'a' is abcd and  
> > > tag 'b' doesn't exist) or (tag a doesn't exist and the value of 'b' is  
> > > wxyz).
> > > 
> > > But what I want in return is all documents which match the condition  
> > > (the value of 'a' is abcd and the value of 'b' is wxyz) or (the value of  
> > > 'a' is abcd and tag 'b' doesn't exist) or (tag a doesn't exist and the  
> > > value of 'b' is wxyz) or (tag 'a' doesn't exist and tag 'b' doesn't exist).
> > > 
> > > Correct me if I am wrong.
> > > 
> > > -Azitabh
> > > 
> > > On Tuesday, 6 November 2012 12:55:36 UTC+5:30, David Pilato wrote:
> > > 
> > > "Should" means that the query is not mandatory.  
> > > If you have a 3rd condition (mandatory or a MatchAll), you can create a  
> > > bool query with :  
> > > Must -\> Your Mandatory Query or MatchAll  
> > > Should -\> TermQuery on "a"  
> > > Should -\> TermQuery on "b"
> > > 
> > > It should work. Or I did not fully understand your use case.
> > > 
> > > HTH
> > > 
> > > --  
> > > David 😉  
> > > Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> > > 
> > > Le 6 nov. 2012 à 08:16, azi \< [azi...@gmail.com](mailto:azi...@gmail.com)\> a écrit :
> > > 
> > > Thanks David.  
> > > But I missed a condition in my earlier post.  
> > > I also want documents in return which doesn't have any of the tag a & b.  
> > > Which simply means that I want to match a & b only if those are there.  
> > > This I don't think can be modeled in the bool query you have suggested.
> > > 
> > > -Azitabh
> > > 
> > > On Tuesday, 6 November 2012 12:38:55 UTC+5:30, David Pilato wrote:
> > > 
> > > You can wrap 2 queries in a Bool Query with should clauses.
> > > 
> > > That means that the doc which hit the two queries will have an higher  
> > > score.
> > > 
> > > See  
> > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html)
> > > 
> > > --  
> > > David 😉[http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html)  
> > > Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> > > 
> > > Le 6 nov. 2012 à 07:39, azi \<[http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html)  
> > > [azi...@gmail.com](mailto:azi...@gmail.com)\> a écrit :
> > > 
> > > Lets say I have a set of document which have a-z as keys.  
> > > And I am firing this term query:  
> > > curl -X GET localhost:9200/memphis/pincode/\_search?pretty=json -d  
> > > '{"query":{"term":{"a":"abcd", "b":"wxyz"}}}'
> > > 
> > > This will return me all the documents for which the value of 'a' is abcd  
> > > and the value of 'b' is wxyz.  
> > > But, what if I want all the such documents in return for which (the  
> > > value of 'a' is abcd and the value of 'b' is wxyz) or (the value of 'a' is  
> > > abcd and tag 'b' doesn't exist) or (tag a doesn't exist and the value of  
> > > 'b' is wxyz).
> > > 
> > > Is this possible.
> > > 
> > > Thanks In Advance  
> > > -Azitabh
> > > 
> > > --
> > > 
> > > --
> > > 
> > > --
> > > 
> > > --  
> > > David Pilato  
> > > [http://www.scrutmydocs.org/](http://www.scrutmydocs.org/)  
> > > [http://dev.david.pilato.fr/](http://dev.david.pilato.fr/)  
> > > Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

--

---

<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 6, 2017, 3:05am UTC](https://discuss.elastic.co/t/how-to-match-non-existant-tags/9574/10 "2017-07-06T03:05:49Z")

</div>


