# Filter only if there are matches against a field

**URL:** <https://discuss.elastic.co/t/filter-only-if-there-are-matches-against-a-field/12741>\
**Category:** Elasticsearch\
**Created:** [July 10, 2013, 12:27pm UTC](https://discuss.elastic.co/t/filter-only-if-there-are-matches-against-a-field/12741 "2013-07-10T12:27:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrea\_Salvadore](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrea_salvadore/32/1839_2.png) [@Andrea\_Salvadore](https://discuss.elastic.co/u/Andrea_Salvadore)\
**Post date:** [July 10, 2013, 12:27pm UTC](https://discuss.elastic.co/t/filter-only-if-there-are-matches-against-a-field/12741/1 "2013-07-10T12:27:12Z")

</div>

Hi all,  
What I'm trying to do is a search that will narrow down results to specific  
locations. If a user types "iphones in London" I would like to show results  
in London and exclude everything else.  
If the search is "red iphone" then the filter should not be applied.

I know I can use different boosts but I need to use a different ordering by  
time. So results that are recent and not in the location are bumped up to  
the top.

Is there anyway to do this in a query or do I need to do it in code / query?

Thanks

--  
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:** ![Itamar\_Syn\_Hershko](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/itamar_syn_hershko/32/725_2.png) [@Itamar\_Syn\_Hershko](https://discuss.elastic.co/u/Itamar_Syn_Hershko)\
**Post date:** [July 10, 2013, 1:53pm UTC](https://discuss.elastic.co/t/filter-only-if-there-are-matches-against-a-field/12741/2 "2013-07-10T13:53:19Z")

</div>

You are not looking at Boosts, you want to use geo-filter and  
location-aware query parsing

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

On Wed, Jul 10, 2013 at 3:27 PM, Andrea Salvadore \<  
[andrea.salvadore@gmail.com](mailto:andrea.salvadore@gmail.com)\> wrote:

> Hi all,  
> What I'm trying to do is a search that will narrow down results to  
> specific locations. If a user types "iphones in London" I would like to  
> show results in London and exclude everything else.  
> If the search is "red iphone" then the filter should not be applied.
> 
> I know I can use different boosts but I need to use a different ordering  
> by time. So results that are recent and not in the location are bumped up  
> to the top.
> 
> Is there anyway to do this in a query or do I need to do it in code /  
> query?
> 
> Thanks
> 
> --  
> 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:** ![Andrea\_Salvadore](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrea_salvadore/32/1839_2.png) [@Andrea\_Salvadore](https://discuss.elastic.co/u/Andrea_Salvadore)\
**Post date:** [July 10, 2013, 3:28pm UTC](https://discuss.elastic.co/t/filter-only-if-there-are-matches-against-a-field/12741/3 "2013-07-10T15:28:43Z")

</div>

Hi Itamar, thanks for your comment.  
The location is an example as I need to do the same thing for other fields,  
like categories..

A user could type "Hamlet in Books" I would like to filter out anything  
that is not a book..

On Wednesday, 10 July 2013 14:53:19 UTC+1, Itamar Syn-Hershko wrote:

> You are not looking at Boosts, you want to use geo-filter and  
> location-aware query parsing  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/geo-distance-filter/)
> 
> On Wed, Jul 10, 2013 at 3:27 PM, Andrea Salvadore \<[andrea.s...@gmail.com](mailto:andrea.s...@gmail.com)\<javascript:\>
> 
> > wrote:
> 
> > Hi all,  
> > What I'm trying to do is a search that will narrow down results to  
> > specific locations. If a user types "iphones in London" I would like to  
> > show results in London and exclude everything else.  
> > If the search is "red iphone" then the filter should not be applied.
> > 
> > I know I can use different boosts but I need to use a different ordering  
> > by time. So results that are recent and not in the location are bumped up  
> > to the top.
> > 
> > Is there anyway to do this in a query or do I need to do it in code /  
> > query?
> > 
> > Thanks
> > 
> > --  
> > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > 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:** ![Itamar\_Syn\_Hershko](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/itamar_syn_hershko/32/725_2.png) [@Itamar\_Syn\_Hershko](https://discuss.elastic.co/u/Itamar_Syn_Hershko)\
**Post date:** [July 10, 2013, 3:36pm UTC](https://discuss.elastic.co/t/filter-only-if-there-are-matches-against-a-field/12741/4 "2013-07-10T15:36:25Z")

</div>

And then you'll use a Term filter or similar. In other words, you need to  
work out a way to parse your app's query language to ES's query DSL which  
includes filters. A good way to do that is use visual facets instead of  
free-text queries.

On Wed, Jul 10, 2013 at 6:28 PM, Andrea Salvadore \<  
[andrea.salvadore@gmail.com](mailto:andrea.salvadore@gmail.com)\> wrote:

> Hi Itamar, thanks for your comment.  
> The location is an example as I need to do the same thing for other  
> fields, like categories..
> 
> A user could type "Hamlet in Books" I would like to filter out anything  
> that is not a book..
> 
> On Wednesday, 10 July 2013 14:53:19 UTC+1, Itamar Syn-Hershko wrote:
> 
> > You are not looking at Boosts, you want to use geo-filter and  
> > location-aware query parsing [http://www](http://www).**[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://elasticsearch.org/guide/)**  
> > reference/query-dsl/geo-\*\*distance-filter/[http://www.elasticsearch.org/guide/reference/query-dsl/geo-distance-filter/](http://www.elasticsearch.org/guide/reference/query-dsl/geo-distance-filter/)
> > 
> > On Wed, Jul 10, 2013 at 3:27 PM, Andrea Salvadore [andrea.s...@gmail.com](mailto:andrea.s...@gmail.com)wrote:
> > 
> > > Hi all,  
> > > What I'm trying to do is a search that will narrow down results to  
> > > specific locations. If a user types "iphones in London" I would like to  
> > > show results in London and exclude everything else.  
> > > If the search is "red iphone" then the filter should not be applied.
> > > 
> > > I know I can use different boosts but I need to use a different ordering  
> > > by time. So results that are recent and not in the location are bumped up  
> > > to the top.
> > > 
> > > Is there anyway to do this in a query or do I need to do it in code /  
> > > query?
> > > 
> > > Thanks
> > > 
> > > --  
> > > 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 elasticsearc...@\*\*[googlegroups.com](http://googlegroups.com).
> > > 
> > > For more options, visit [https://groups.google.com/\*\*groups/opt\_out](https://groups.google.com/**groups/opt_out)[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).

--  
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:** ![Andrea\_Salvadore](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrea_salvadore/32/1839_2.png) [@Andrea\_Salvadore](https://discuss.elastic.co/u/Andrea_Salvadore)\
**Post date:** [July 10, 2013, 4:04pm UTC](https://discuss.elastic.co/t/filter-only-if-there-are-matches-against-a-field/12741/5 "2013-07-10T16:04:08Z")

</div>

Hi Itamar,  
could you go a bit into more details about using facets for this?  
I would still have to run 2 separate queries, is that correct?

On Wednesday, 10 July 2013 16:36:25 UTC+1, Itamar Syn-Hershko wrote:

> And then you'll use a Term filter or similar. In other words, you need to  
> work out a way to parse your app's query language to ES's query DSL which  
> includes filters. A good way to do that is use visual facets instead of  
> free-text queries.
> 
> On Wed, Jul 10, 2013 at 6:28 PM, Andrea Salvadore \<[andrea.s...@gmail.com](mailto:andrea.s...@gmail.com)\<javascript:\>
> 
> > wrote:
> 
> > Hi Itamar, thanks for your comment.  
> > The location is an example as I need to do the same thing for other  
> > fields, like categories..
> > 
> > A user could type "Hamlet in Books" I would like to filter out anything  
> > that is not a book..
> > 
> > On Wednesday, 10 July 2013 14:53:19 UTC+1, Itamar Syn-Hershko wrote:
> > 
> > > You are not looking at Boosts, you want to use geo-filter and  
> > > location-aware query parsing [http://www](http://www).**[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://elasticsearch.org/guide/)**  
> > > reference/query-dsl/geo-\*\*distance-filter/[http://www.elasticsearch.org/guide/reference/query-dsl/geo-distance-filter/](http://www.elasticsearch.org/guide/reference/query-dsl/geo-distance-filter/)
> > > 
> > > On Wed, Jul 10, 2013 at 3:27 PM, Andrea Salvadore \<[andrea.s...@gmail.com](mailto:andrea.s...@gmail.com)
> > > 
> > > > wrote:
> > > 
> > > > Hi all,  
> > > > What I'm trying to do is a search that will narrow down results to  
> > > > specific locations. If a user types "iphones in London" I would like to  
> > > > show results in London and exclude everything else.  
> > > > If the search is "red iphone" then the filter should not be applied.
> > > > 
> > > > I know I can use different boosts but I need to use a different  
> > > > ordering by time. So results that are recent and not in the location are  
> > > > bumped up to the top.
> > > > 
> > > > Is there anyway to do this in a query or do I need to do it in code /  
> > > > query?
> > > > 
> > > > Thanks
> > > > 
> > > > --  
> > > > 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 elasticsearc...@\*\*[googlegroups.com](http://googlegroups.com).
> > > > 
> > > > For more options, visit [https://groups.google.com/\*\*groups/opt\_out](https://groups.google.com/**groups/opt_out)[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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > > 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:27am UTC](https://discuss.elastic.co/t/filter-only-if-there-are-matches-against-a-field/12741/6 "2017-07-06T02:27:09Z")

</div>


