# How to custom score (score based on a date field)?

**URL:** <https://discuss.elastic.co/t/how-to-custom-score-score-based-on-a-date-field/2971>\
**Category:** Elasticsearch\
**Created:** [May 25, 2010, 7:29pm UTC](https://discuss.elastic.co/t/how-to-custom-score-score-based-on-a-date-field/2971 "2010-05-25T19:29:47Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Chang](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@John\_Chang](https://discuss.elastic.co/u/John_Chang)\
**Post date:** [May 25, 2010, 7:29pm UTC](https://discuss.elastic.co/t/how-to-custom-score-score-based-on-a-date-field/2971/1 "2010-05-25T19:29:47Z")

</div>

I'd like to have documents come back sorted by a date saved in my doc. So if I ask for the top 100 hits, I want the 100 most recent according to a doc field, not according to the time time of indexing. I did find the discussion titled "Recency" in this forum which suggested it might be coming (in April 2010 kimchy wrote he intended to add it).

Is it in 0.7.1 by chance? If not, do you know when it might come? Any suggestions in the meantime for how to get this to work?

Thanks for your time,  
John

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [May 26, 2010, 6:28am UTC](https://discuss.elastic.co/t/how-to-custom-score-score-based-on-a-date-field/2971/2 "2010-05-26T06:28:02Z")

</div>

I am not really sure I understand, why can't you use sorting?

On Tue, May 25, 2010 at 10:29 PM, John Chang [jchangkihtest2@gmail.com](mailto:jchangkihtest2@gmail.com)wrote:

> I'd like to have documents come back sorted by a date saved in my doc. So  
> if  
> I ask for the top 100 hits, I want the 100 most recent according to a doc  
> field, not according to the time time of indexing. I did find the  
> discussion titled "Recency" in this forum which suggested it might be  
> coming  
> (in April 2010 kimchy wrote he intended to add it).
> 
> Is it in 0.7.1 by chance? If not, do you know when it might come? Any  
> suggestions in the meantime for how to get this to work?
> 
> ## Thanks for your time, John
> 
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/How-to-custom-score-score-based-on-a-date-field-tp842971p842971.html](http://elasticsearch-users.115913.n3.nabble.com/How-to-custom-score-score-based-on-a-date-field-tp842971p842971.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [May 26, 2010, 10:43am UTC](https://discuss.elastic.co/t/how-to-custom-score-score-based-on-a-date-field/2971/3 "2010-05-26T10:43:15Z")

</div>

On Wed, 2010-05-26 at 09:28 +0300, Shay Banon wrote:

> I am not really sure I understand, why can't you use sorting?

I think this relates to my previous thread about using 'recency' as part  
of the relevance weighting.

So a search for Joe Bloggs should give more weight to a document created  
recently, than one a year old.

clint

> On Tue, May 25, 2010 at 10:29 PM, John Chang  
> [jchangkihtest2@gmail.com](mailto:jchangkihtest2@gmail.com) wrote:
> 
> ```
> I'd like to have documents come back sorted by a date saved in
> my doc. So if
> I ask for the top 100 hits, I want the 100 most recent
> according to a doc
> field, not according to the time time of indexing. I did find
> the
> discussion titled "Recency" in this forum which suggested it
> might be coming
> (in April 2010 kimchy wrote he intended to add it).
>     
> Is it in 0.7.1 by chance? If not, do you know when it might
> come? Any
> suggestions in the meantime for how to get this to work?
>     
> Thanks for your time,
> John
> --
> View this message in context:
> http://elasticsearch-users.115913.n3.nabble.com/How-to-custom-score-score-based-on-a-date-field-tp842971p842971.html
> Sent from the ElasticSearch Users mailing list archive at
> Nabble.com.
> 
> ```

--  
Web Announcements Limited is a company registered in England and Wales,  
with company number 05608868, with registered address at 10 Arvon Road,  
London, N5 1PR.

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [May 26, 2010, 11:22am UTC](https://discuss.elastic.co/t/how-to-custom-score-score-based-on-a-date-field/2971/4 "2010-05-26T11:22:15Z")

</div>

If it does, then this is not yet in elasticsearch. Was just wondering if the  
question is more basic about sorting.

On Wed, May 26, 2010 at 1:43 PM, Clinton Gormley [clinton@iannounce.co.uk](mailto:clinton@iannounce.co.uk)wrote:

> On Wed, 2010-05-26 at 09:28 +0300, Shay Banon wrote:
> 
> > I am not really sure I understand, why can't you use sorting?
> 
> I think this relates to my previous thread about using 'recency' as part  
> of the relevance weighting.
> 
> So a search for Joe Bloggs should give more weight to a document created  
> recently, than one a year old.
> 
> clint
> 
> > On Tue, May 25, 2010 at 10:29 PM, John Chang  
> > [jchangkihtest2@gmail.com](mailto:jchangkihtest2@gmail.com) wrote:
> > 
> > ```
> > I'd like to have documents come back sorted by a date saved in
> > my doc. So if
> > I ask for the top 100 hits, I want the 100 most recent
> > according to a doc
> > field, not according to the time time of indexing. I did find
> > the
> > discussion titled "Recency" in this forum which suggested it
> > might be coming
> > (in April 2010 kimchy wrote he intended to add it).
> > 
> > Is it in 0.7.1 by chance? If not, do you know when it might
> > come? Any
> > suggestions in the meantime for how to get this to work?
> > 
> > Thanks for your time,
> > John
> > --
> > View this message in context:
> > 
> > ```
> 
> [http://elasticsearch-users.115913.n3.nabble.com/How-to-custom-score-score-based-on-a-date-field-tp842971p842971.html](http://elasticsearch-users.115913.n3.nabble.com/How-to-custom-score-score-based-on-a-date-field-tp842971p842971.html)
> 
> > ```
> > Sent from the ElasticSearch Users mailing list archive at
> > Nabble.com.
> > 
> > ```
> 
> --  
> Web Announcements Limited is a company registered in England and Wales,  
> with company number 05608868, with registered address at 10 Arvon Road,  
> London, N5 1PR.

---

<div class="post-metadata">

**Author:** ![John\_Chang](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@John\_Chang](https://discuss.elastic.co/u/John_Chang)\
**Post date:** [May 26, 2010, 6:55pm UTC](https://discuss.elastic.co/t/how-to-custom-score-score-based-on-a-date-field/2971/5 "2010-05-26T18:55:00Z")

</div>

Sounds like it is not yet in Elastic Search. But, for the record, it is _related_ to recency, but not exactly recency as discussed above. I want to sort based on a datetime field in the document, not on the datetime it was indexed. Say, sorting by birthdate when the order of indexing is not related to order of birth. So, the most recently indexed document is not necessarily the one with the most "recency" as concerns the query.

---

<div class="post-metadata">

**Author:** ![John\_Chang](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@John\_Chang](https://discuss.elastic.co/u/John_Chang)\
**Post date:** [June 2, 2010, 6:05pm UTC](https://discuss.elastic.co/t/how-to-custom-score-score-based-on-a-date-field/2971/6 "2010-06-02T18:05:46Z")

</div>

Kimchy,

We have been testing out Elastic Search and are very impressed. However, we do need custom scoring based on a specific document date field; this is a must-have for V1 for us. We have a custom scoring solution we like with our straight-lucene (single box) setup and are now looking into how we could integrate that into Elastic Search. Ideally, we would like to make it a flexible solution that could be pulled into the master.

I didn't see any pulls into master for elasticsearch. Is this something you are open to?

Thanks for your time,

John Chang

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [June 2, 2010, 8:35pm UTC](https://discuss.elastic.co/t/how-to-custom-score-score-based-on-a-date-field/2971/7 "2010-06-02T20:35:38Z")

</div>

First, I am very much open for contributions. But, I am working as I type on  
building the infra to support custom scoring, dynamic facets, range facets,  
bucket facets, custom return fields, and map reduce (thats a mouthful 🙂 ).  
It all revolves around the same infra.

I hope that within several days, you will see something.

-shay.banon

On Wed, Jun 2, 2010 at 9:05 PM, John Chang [jchangkihtest2@gmail.com](mailto:jchangkihtest2@gmail.com) wrote:

> Kimchy,
> 
> We have been testing out Elastic Search and are very impressed. However,  
> we  
> do need custom scoring based on a specific document date field; this is a  
> must-have for V1 for us. We have a custom scoring solution we like with  
> our  
> straight-lucene (single box) setup and are now looking into how we could  
> integrate that into Elastic Search. Ideally, we would like to make it a  
> flexible solution that could be pulled into the master.
> 
> I didn't see any pulls into master for elasticsearch. Is this something  
> you  
> are open to?
> 
> Thanks for your time,
> 
> ## John Chang
> 
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/How-to-custom-score-score-based-on-a-date-field-tp842971p865496.html](http://elasticsearch-users.115913.n3.nabble.com/How-to-custom-score-score-based-on-a-date-field-tp842971p865496.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [June 2, 2010, 8:36pm UTC](https://discuss.elastic.co/t/how-to-custom-score-score-based-on-a-date-field/2971/8 "2010-06-02T20:36:18Z")

</div>

As a side note, is your custom implementation revolves around Lucene built  
in function queries?

On Wed, Jun 2, 2010 at 11:35 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)wrote:

> First, I am very much open for contributions. But, I am working as I type  
> on building the infra to support custom scoring, dynamic facets, range  
> facets, bucket facets, custom return fields, and map reduce (thats a  
> mouthful 🙂 ). It all revolves around the same infra.
> 
> I hope that within several days, you will see something.
> 
> -shay.banon
> 
> On Wed, Jun 2, 2010 at 9:05 PM, John Chang [jchangkihtest2@gmail.com](mailto:jchangkihtest2@gmail.com)wrote:
> 
> > Kimchy,
> > 
> > We have been testing out Elastic Search and are very impressed. However,  
> > we  
> > do need custom scoring based on a specific document date field; this is a  
> > must-have for V1 for us. We have a custom scoring solution we like with  
> > our  
> > straight-lucene (single box) setup and are now looking into how we could  
> > integrate that into Elastic Search. Ideally, we would like to make it a  
> > flexible solution that could be pulled into the master.
> > 
> > I didn't see any pulls into master for elasticsearch. Is this something  
> > you  
> > are open to?
> > 
> > Thanks for your time,
> > 
> > ## John Chang
> > 
> > View this message in context:  
> > [http://elasticsearch-users.115913.n3.nabble.com/How-to-custom-score-score-based-on-a-date-field-tp842971p865496.html](http://elasticsearch-users.115913.n3.nabble.com/How-to-custom-score-score-based-on-a-date-field-tp842971p865496.html)  
> > Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![John\_Chang](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@John\_Chang](https://discuss.elastic.co/u/John_Chang)\
**Post date:** [June 3, 2010, 3:41am UTC](https://discuss.elastic.co/t/how-to-custom-score-score-based-on-a-date-field/2971/9 "2010-06-03T03:41:59Z")

</div>

It is based on org.apache.lucene.search.function.FieldScoreQuery and and our subclasses of org.apache.lucene.search.function.CustomScoreQuery org.apache.lucene.search.function.CustomScoreProvider.

We are open to other solutions that work; the final solution doesn't have to be as described above, as long as we can score on a document field of our choosing and it works well.

Thanks -- I'll keep my eyes open for something new coming out soon.

---

<div class="post-metadata">

**Author:** ![John\_Chang](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@John\_Chang](https://discuss.elastic.co/u/John_Chang)\
**Post date:** [June 3, 2010, 3:46am UTC](https://discuss.elastic.co/t/how-to-custom-score-score-based-on-a-date-field/2971/10 "2010-06-03T03:46:01Z")

</div>

Oh, and I should add that although our current solution works acceptably, it is not perfect. We want to sort based on a date field in the document, which is stored as a long (java.util.Date.getTime()). As you probably know, Lucene CustomScoreProvider (and our subclasses) must return scores as a float, so we loose some precision in our scores. It is good enough for now, but it would be good to implement something better before long.

---

<div class="post-metadata">

**Author:** ![John\_Chang](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@John\_Chang](https://discuss.elastic.co/u/John_Chang)\
**Post date:** [June 3, 2010, 6:17pm UTC](https://discuss.elastic.co/t/how-to-custom-score-score-based-on-a-date-field/2971/11 "2010-06-03T18:17:27Z")

</div>

Thanks. Please notify when it is in master, even if it is not yet in release, as we'd be happy to build it and try it out from there. We could at least help with testing, then.

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [June 4, 2010, 4:22pm UTC](https://discuss.elastic.co/t/how-to-custom-score-score-based-on-a-date-field/2971/12 "2010-06-04T16:22:06Z")

</div>

cool, I will notify you once its up.

On Thu, Jun 3, 2010 at 9:17 PM, John Chang [jchangkihtest2@gmail.com](mailto:jchangkihtest2@gmail.com) wrote:

> ## Thanks. Please notify when it is in master, even if it is not yet in release, as we'd be happy to build it and try it out from there. We could at least help with testing, then.
> 
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/How-to-custom-score-score-based-on-a-date-field-tp842971p868331.html](http://elasticsearch-users.115913.n3.nabble.com/How-to-custom-score-score-based-on-a-date-field-tp842971p868331.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<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, 4:23am UTC](https://discuss.elastic.co/t/how-to-custom-score-score-based-on-a-date-field/2971/13 "2017-07-06T04:23:35Z")

</div>


