# Sort by document boost

**URL:** <https://discuss.elastic.co/t/sort-by-document-boost/13641>\
**Category:** Elasticsearch\
**Created:** [September 17, 2013, 9:53am UTC](https://discuss.elastic.co/t/sort-by-document-boost/13641 "2013-09-17T09:53:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hannes\_Korte](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hannes_korte/32/909_2.png) [@Hannes\_Korte](https://discuss.elastic.co/u/Hannes_Korte)\
**Post date:** [September 17, 2013, 9:53am UTC](https://discuss.elastic.co/t/sort-by-document-boost/13641/1 "2013-09-17T09:53:03Z")

</div>

Hi all,

I have an index where every document has a certain document boost based  
on some document importance score. With the usual search everything  
works fine. But if the user browses the content using only filters  
without a query string, I want the result to be sorted by the boost.

The match all query ignores the boost value as it assigns a constant  
score to each document. Adding a sort desc on the boost field results in  
undefined ordering with "-Infinity" as sort values.

I noticed that defining a field as a boost field removes the other  
field's properties in the mapping. In this case being a float value.

[https://gist.github.com/hkorte/6592245](https://gist.github.com/hkorte/6592245)

The simple solution is obviously to add this field twice (one as a boost  
and one for sorting). But is there a more elegant solution? I would like  
to avoid having this duplicated field in my model files. I already tried  
using a multi field to no avail.

[https://gist.github.com/hkorte/6592285](https://gist.github.com/hkorte/6592285)

Any ideas?

Best regards,  
Hannes

--  
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:** [September 18, 2013, 2:26am UTC](https://discuss.elastic.co/t/sort-by-document-boost/13641/2 "2013-09-18T02:26:23Z")

</div>

I submitted a pull request to provide this functionality a while ago, but  
it was ignored by the elasticsearch team:

> <https://github.com/elastic/elasticsearch/pull/2913>
>
> Currently there is no way to change the default behavior of boost fields being n…ot indexed and not stored. If a boosted field can be also indexed, then it can be sorted/faceted on if there is a use case that requires it. This change will make boost fields behave like the timestamp field, which can be indexed and stored.
> 
> I know that Shay is not a fan of document-level boosts and in fact, Lucene 4 has gotten rid of them. But the boost field feature is there, so it would be great to use it to its full potential.

I use the two field solution currently.

Cheers,

Ivan

On Tue, Sep 17, 2013 at 2:54 AM, Hannes Korte [email@hkorte.com](mailto:email@hkorte.com) wrote:

> Hi all,
> 
> I have an index where every document has a certain document boost based on  
> some document importance score. With the usual search everything works  
> fine. But if the user browses the content using only filters without a  
> query string, I want the result to be sorted by the boost.
> 
> The match all query ignores the boost value as it assigns a constant score  
> to each document. Adding a sort desc on the boost field results in  
> undefined ordering with "-Infinity" as sort values.
> 
> I noticed that defining a field as a boost field removes the other field's  
> properties in the mapping. In this case being a float value.
> 
> [https://gist.github.com/\*\*hkorte/6592245](https://gist.github.com/**hkorte/6592245)[https://gist.github.com/hkorte/6592245](https://gist.github.com/hkorte/6592245)
> 
> The simple solution is obviously to add this field twice (one as a boost  
> and one for sorting). But is there a more elegant solution? I would like to  
> avoid having this duplicated field in my model files. I already tried using  
> a multi field to no avail.
> 
> [https://gist.github.com/\*\*hkorte/6592285](https://gist.github.com/**hkorte/6592285)[https://gist.github.com/hkorte/6592285](https://gist.github.com/hkorte/6592285)
> 
> Any ideas?
> 
> Best regards,  
> Hannes
> 
> --  
> 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](http://googlegroups.com)[elasticsearch%2Bunsubscribe@googlegroups.com](mailto:elasticsearch%2Bunsubscribe@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).

---

<div class="post-metadata">

**Author:** ![Britta\_Weber](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/britta_weber/32/1113_2.png) [@Britta\_Weber](https://discuss.elastic.co/u/Britta_Weber)\
**Post date:** [September 18, 2013, 9:25pm UTC](https://discuss.elastic.co/t/sort-by-document-boost/13641/3 "2013-09-18T21:25:12Z")

</div>

Hi,

it is possible to access the \_boost field with a script. The query  
would look like this:

{  
"query": {  
"function\_score": {  
"script\_score": {  
"script": "\_source.\_boost"  
},  
"boost\_mode":"replace"  
}  
}  
}

Alternatively, you could use the custom\_score:

{  
"query": {  
"custom\_score": {  
"query": {  
"match\_all": {}  
},  
"script": "\_source.\_boost"  
}  
}  
}

Is this what you need?

Cheers,  
Britta

---------- Forwarded message ----------  
From: Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com)  
Date: Wed, Sep 18, 2013 at 4:26 AM  
Subject: Re: Sort by document boost  
To: [elasticsearch@googlegroups.com](mailto:elasticsearch@googlegroups.com)

I submitted a pull request to provide this functionality a while ago,  
but it was ignored by the elasticsearch team:

> <https://github.com/elastic/elasticsearch/pull/2913>
>
> Currently there is no way to change the default behavior of boost fields being n…ot indexed and not stored. If a boosted field can be also indexed, then it can be sorted/faceted on if there is a use case that requires it. This change will make boost fields behave like the timestamp field, which can be indexed and stored.
> 
> I know that Shay is not a fan of document-level boosts and in fact, Lucene 4 has gotten rid of them. But the boost field feature is there, so it would be great to use it to its full potential.

I use the two field solution currently.

Cheers,

Ivan

On Tue, Sep 17, 2013 at 2:54 AM, Hannes Korte [email@hkorte.com](mailto:email@hkorte.com) wrote:

> Hi all,
> 
> I have an index where every document has a certain document boost based on some document importance score. With the usual search everything works fine. But if the user browses the content using only filters without a query string, I want the result to be sorted by the boost.
> 
> The match all query ignores the boost value as it assigns a constant score to each document. Adding a sort desc on the boost field results in undefined ordering with "-Infinity" as sort values.
> 
> I noticed that defining a field as a boost field removes the other field's properties in the mapping. In this case being a float value.
> 
> [Defining a field as boost removes this field's properties. Thus, it is impossible to sort on the boost field. · GitHub](https://gist.github.com/hkorte/6592245)
> 
> The simple solution is obviously to add this field twice (one as a boost and one for sorting). But is there a more elegant solution? I would like to avoid having this duplicated field in my model files. I already tried using a multi field to no avail.
> 
> [Defining a field as boost removes this field's properties. Thus, it is impossible to sort on the boost field. The last search with sorting the bothdocs shows the problem with "sort" : ["-Infinity"]. · GitHub](https://gist.github.com/hkorte/6592285)
> 
> Any ideas?
> 
> Best regards,  
> Hannes
> 
> --  
> 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).

--  
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:** ![simonw\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simonw_2/32/1130_2.png) [@simonw\_2](https://discuss.elastic.co/u/simonw_2)\
**Post date:** [September 19, 2013, 7:23am UTC](https://discuss.elastic.co/t/sort-by-document-boost/13641/4 "2013-09-19T07:23:16Z")

</div>

I think all of the solutions here are working. Yet, solution with the  
script and the source will likely suffer from loading the source for each  
document. The purpose if the \_boost field is to assign the boost that is  
set to the field to every field in the document to be available to the  
lucene scoring. @Ivan, I don't think we ignored your PR it somehow got lost  
I guess. I think @mvg is looking into it and we are discussing solutions  
internally. Sorry for not responding on it, please feel free to ping on  
issues like this once in a while if they don't get attention.

simon

On Wednesday, September 18, 2013 11:25:12 PM UTC+2, Britta Weber wrote:

> Hi,
> 
> it is possible to access the \_boost field with a script. The query  
> would look like this:
> 
> {  
> "query": {  
> "function\_score": {  
> "script\_score": {  
> "script": "\_source.\_boost"  
> },  
> "boost\_mode":"replace"  
> }  
> }  
> }
> 
> Alternatively, you could use the custom\_score:
> 
> {  
> "query": {  
> "custom\_score": {  
> "query": {  
> "match\_all": {}  
> },  
> "script": "\_source.\_boost"  
> }  
> }  
> }
> 
> Is this what you need?
> 
> Cheers,  
> Britta
> 
> ---------- Forwarded message ----------  
> From: Ivan Brusic \<[iv...@brusic.com](mailto:iv...@brusic.com) \<javascript:\>\>  
> Date: Wed, Sep 18, 2013 at 4:26 AM  
> Subject: Re: Sort by document boost  
> To: [elasti...@googlegroups.com](mailto:elasti...@googlegroups.com) \<javascript:\>
> 
> I submitted a pull request to provide this functionality a while ago,  
> but it was ignored by the elasticsearch team:  
> [Allow boost fields to be indexed and stored by brusic · Pull Request #2913 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/pull/2913)
> 
> I use the two field solution currently.
> 
> Cheers,
> 
> Ivan
> 
> On Tue, Sep 17, 2013 at 2:54 AM, Hannes Korte \<[em...@hkorte.com](mailto:em...@hkorte.com)\<javascript:\>\>  
> wrote:
> 
> > Hi all,
> > 
> > I have an index where every document has a certain document boost based  
> > on some document importance score. With the usual search everything works  
> > fine. But if the user browses the content using only filters without a  
> > query string, I want the result to be sorted by the boost.
> > 
> > The match all query ignores the boost value as it assigns a constant  
> > score to each document. Adding a sort desc on the boost field results in  
> > undefined ordering with "-Infinity" as sort values.
> > 
> > I noticed that defining a field as a boost field removes the other  
> > field's properties in the mapping. In this case being a float value.
> > 
> > [Defining a field as boost removes this field's properties. Thus, it is impossible to sort on the boost field. · GitHub](https://gist.github.com/hkorte/6592245)
> > 
> > The simple solution is obviously to add this field twice (one as a boost  
> > and one for sorting). But is there a more elegant solution? I would like to  
> > avoid having this duplicated field in my model files. I already tried using  
> > a multi field to no avail.
> > 
> > [Defining a field as boost removes this field's properties. Thus, it is impossible to sort on the boost field. The last search with sorting the bothdocs shows the problem with "sort" : ["-Infinity"]. · GitHub](https://gist.github.com/hkorte/6592285)
> > 
> > Any ideas?
> > 
> > Best regards,  
> > Hannes
> > 
> > --  
> > 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 [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:** ![Hannes\_Korte](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hannes_korte/32/909_2.png) [@Hannes\_Korte](https://discuss.elastic.co/u/Hannes_Korte)\
**Post date:** [September 19, 2013, 12:07pm UTC](https://discuss.elastic.co/t/sort-by-document-boost/13641/5 "2013-09-19T12:07:31Z")

</div>

Hi Ivan,

On 18.09.2013 04:26, Ivan Brusic wrote:

> I submitted a pull request to provide this functionality a while ago, but  
> it was ignored by the elasticsearch team:  
> [Allow boost fields to be indexed and stored by brusic · Pull Request #2913 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/pull/2913)

Interesting. Good to know, that I am not the only one having this  
problem.. 🙂

> I use the two field solution currently.

Yes, me too. I think this is the best solution so far. To avoid  
duplicating the boost field in my model files, I simply added this line  
after JSON serialization:

str = str.substring(0, str.length() - 1) + ","\_boost":" + boost + "}";

Not beautiful, but simple.. and it works.

Hannes

> Cheers,
> 
> Ivan
> 
> On Tue, Sep 17, 2013 at 2:54 AM, Hannes Korte [email@hkorte.com](mailto:email@hkorte.com) wrote:
> 
> > Hi all,
> > 
> > I have an index where every document has a certain document boost based on  
> > some document importance score. With the usual search everything works  
> > fine. But if the user browses the content using only filters without a  
> > query string, I want the result to be sorted by the boost.
> > 
> > The match all query ignores the boost value as it assigns a constant score  
> > to each document. Adding a sort desc on the boost field results in  
> > undefined ordering with "-Infinity" as sort values.
> > 
> > I noticed that defining a field as a boost field removes the other field's  
> > properties in the mapping. In this case being a float value.
> > 
> > [https://gist.github.com/\*\*hkorte/6592245](https://gist.github.com/**hkorte/6592245)[https://gist.github.com/hkorte/6592245](https://gist.github.com/hkorte/6592245)
> > 
> > The simple solution is obviously to add this field twice (one as a boost  
> > and one for sorting). But is there a more elegant solution? I would like to  
> > avoid having this duplicated field in my model files. I already tried using  
> > a multi field to no avail.
> > 
> > [https://gist.github.com/\*\*hkorte/6592285](https://gist.github.com/**hkorte/6592285)[https://gist.github.com/hkorte/6592285](https://gist.github.com/hkorte/6592285)
> > 
> > Any ideas?
> > 
> > Best regards,  
> > Hannes
> > 
> > --  
> > 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](http://googlegroups.com)[elasticsearch%2Bunsubscribe@googlegroups.com](mailto:elasticsearch%2Bunsubscribe@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).

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [September 19, 2013, 3:38pm UTC](https://discuss.elastic.co/t/sort-by-document-boost/13641/6 "2013-09-19T15:38:40Z")

</div>

Hi Hannes,

Once PR 2913 gets in, you can just set the 'index' option to 'not\_analyzed'  
for the '\_boost' field and then you should be able to sort by it.

Martijn

On 19 September 2013 14:08, Hannes Korte [email@hkorte.com](mailto:email@hkorte.com) wrote:

> Hi Ivan,
> 
> On 18.09.2013 04:26, Ivan Brusic wrote:
> 
> > I submitted a pull request to provide this functionality a while ago, but  
> > it was ignored by the elasticsearch team:  
> > [https://github.com/\*\*elasticsearch/elasticsearch/\*\*pull/2913](https://github.com/ **elasticsearch/elasticsearch/** pull/2913)[https://github.com/elasticsearch/elasticsearch/pull/2913](https://github.com/elasticsearch/elasticsearch/pull/2913)
> 
> Interesting. Good to know, that I am not the only one having this  
> problem.. 🙂
> 
> I use the two field solution currently.
> 
> > 
> 
> Yes, me too. I think this is the best solution so far. To avoid  
> duplicating the boost field in my model files, I simply added this line  
> after JSON serialization:
> 
> str = str.substring(0, str.length() - 1) + ","\_boost":" + boost + "}";
> 
> Not beautiful, but simple.. and it works.
> 
> Hannes
> 
> > Cheers,
> > 
> > Ivan
> > 
> > On Tue, Sep 17, 2013 at 2:54 AM, Hannes Korte [email@hkorte.com](mailto:email@hkorte.com) wrote:
> > 
> > Hi all,
> > 
> > > I have an index where every document has a certain document boost based  
> > > on  
> > > some document importance score. With the usual search everything works  
> > > fine. But if the user browses the content using only filters without a  
> > > query string, I want the result to be sorted by the boost.
> > > 
> > > The match all query ignores the boost value as it assigns a constant  
> > > score  
> > > to each document. Adding a sort desc on the boost field results in  
> > > undefined ordering with "-Infinity" as sort values.
> > > 
> > > I noticed that defining a field as a boost field removes the other  
> > > field's  
> > > properties in the mapping. In this case being a float value.
> > > 
> > > [https://gist.github.com/\*\*\*\*hkorte/6592245](https://gist.github.com/****hkorte/6592245)[https://gist.github.com/\*\*hkorte/6592245](https://gist.github.com/**hkorte/6592245)  
> > > \<[https://gist](https://gist).\*\*[github.com/hkorte/6592245](http://github.com/hkorte/6592245)[https://gist.github.com/hkorte/6592245](https://gist.github.com/hkorte/6592245)
> > > 
> > > > 
> > > 
> > > The simple solution is obviously to add this field twice (one as a boost  
> > > and one for sorting). But is there a more elegant solution? I would like  
> > > to  
> > > avoid having this duplicated field in my model files. I already tried  
> > > using  
> > > a multi field to no avail.
> > > 
> > > [https://gist.github.com/\*\*\*\*hkorte/6592285](https://gist.github.com/****hkorte/6592285)[https://gist.github.com/\*\*hkorte/6592285](https://gist.github.com/**hkorte/6592285)  
> > > \<[https://gist](https://gist).\*\*[github.com/hkorte/6592285](http://github.com/hkorte/6592285)[https://gist.github.com/hkorte/6592285](https://gist.github.com/hkorte/6592285)
> > > 
> > > > 
> > > 
> > > Any ideas?
> > > 
> > > Best regards,  
> > > Hannes
> > > 
> > > --  
> > > 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@ **go** [oglegroups.com](http://oglegroups.com)[http://googlegroups.com](http://googlegroups.com)  
> > > \<elasticsearch%\*\*2Bunsubscribe@googlegroups.com[elasticsearch%252Bunsubscribe@googlegroups.com](mailto:elasticsearch%252Bunsubscribe@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)  
> > > \<[https://groups](https://groups).\*\*[google.com/groups/opt\_out](http://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](http://googlegroups.com)[elasticsearch%2Bunsubscribe@googlegroups.com](mailto:elasticsearch%2Bunsubscribe@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)  
> .

--  
Met vriendelijke groet,

Martijn van Groningen

--  
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:** [September 19, 2013, 4:41pm UTC](https://discuss.elastic.co/t/sort-by-document-boost/13641/7 "2013-09-19T16:41:08Z")

</div>

The workaround is simple and only has a minuscule affect on space, so the  
pull request was not really important. For me, the change was more  
important so that the functionality of boost field would be consistent with  
the timestamp field. If the pull request gets accepted, I guess we will  
have eventual consistency. :rimshot:

Eventually, I need to move from document boosts to using a custom score  
such as the one Britta mentioned. There is a loss of precision when the  
boost gets encoded in the field norms and I also need to provided dynamic  
boosts based on different fields. Easy to code (already did months ago),  
but since there is some performance impact, I need to benchmark it, which I  
do not have time for.

My git-fu skills have deteriorated since I have been stuck in Perforce-land  
for two years. Sorry about the messy commits, will fix soon. It is ironic  
since I consistently complain about not have a VCS with lightweight  
branching and here I am messing up branching. 🙂

Cheers,

Ivan

On Thu, Sep 19, 2013 at 8:38 AM, Martijn v Groningen \<  
[martijn.v.groningen@gmail.com](mailto:martijn.v.groningen@gmail.com)\> wrote:

> Hi Hannes,
> 
> Once PR 2913 gets in, you can just set the 'index' option to  
> 'not\_analyzed' for the '\_boost' field and then you should be able to sort  
> by it.
> 
> Martijn
> 
> On 19 September 2013 14:08, Hannes Korte [email@hkorte.com](mailto:email@hkorte.com) wrote:
> 
> > Hi Ivan,
> > 
> > On 18.09.2013 04:26, Ivan Brusic wrote:
> > 
> > > I submitted a pull request to provide this functionality a while ago, but  
> > > it was ignored by the elasticsearch team:  
> > > [https://github.com/\*\*elasticsearch/elasticsearch/\*\*pull/2913](https://github.com/ **elasticsearch/elasticsearch/** pull/2913)[https://github.com/elasticsearch/elasticsearch/pull/2913](https://github.com/elasticsearch/elasticsearch/pull/2913)
> > 
> > Interesting. Good to know, that I am not the only one having this  
> > problem.. 🙂
> > 
> > I use the two field solution currently.
> > 
> > > 
> > 
> > Yes, me too. I think this is the best solution so far. To avoid  
> > duplicating the boost field in my model files, I simply added this line  
> > after JSON serialization:
> > 
> > str = str.substring(0, str.length() - 1) + ","\_boost":" + boost + "}";
> > 
> > Not beautiful, but simple.. and it works.
> > 
> > Hannes
> > 
> > > Cheers,
> > > 
> > > Ivan
> > > 
> > > On Tue, Sep 17, 2013 at 2:54 AM, Hannes Korte [email@hkorte.com](mailto:email@hkorte.com) wrote:
> > > 
> > > Hi all,
> > > 
> > > > I have an index where every document has a certain document boost based  
> > > > on  
> > > > some document importance score. With the usual search everything works  
> > > > fine. But if the user browses the content using only filters without a  
> > > > query string, I want the result to be sorted by the boost.
> > > > 
> > > > The match all query ignores the boost value as it assigns a constant  
> > > > score  
> > > > to each document. Adding a sort desc on the boost field results in  
> > > > undefined ordering with "-Infinity" as sort values.
> > > > 
> > > > I noticed that defining a field as a boost field removes the other  
> > > > field's  
> > > > properties in the mapping. In this case being a float value.
> > > > 
> > > > [https://gist.github.com/\*\*\*\*hkorte/6592245](https://gist.github.com/****hkorte/6592245)[https://gist.github.com/\*\*hkorte/6592245](https://gist.github.com/**hkorte/6592245)  
> > > > \<[https://gist](https://gist).\*\*[github.com/hkorte/6592245](http://github.com/hkorte/6592245)[https://gist.github.com/hkorte/6592245](https://gist.github.com/hkorte/6592245)
> > > > 
> > > > > 
> > > > 
> > > > The simple solution is obviously to add this field twice (one as a boost  
> > > > and one for sorting). But is there a more elegant solution? I would  
> > > > like to  
> > > > avoid having this duplicated field in my model files. I already tried  
> > > > using  
> > > > a multi field to no avail.
> > > > 
> > > > [https://gist.github.com/\*\*\*\*hkorte/6592285](https://gist.github.com/****hkorte/6592285)[https://gist.github.com/\*\*hkorte/6592285](https://gist.github.com/**hkorte/6592285)  
> > > > \<[https://gist](https://gist).\*\*[github.com/hkorte/6592285](http://github.com/hkorte/6592285)[https://gist.github.com/hkorte/6592285](https://gist.github.com/hkorte/6592285)
> > > > 
> > > > > 
> > > > 
> > > > Any ideas?
> > > > 
> > > > Best regards,  
> > > > Hannes
> > > > 
> > > > --  
> > > > 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@ **go** [oglegroups.com](http://oglegroups.com)[http://googlegroups.com](http://googlegroups.com)  
> > > > \<elasticsearch%\*\*2Bunsubscribe@googlegroups.com[elasticsearch%252Bunsubscribe@googlegroups.com](mailto:elasticsearch%252Bunsubscribe@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)  
> > > > \<[https://groups](https://groups).\*\*[google.com/groups/opt\_out](http://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](http://googlegroups.com)[elasticsearch%2Bunsubscribe@googlegroups.com](mailto:elasticsearch%2Bunsubscribe@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)  
> > .
> 
> --  
> Met vriendelijke groet,
> 
> Martijn van Groningen
> 
> --  
> 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:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [September 20, 2013, 5:44pm UTC](https://discuss.elastic.co/t/sort-by-document-boost/13641/8 "2013-09-20T17:44:46Z")

</div>

Thanks to Martijn for pushing out the change. I think that means it is time  
to finally abandon document boost and switch to custom scoring. 🙂

> <https://github.com/elastic/elasticsearch/issues/3752>
>
> The idea of the \`\_boost\` field is that it provides an index time boost for all f…ields a document has, because of this the \`\_boost\` field isn't stored or indexed separately and only saved as part of the \`\_source\`.
> 
> However the \`\_boost\` field is also used to as just a sort field, but because it can't be indexed separately, this doesn't work. By allowing to make the \`\_boost\` indexable separately, sorting by the \`\_boost\` field does work. By default the \`\_boost\` field remains not indexed and not stored.
> 
> This issue originates from PR #2913

On Thu, Sep 19, 2013 at 9:41 AM, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:

> The workaround is simple and only has a minuscule affect on space, so the  
> pull request was not really important. For me, the change was more  
> important so that the functionality of boost field would be consistent with  
> the timestamp field. If the pull request gets accepted, I guess we will  
> have eventual consistency. :rimshot:
> 
> Eventually, I need to move from document boosts to using a custom score  
> such as the one Britta mentioned. There is a loss of precision when the  
> boost gets encoded in the field norms and I also need to provided dynamic  
> boosts based on different fields. Easy to code (already did months ago),  
> but since there is some performance impact, I need to benchmark it, which I  
> do not have time for.
> 
> My git-fu skills have deteriorated since I have been stuck in  
> Perforce-land for two years. Sorry about the messy commits, will fix soon.  
> It is ironic since I consistently complain about not have a VCS with  
> lightweight branching and here I am messing up branching. 🙂
> 
> Cheers,
> 
> Ivan
> 
> On Thu, Sep 19, 2013 at 8:38 AM, Martijn v Groningen \<  
> [martijn.v.groningen@gmail.com](mailto:martijn.v.groningen@gmail.com)\> wrote:
> 
> > Hi Hannes,
> > 
> > Once PR 2913 gets in, you can just set the 'index' option to  
> > 'not\_analyzed' for the '\_boost' field and then you should be able to sort  
> > by it.
> > 
> > Martijn
> > 
> > On 19 September 2013 14:08, Hannes Korte [email@hkorte.com](mailto:email@hkorte.com) wrote:
> > 
> > > Hi Ivan,
> > > 
> > > On 18.09.2013 04:26, Ivan Brusic wrote:
> > > 
> > > > I submitted a pull request to provide this functionality a while ago,  
> > > > but  
> > > > it was ignored by the elasticsearch team:  
> > > > [https://github.com/\*\*elasticsearch/elasticsearch/\*\*pull/2913](https://github.com/ **elasticsearch/elasticsearch/** pull/2913)[https://github.com/elasticsearch/elasticsearch/pull/2913](https://github.com/elasticsearch/elasticsearch/pull/2913)
> > > 
> > > Interesting. Good to know, that I am not the only one having this  
> > > problem.. 🙂
> > > 
> > > I use the two field solution currently.
> > > 
> > > > 
> > > 
> > > Yes, me too. I think this is the best solution so far. To avoid  
> > > duplicating the boost field in my model files, I simply added this line  
> > > after JSON serialization:
> > > 
> > > str = str.substring(0, str.length() - 1) + ","\_boost":" + boost + "}";
> > > 
> > > Not beautiful, but simple.. and it works.
> > > 
> > > Hannes
> > > 
> > > > Cheers,
> > > > 
> > > > Ivan
> > > > 
> > > > On Tue, Sep 17, 2013 at 2:54 AM, Hannes Korte [email@hkorte.com](mailto:email@hkorte.com) wrote:
> > > > 
> > > > Hi all,
> > > > 
> > > > > I have an index where every document has a certain document boost  
> > > > > based on  
> > > > > some document importance score. With the usual search everything works  
> > > > > fine. But if the user browses the content using only filters without a  
> > > > > query string, I want the result to be sorted by the boost.
> > > > > 
> > > > > The match all query ignores the boost value as it assigns a constant  
> > > > > score  
> > > > > to each document. Adding a sort desc on the boost field results in  
> > > > > undefined ordering with "-Infinity" as sort values.
> > > > > 
> > > > > I noticed that defining a field as a boost field removes the other  
> > > > > field's  
> > > > > properties in the mapping. In this case being a float value.
> > > > > 
> > > > > [https://gist.github.com/\*\*\*\*hkorte/6592245](https://gist.github.com/****hkorte/6592245)[https://gist.github.com/\*\*hkorte/6592245](https://gist.github.com/**hkorte/6592245)  
> > > > > \<[https://gist](https://gist).\*\*[github.com/hkorte/6592245](http://github.com/hkorte/6592245)[https://gist.github.com/hkorte/6592245](https://gist.github.com/hkorte/6592245)
> > > > > 
> > > > > > 
> > > > > 
> > > > > The simple solution is obviously to add this field twice (one as a  
> > > > > boost  
> > > > > and one for sorting). But is there a more elegant solution? I would  
> > > > > like to  
> > > > > avoid having this duplicated field in my model files. I already tried  
> > > > > using  
> > > > > a multi field to no avail.
> > > > > 
> > > > > [https://gist.github.com/\*\*\*\*hkorte/6592285](https://gist.github.com/****hkorte/6592285)[https://gist.github.com/\*\*hkorte/6592285](https://gist.github.com/**hkorte/6592285)  
> > > > > \<[https://gist](https://gist).\*\*[github.com/hkorte/6592285](http://github.com/hkorte/6592285)[https://gist.github.com/hkorte/6592285](https://gist.github.com/hkorte/6592285)
> > > > > 
> > > > > > 
> > > > > 
> > > > > Any ideas?
> > > > > 
> > > > > Best regards,  
> > > > > Hannes
> > > > > 
> > > > > --  
> > > > > 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@ **go** [oglegroups.com](http://oglegroups.com)[http://googlegroups.com](http://googlegroups.com)  
> > > > > \<elasticsearch%\*\*2Bunsubscribe@googlegroups.com[elasticsearch%252Bunsubscribe@googlegroups.com](mailto:elasticsearch%252Bunsubscribe@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)  
> > > > > \<[https://groups](https://groups).\*\*[google.com/groups/opt\_out](http://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](http://googlegroups.com)[elasticsearch%2Bunsubscribe@googlegroups.com](mailto:elasticsearch%2Bunsubscribe@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)  
> > > .
> > 
> > --  
> > Met vriendelijke groet,
> > 
> > Martijn van Groningen
> > 
> > --  
> > 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:15am UTC](https://discuss.elastic.co/t/sort-by-document-boost/13641/9 "2017-07-06T02:15:20Z")

</div>


