# Querying the results of a FilteredQuery

**URL:** <https://discuss.elastic.co/t/querying-the-results-of-a-filteredquery/8955>\
**Category:** Elasticsearch\
**Created:** [September 7, 2012, 4:02pm UTC](https://discuss.elastic.co/t/querying-the-results-of-a-filteredquery/8955 "2012-09-07T16:02:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Allison\_A](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@Allison\_A](https://discuss.elastic.co/u/Allison_A)\
**Post date:** [September 7, 2012, 4:02pm UTC](https://discuss.elastic.co/t/querying-the-results-of-a-filteredquery/8955/1 "2012-09-07T16:02:12Z")

</div>

Hello ElasticSearch friends,

I have come to a dilemma. Forgive me if I am a QueryDSL newbie, but is  
there a way to do a scored "match" query against the results of a filtered  
query? I know filtered queries do not return boost.

Effectively I would like to return text relevancy filtered down by geo  
distance.

Here is the query set up in C# code (although it should make sense for  
everyone!):

```
        var json = new {
            query = new {
                filtered = new {
                    query = new {
                        //match_all = new { }
                        query_string = new {
                            fields = new string[] {
                                "BusinessName^1.0", "Teaser^0.2", 

```

"ReviewText^0.7",  
"CategoryMajor^2.0",  
"CategoryMinor^0.5",  
"State^0.5", "City^0.5"  
},  
query = "restaurants",  
use\_dis\_max = true  
}  
},  
filter = new {  
geo\_distance = new {  
distance = "100mi",  
Location = "40.748470,-73.939180"  
}  
}  
}  
},  
sort = new {  
\_geo\_distance = new {  
Location = "40.748470,-73.939180",  
order = "asc",  
unit = "mi"  
}  
},  
from = 0,  
size = 5  
};

How do I get it to now sort by score??

Thanks,

Allison

--

---

<div class="post-metadata">

**Author:** ![Allison\_A](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@Allison\_A](https://discuss.elastic.co/u/Allison_A)\
**Post date:** [September 7, 2012, 4:26pm UTC](https://discuss.elastic.co/t/querying-the-results-of-a-filteredquery/8955/2 "2012-09-07T16:26:49Z")

</div>

I don't know if I explained this well.

I feel like the results I am getting are showing sort by distance, but not  
also taking into account the relevancy of the text. Like, I am getting back  
a dance studio with one instance of "restaurant" in "CategoryMajor" as  
prioritized over a restaurant with "restaurant restaurant restaurant  
restaurant" in CategoryMajor, because of the distance.

I want a sort that takes into account relevancy AND distance in the  
weighting.

On Friday, September 7, 2012 12:02:12 PM UTC-4, Allison A. wrote:

> Hello Elasticsearch friends,
> 
> I have come to a dilemma. Forgive me if I am a QueryDSL newbie, but is  
> there a way to do a scored "match" query against the results of a filtered  
> query? I know filtered queries do not return boost.
> 
> Effectively I would like to return text relevancy filtered down by geo  
> distance.
> 
> Here is the query set up in C# code (although it should make sense for  
> everyone!):
> 
> ```
> var json = new {
> query = new {
> filtered = new {
> query = new {
> //match_all = new { }
> query_string = new {
> fields = new string[] {
> "BusinessName^1.0", "Teaser^0.2", 
> 
> ```
> 
> "ReviewText^0.7",  
> "CategoryMajor^2.0",  
> "CategoryMinor^0.5",  
> "State^0.5", "City^0.5"  
> },  
> query = "restaurants",  
> use\_dis\_max = true  
> }  
> },  
> filter = new {  
> geo\_distance = new {  
> distance = "100mi",  
> Location = "40.748470,-73.939180"  
> }  
> }  
> }  
> },  
> sort = new {  
> \_geo\_distance = new {  
> Location = "40.748470,-73.939180",  
> order = "asc",  
> unit = "mi"  
> }  
> },  
> from = 0,  
> size = 5  
> };
> 
> How do I get it to now sort by score??
> 
> Thanks,
> 
> Allison

--

---

<div class="post-metadata">

**Author:** ![phill](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@phill](https://discuss.elastic.co/u/phill)\
**Post date:** [September 7, 2012, 9:36pm UTC](https://discuss.elastic.co/t/querying-the-results-of-a-filteredquery/8955/3 "2012-09-07T21:36:27Z")

</div>

You can add extra fields to short by, but in your case maybe what you  
really want is to NOT sort by location, but instead sort by score.  
You have already filtered by distance and then matched as appropriate  
then filtered.

-Paul

On 9/7/2012 9:26 AM, Allison A. wrote:

> ```
> sort = new {
> _geo_distance = new {
> Location = "40.748470,-73.939180",
> order = "asc",
> unit = "mi"
> }
> },
> from = 0,
> size = 5
> };
> 
> How do I get it to now sort by score??
> 
> ```

--

---

<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:13am UTC](https://discuss.elastic.co/t/querying-the-results-of-a-filteredquery/8955/4 "2017-07-06T03:13:37Z")

</div>


