# Is it possible to combine a custom score query with a filtered query?

**URL:** https://discuss.elastic.co/t/is-it-possible-to-combine-a-custom-score-query-with-a-filtered-query/9958
**Category:** Elasticsearch
**Created:** [December 5, 2012, 7:02pm UTC](https://discuss.elastic.co/t/is-it-possible-to-combine-a-custom-score-query-with-a-filtered-query/9958 "2012-12-05T19:02:02Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Anthony\_Campagna](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anthony_campagna/32/2079_2.png) [@Anthony\_Campagna](https://discuss.elastic.co/u/Anthony_Campagna)
#### Post date: [December 5, 2012, 7:02pm UTC](https://discuss.elastic.co/t/is-it-possible-to-combine-a-custom-score-query-with-a-filtered-query/9958/1 "2012-12-05T19:02:02Z")

</div>

I have two queries that i've obtained during testing and vetting of  
Elasticsearch. The last part of this process is to merge the parts of both  
queries into one query if possible. Ultimately, is it possible to combine a  
custom score query with a filtered query? Both queries are listed below. I  
have tried a bunch of different "ideas" but nothing has worked so far.

Note: I am actually using the Java API so I will need to translate the  
final query into Java. Any assistance or hints towards that end would be  
greatly appreciated.

_Custom Score Query_

{  
"query" :{  
"custom\_score": {  
"query": {  
"multi\_match": {  
"query": "italian",  
"fields": [  
"name",  
"tags"  
]  
}  
},  
"script" : "citymapsscript",  
"params" :{  
"lat": 40.76405282025,  
"lon": -73.972994269042  
},  
"lang": "native"  
}  
}  
}

_Filtered Query:_  
\*  
\*  
{  
"from": 0,  
"size": 50,  
"query": {  
"filtered": {  
"query": {  
"multi\_match": {  
"query": "italian",  
"fields": [  
"name",  
"tags"  
]  
}  
},  
"filter": {  
"geo\_distance": {  
"distance": "5km",  
"location": {  
"lat": 40.76405282025,  
"lon": -73.972994269042  
}  
}  
}  
}  
}  
}

--

---

<div class="post-metadata">

### Author: ![Anthony\_Campagna](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anthony_campagna/32/2079_2.png) [@Anthony\_Campagna](https://discuss.elastic.co/u/Anthony_Campagna)
#### Post date: [December 5, 2012, 7:59pm UTC](https://discuss.elastic.co/t/is-it-possible-to-combine-a-custom-score-query-with-a-filtered-query/9958/2 "2012-12-05T19:59:56Z")

</div>

The following MIGHT have done it:

{  
"query" :{  
"custom\_score": {  
"query" :{  
"filtered": {  
"query": {  
"multi\_match": {  
"query": "italian",  
"fields": [  
"name",  
"tags"  
]  
}  
},  
"filter": {  
"geo\_distance": {  
"distance": "5km",  
"location": {  
"lat": 40.76405282025,  
"lon": -73.972994269042  
}  
}  
}  
}  
},  
"script" : "citymapsscript",  
"params" :{  
"lat": 40.76405282025,  
"lon": -73.972994269042  
},  
"lang": "native"  
}  
}  
}

On Wednesday, December 5, 2012 2:02:02 PM UTC-5, Anthony Campagna wrote:

> I have two queries that i've obtained during testing and vetting of  
> Elasticsearch. The last part of this process is to merge the parts of both  
> queries into one query if possible. Ultimately, is it possible to combine a  
> custom score query with a filtered query? Both queries are listed below. I  
> have tried a bunch of different "ideas" but nothing has worked so far.
> 
> Note: I am actually using the Java API so I will need to translate the  
> final query into Java. Any assistance or hints towards that end would be  
> greatly appreciated.
> 
> _Custom Score Query_
> 
> {  
> "query" :{  
> "custom\_score": {  
> "query": {  
> "multi\_match": {  
> "query": "italian",  
> "fields": [  
> "name",  
> "tags"  
> ]  
> }  
> },  
> "script" : "citymapsscript",  
> "params" :{  
> "lat": 40.76405282025,  
> "lon": -73.972994269042  
> },  
> "lang": "native"  
> }  
> }  
> }
> 
> _Filtered Query:_  
> \*  
> \*  
> {  
> "from": 0,  
> "size": 50,  
> "query": {  
> "filtered": {  
> "query": {  
> "multi\_match": {  
> "query": "italian",  
> "fields": [  
> "name",  
> "tags"  
> ]  
> }  
> },  
> "filter": {  
> "geo\_distance": {  
> "distance": "5km",  
> "location": {  
> "lat": 40.76405282025,  
> "lon": -73.972994269042  
> }  
> }  
> }  
> }  
> }  
> }

--

---

<div class="post-metadata">

### Author: ![javanna](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/javanna/32/4698_2.png) [@javanna](https://discuss.elastic.co/u/javanna)
#### Post date: [December 5, 2012, 8:08pm UTC](https://discuss.elastic.co/t/is-it-possible-to-combine-a-custom-score-query-with-a-filtered-query/9958/3 "2012-12-05T20:08:34Z")

</div>

Yep, correct, I was about to reply.

Something like this with the Java API:

QueryBuilders.customScoreQuery(  
QueryBuilders.filteredQuery(  
QueryBuilders.multiMatchQuery("italian", "name",  
"tags"),

FilterBuilders.geoDistanceFilter("geo\_distance").distance("5km").lat(40.76405282025).lon(-73.972994269042)))  
.script("citymapsscript").param("lat",  
40.76405282025).param("lon", -73.972994269042);

Cheers

On Wednesday, December 5, 2012 8:59:56 PM UTC+1, Anthony Campagna wrote:

> The following MIGHT have done it:
> 
> {  
> "query" :{  
> "custom\_score": {  
> "query" :{  
> "filtered": {  
> "query": {  
> "multi\_match": {  
> "query": "italian",  
> "fields": [  
> "name",  
> "tags"  
> ]  
> }  
> },  
> "filter": {  
> "geo\_distance": {  
> "distance": "5km",  
> "location": {  
> "lat": 40.76405282025,  
> "lon": -73.972994269042  
> }  
> }  
> }  
> }  
> },  
> "script" : "citymapsscript",  
> "params" :{  
> "lat": 40.76405282025,  
> "lon": -73.972994269042  
> },  
> "lang": "native"  
> }  
> }  
> }
> 
> On Wednesday, December 5, 2012 2:02:02 PM UTC-5, Anthony Campagna wrote:
> 
> > I have two queries that i've obtained during testing and vetting of  
> > Elasticsearch. The last part of this process is to merge the parts of both  
> > queries into one query if possible. Ultimately, is it possible to combine a  
> > custom score query with a filtered query? Both queries are listed below. I  
> > have tried a bunch of different "ideas" but nothing has worked so far.
> > 
> > Note: I am actually using the Java API so I will need to translate the  
> > final query into Java. Any assistance or hints towards that end would be  
> > greatly appreciated.
> > 
> > _Custom Score Query_
> > 
> > {  
> > "query" :{  
> > "custom\_score": {  
> > "query": {  
> > "multi\_match": {  
> > "query": "italian",  
> > "fields": [  
> > "name",  
> > "tags"  
> > ]  
> > }  
> > },  
> > "script" : "citymapsscript",  
> > "params" :{  
> > "lat": 40.76405282025,  
> > "lon": -73.972994269042  
> > },  
> > "lang": "native"  
> > }  
> > }  
> > }
> > 
> > _Filtered Query:_  
> > \*  
> > \*  
> > {  
> > "from": 0,  
> > "size": 50,  
> > "query": {  
> > "filtered": {  
> > "query": {  
> > "multi\_match": {  
> > "query": "italian",  
> > "fields": [  
> > "name",  
> > "tags"  
> > ]  
> > }  
> > },  
> > "filter": {  
> > "geo\_distance": {  
> > "distance": "5km",  
> > "location": {  
> > "lat": 40.76405282025,  
> > "lon": -73.972994269042  
> > }  
> > }  
> > }  
> > }  
> > }  
> > }

--

---

<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:01am UTC](https://discuss.elastic.co/t/is-it-possible-to-combine-a-custom-score-query-with-a-filtered-query/9958/4 "2017-07-06T03:01:11Z")

</div>


