# Boosting results at query time using java api

**URL:** <https://discuss.elastic.co/t/boosting-results-at-query-time-using-java-api/11143>\
**Category:** Elasticsearch\
**Created:** [March 13, 2013, 9:40pm UTC](https://discuss.elastic.co/t/boosting-results-at-query-time-using-java-api/11143 "2013-03-13T21:40:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amit\_Patel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amit_patel/32/2323_2.png) [@Amit\_Patel](https://discuss.elastic.co/u/Amit_Patel)\
**Post date:** [March 13, 2013, 9:40pm UTC](https://discuss.elastic.co/t/boosting-results-at-query-time-using-java-api/11143/1 "2013-03-13T21:40:46Z")

</div>

Hi,  
I am new to ES and trying to search using java apis. I am unable to figure  
out how I can provide filed specific boosting using the java apis.  
Here is the example:  
My index document looks like:

\_source": {

- "th\_id": 1,
- "th\_name": "test name",
- "th\_description": "test desc",
- "th\_image": "test-img",
- "th\_slug": "Make-Me-Smart",
- "th\_show\_title": "Coast Tech Podcast",
- "th\_sh\_category": "Alternative Health

}

When i search for keywords I want to boost the results higher if they found  
in the "th\_name" compared to they're found in some other fields.  
Currently I am using below code to do search:  
QueryBuilder qb1 = QueryBuilders.matchAllQuery();  
FilterBuilder fb = FilterBuilders.orFilter(  
FilterBuilders.termFilter("th\_name", keyword),  
FilterBuilders.termFilter("th\_description", keyword),  
FilterBuilders.termFilter("th\_show\_title", keyword),  
FilterBuilders.termFilter("th\_sh\_category", keyword)  
);

```
    // WildcardQueryBuilder wqb = new WildcardQueryBuilder(name, 

```

wildcard)  
log.info("Executing Query: " + qb1.toString());  
SearchResponse response =  
client.prepareSearch("talk").setTypes("themes")

.setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH).setQuery(qb1).setFilter(fb)  
.setFrom(start).setSize(maxRows)  
.setExplain(true).execute().actionGet();

Is there anything I can do at query time to boost the document if the  
keyword is found in "th\_name" field compared to found in other fields?

