# Help with a UNION style search

**URL:** <https://discuss.elastic.co/t/help-with-a-union-style-search/11724>\
**Category:** Elasticsearch\
**Created:** [April 29, 2013, 6:19am UTC](https://discuss.elastic.co/t/help-with-a-union-style-search/11724 "2013-04-29T06:19:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![brendan\_rb](https://avatars.discourse-cdn.com/v4/letter/b/f14d63/32.png) [@brendan\_rb](https://discuss.elastic.co/u/brendan_rb)\
**Post date:** [April 29, 2013, 6:19am UTC](https://discuss.elastic.co/t/help-with-a-union-style-search/11724/1 "2013-04-29T06:19:24Z")

</div>

Hi there,

Unfortunately I don't have a set of CURL steps available at the moment so I  
will do my best to succinctly describe my situation.

We currently have two mappings, one for beneficiaries, the other for  
campaigns.

When searching for a term, such as "heart", I am trying to return all  
results that match this term from both mappings, and if the potential  
result is from the campaigns mapping, also ensure that the campaign is  
'active' (current date is between start & end date of campaign).

This is query I have constructed:

{  
"min\_score": 0.99,  
"from": 0,  
"size": 9,  
"query": {  
"query\_string": {  
"query": "heart",  
"default\_operator": "OR",  
"fields": [  
"beneficiaries.name.search",  
"beneficiaries.categories.search^2",  
"campaigns.beneficiary.search",  
"campaigns.categories.search^2"  
]  
}  
},  
"filter": {  
"and": [  
{  
"terms": {  
"\_type": [  
"beneficiaries",  
"campaigns"  
]  
}  
},  
{  
"range": {  
"start\_date.value": {  
"lte": "now",  
"gte": "19700101T100000Z"  
}  
}  
},  
{  
"range": {  
"end\_date.value": {  
"lte": "20150429T161301Z",  
"gte": "now"  
}  
}  
}  
]  
}  
}

This query "works", but fails to return any results from the beneficiaries  
mapping.

I think where this query is going wrong is it trying to match the range  
even though those fields don't exist on the beneficiaries mapping.

Is there some sort of way to require the result to match these fields  
(start\_date, end\_date range) if they exist (ie. \_type = "campaigns") and  
ignore them otherwise?

Thanks for any help!

Cheers,

Brendan

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [April 29, 2013, 2:15pm UTC](https://discuss.elastic.co/t/help-with-a-union-style-search/11724/2 "2013-04-29T14:15:09Z")

</div>

Do the date fields always exist in the campaign data and always do not  
exist in the beneficiaries? If so, you can add a missing filter to your  
existing filter. You can change your and filter into a bool filter and add  
it as an should clause or wrap your existing and filter in an or filter.

--  
Ivan

On Sun, Apr 28, 2013 at 11:19 PM, [brendan.rb@randb.com.au](mailto:brendan.rb@randb.com.au) wrote:

> Hi there,
> 
> Unfortunately I don't have a set of CURL steps available at the moment so  
> I will do my best to succinctly describe my situation.
> 
> We currently have two mappings, one for beneficiaries, the other for  
> campaigns.
> 
> When searching for a term, such as "heart", I am trying to return all  
> results that match this term from both mappings, and if the potential  
> result is from the campaigns mapping, also ensure that the campaign is  
> 'active' (current date is between start & end date of campaign).
> 
> This is query I have constructed:
> 
> {  
> "min\_score": 0.99,  
> "from": 0,  
> "size": 9,  
> "query": {  
> "query\_string": {  
> "query": "heart",  
> "default\_operator": "OR",  
> "fields": [  
> "beneficiaries.name.search",  
> "beneficiaries.categories.search^2",  
> "campaigns.beneficiary.search",  
> "campaigns.categories.search^2"  
> ]  
> }  
> },  
> "filter": {  
> "and": [  
> {  
> "terms": {  
> "\_type": [  
> "beneficiaries",  
> "campaigns"  
> ]  
> }  
> },  
> {  
> "range": {  
> "start\_date.value": {  
> "lte": "now",  
> "gte": "19700101T100000Z"  
> }  
> }  
> },  
> {  
> "range": {  
> "end\_date.value": {  
> "lte": "20150429T161301Z",  
> "gte": "now"  
> }  
> }  
> }  
> ]  
> }  
> }
> 
> This query "works", but fails to return any results from the beneficiaries  
> mapping.
> 
> I think where this query is going wrong is it trying to match the range  
> even though those fields don't exist on the beneficiaries mapping.
> 
> Is there some sort of way to require the result to match these fields  
> (start\_date, end\_date range) if they exist (ie. \_type = "campaigns") and  
> ignore them otherwise?
> 
> Thanks for any help!
> 
> Cheers,
> 
> Brendan
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![brendan\_rb](https://avatars.discourse-cdn.com/v4/letter/b/f14d63/32.png) [@brendan\_rb](https://discuss.elastic.co/u/brendan_rb)\
**Post date:** [April 29, 2013, 11:25pm UTC](https://discuss.elastic.co/t/help-with-a-union-style-search/11724/3 "2013-04-29T23:25:29Z")

</div>

Hi Ivan,

Thanks for the reply. I managed to get it working using and/or:

{  
"from": 0,  
"size": 9,  
"query": {  
"query\_string": {  
"query": "heart",  
"default\_operator": "OR",  
"fields": [  
"beneficiaries.name.search",  
"beneficiaries.categories.search^2",  
"campaigns.beneficiary.search",  
"campaigns.categories.search^2"  
]  
}  
},  
"filter": {  
"or": [  
{  
"terms": {  
"\_type": [  
"beneficiaries"  
]  
}  
},  
{  
"and": [  
{  
"terms": {  
"\_type": [  
"campaigns"  
]  
}  
},  
{  
"range": {  
"start\_date.value": {  
"lte": "now",  
"gte": "19700101T100000Z"  
}  
}  
},  
{  
"range": {  
"end\_date.value": {  
"lte": "20150429T161301Z",  
"gte": "now"  
}  
}  
}  
]  
}  
]  
}  
}

I did see the docs on the missing filter, but I wasn't sure on how to apply  
in this scenario. Could you show an brief example? I'm curious 🙂

Thanks again,

Brendan

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![brendan\_rb](https://avatars.discourse-cdn.com/v4/letter/b/f14d63/32.png) [@brendan\_rb](https://discuss.elastic.co/u/brendan_rb)\
**Post date:** [April 30, 2013, 7:12am UTC](https://discuss.elastic.co/t/help-with-a-union-style-search/11724/4 "2013-04-30T07:12:30Z")

</div>

Hi Ivan,

I now think I know why you mentioned the missing filter. Some campaigns  
don't have start/end dates, so in this scenario we need a query that will  
use start/end dates if they exist, and if they are missing, just include  
that result anyway. Could you show an example of this in action?

Reading this[http://www.elasticsearch.org/guide/reference/query-dsl/missing-filter/](http://www.elasticsearch.org/guide/reference/query-dsl/missing-filter/),  
is it just another item in the "and" of the campaign related filters?

Thanks for your time,

Brendan

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [April 30, 2013, 6:00pm UTC](https://discuss.elastic.co/t/help-with-a-union-style-search/11724/5 "2013-04-30T18:00:28Z")

</div>

I originally suggested the missing filter because I missed the fact that  
your "mappings" are essentially different types. A different mapping  
doesn't have to be a different type, but it should be. Your second solution  
is ideal since it you can filter on the type.

As far as the missing filter goes, you are correct and you should just add  
it to your existing filters. Something like  
(BENEFICIARY || (CAMPAIGN && (some\_range\_filter || missing filter))

--  
Ivan

On Tue, Apr 30, 2013 at 12:12 AM, [brendan.rb@randb.com.au](mailto:brendan.rb@randb.com.au) wrote:

> Hi Ivan,
> 
> I now think I know why you mentioned the missing filter. Some campaigns  
> don't have start/end dates, so in this scenario we need a query that will  
> use start/end dates if they exist, and if they are missing, just include  
> that result anyway. Could you show an example of this in action?
> 
> Reading this[http://www.elasticsearch.org/guide/reference/query-dsl/missing-filter/](http://www.elasticsearch.org/guide/reference/query-dsl/missing-filter/),  
> is it just another item in the "and" of the campaign related filters?
> 
> Thanks for your time,
> 
> Brendan
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:39am UTC](https://discuss.elastic.co/t/help-with-a-union-style-search/11724/6 "2017-07-06T02:39:02Z")

</div>

Closed on July 6, 2017, 2:39am UTC