--  
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:** ![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:** [March 14, 2013, 6:56am UTC](https://discuss.elastic.co/t/boosting-results-at-query-time-using-java-api/11143/2 "2013-03-14T06:56:57Z")

</div>

Filters are not used for scoring. They only said 'yes' or 'no'!  
If you want to use boost, use queries, not filters.

Does it help?

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

Le 13 mars 2013 à 22:40, Amit Patel [avpatel.257@gmail.com](mailto:avpatel.257@gmail.com) a écrit :

Hi,  
I am new to ES and trying to search using java apis. I am unable to figure out how I can provide filed specific boosting using the java apis.  
Here is the example:  
My index document looks like:

\_source": {  
"th\_id": 1,  
"th\_name": "test name",  
"th\_description": "test desc",  
"th\_image": "test-img",  
"th\_slug": "Make-Me-Smart",  
"th\_show\_title": "Coast Tech Podcast",  
"th\_sh\_category": "Alternative Health  
}

When i search for keywords I want to boost the results higher if they found in the "th\_name" compared to they're found in some other fields.  
Currently I am using below code to do search:  
QueryBuilder qb1 = QueryBuilders.matchAllQuery();  
FilterBuilder fb = FilterBuilders.orFilter(  
FilterBuilders.termFilter("th\_name", keyword),  
FilterBuilders.termFilter("th\_description", keyword),  
FilterBuilders.termFilter("th\_show\_title", keyword),  
FilterBuilders.termFilter("th\_sh\_category", keyword)  
);

```
    // WildcardQueryBuilder wqb = new WildcardQueryBuilder(name, wildcard)
    log.info("Executing Query: " + qb1.toString());
    SearchResponse response = client.prepareSearch("talk").setTypes("themes")
        .setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(qb1).setFilter(fb)
        .setFrom(start).setSize(maxRows)
        .setExplain(true).execute().actionGet();

```

Is there anything I can do at query time to boost the document if the keyword is found in "th\_name" field compared to found in other fields?

--  
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:** ![Olivier\_Girardot](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/olivier_girardot/32/2440_2.png) [@Olivier\_Girardot](https://discuss.elastic.co/u/Olivier_Girardot)\
**Post date:** [March 14, 2013, 1:23pm UTC](https://discuss.elastic.co/t/boosting-results-at-query-time-using-java-api/11143/3 "2013-03-14T13:23:25Z")

</div>

Hi,

David is right, and the java api documentation shows you exactly how to do  
that :  
[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/java-api/query-dsl-queries.html) in  
the BoostingQuery part.

Regards,

Olivier.

Le jeudi 14 mars 2013 07:56:57 UTC+1, David Pilato a écrit :

> Filters are not used for scoring. They only said 'yes' or 'no'!  
> If you want to use boost, use queries, not filters.
> 
> Does it help?
> 
> --  
> David 😉  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> Le 13 mars 2013 à 22:40, Amit Patel \<[avpat...@gmail.com](mailto:avpat...@gmail.com) \<javascript:\>\> a  
> écrit :
> 
> Hi,  
> I am new to ES and trying to search using java apis. I am unable to figure  
> out how I can provide filed specific boosting using the java apis.  
> Here is the example:  
> My index document looks like:
> 
> \_source": {
> 
> - "th\_id": 1,
> - "th\_name": "test name",
> - "th\_description": "test desc",
> - "th\_image": "test-img",
> - "th\_slug": "Make-Me-Smart",
> - "th\_show\_title": "Coast Tech Podcast",
> - "th\_sh\_category": "Alternative Health
> 
> }
> 
> When i search for keywords I want to boost the results higher if they  
> found in the "th\_name" compared to they're found in some other fields.  
> Currently I am using below code to do search:  
> QueryBuilder qb1 = QueryBuilders.matchAllQuery();  
> FilterBuilder fb = FilterBuilders.orFilter(  
> FilterBuilders.termFilter("th\_name", keyword),  
> FilterBuilders.termFilter("th\_description", keyword),  
> FilterBuilders.termFilter("th\_show\_title", keyword),  
> FilterBuilders.termFilter("th\_sh\_category", keyword)  
> );
> 
> ```
> // WildcardQueryBuilder wqb = new WildcardQueryBuilder(name, 
> 
> ```
> 
> wildcard)  
> log.info("Executing Query: " + qb1.toString());  
> SearchResponse response =  
> client.prepareSearch("talk").setTypes("themes")
> 
> .setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH).setQuery(qb1).setFilter(fb)  
> .setFrom(start).setSize(maxRows)  
> .setExplain(true).execute().actionGet();
> 
> Is there anything I can do at query time to boost the document if the  
> keyword is found in "th\_name" field compared to found in other fields?
> 
> --  
> 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:** ![Amit\_Patel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amit_patel/32/2323_2.png) [@Amit\_Patel](https://discuss.elastic.co/u/Amit_Patel)\
**Post date:** [March 14, 2013, 7:52pm UTC](https://discuss.elastic.co/t/boosting-results-at-query-time-using-java-api/11143/4 "2013-03-14T19:52:05Z")

</div>

Thanks David and Olivier for your help.  
I've changed my query builder to :  
QueryBuilder qb1 = QueryBuilders.multiMatchQuery(keyword, "th\_name",  
"th\_description", "th\_show\_title", "th\_sh\_category");

And now its only searching through the given list of fileds.

And I guess I would have to change the query builder to :  
QueryBuilder qb2 =  
QueryBuilders.boostingQuery().positive(QueryBuilders.termQuery("th\_name",  
keyword))  
.negative(QueryBuilders.multiMatchQuery(keyword,  
"th\_description", "th\_show\_title", "th\_sh\_category")).negativeBoost(0.2f);

to make the boosting work.

On Thursday, March 14, 2013 9:23:25 AM UTC-4, Olivier Girardot wrote:

> Hi,
> 
> David is right, and the java api documentation shows you exactly how to do  
> that :  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/java-api/query-dsl-queries.html) in  
> the BoostingQuery part.
> 
> Regards,
> 
> Olivier.
> 
> Le jeudi 14 mars 2013 07:56:57 UTC+1, David Pilato a écrit :
> 
> > Filters are not used for scoring. They only said 'yes' or 'no'!  
> > If you want to use boost, use queries, not filters.
> > 
> > Does it help?
> > 
> > --  
> > David 😉  
> > Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> > 
> > Le 13 mars 2013 à 22:40, Amit Patel [avpat...@gmail.com](mailto:avpat...@gmail.com) a écrit :
> > 
> > Hi,  
> > I am new to ES and trying to search using java apis. I am unable to  
> > figure out how I can provide filed specific boosting using the java apis.  
> > Here is the example:  
> > My index document looks like:
> > 
> > \_source": {
> > 
> > - "th\_id": 1,
> > - "th\_name": "test name",
> > - "th\_description": "test desc",
> > - "th\_image": "test-img",
> > - "th\_slug": "Make-Me-Smart",
> > - "th\_show\_title": "Coast Tech Podcast",
> > - "th\_sh\_category": "Alternative Health
> > 
> > }
> > 
> > When i search for keywords I want to boost the results higher if they  
> > found in the "th\_name" compared to they're found in some other fields.  
> > Currently I am using below code to do search:  
> > QueryBuilder qb1 = QueryBuilders.matchAllQuery();  
> > FilterBuilder fb = FilterBuilders.orFilter(  
> > FilterBuilders.termFilter("th\_name", keyword),  
> > FilterBuilders.termFilter("th\_description", keyword),  
> > FilterBuilders.termFilter("th\_show\_title", keyword),  
> > FilterBuilders.termFilter("th\_sh\_category", keyword)  
> > );
> > 
> > ```
> > // WildcardQueryBuilder wqb = new WildcardQueryBuilder(name, 
> > 
> > ```
> > 
> > wildcard)  
> > log.info("Executing Query: " + qb1.toString());  
> > SearchResponse response =  
> > client.prepareSearch("talk").setTypes("themes")
> > 
> > .setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH).setQuery(qb1).setFilter(fb)  
> > .setFrom(start).setSize(maxRows)  
> > .setExplain(true).execute().actionGet();
> > 
> > Is there anything I can do at query time to boost the document if the  
> > keyword is found in "th\_name" field compared to found in other fields?
> > 
> > --  
> > 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).  
> > 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:46am UTC](https://discuss.elastic.co/t/boosting-results-at-query-time-using-java-api/11143/5 "2017-07-06T02:46:22Z")

</div>


