# Facet that returns a value from the most recent matching document?

**URL:** <https://discuss.elastic.co/t/facet-that-returns-a-value-from-the-most-recent-matching-document/9151>\
**Category:** Elasticsearch\
**Created:** [September 26, 2012, 8:25am UTC](https://discuss.elastic.co/t/facet-that-returns-a-value-from-the-most-recent-matching-document/9151 "2012-09-26T08:25:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric\_Jain](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eric_jain/32/834_2.png) [@Eric\_Jain](https://discuss.elastic.co/u/Eric_Jain)\
**Post date:** [September 26, 2012, 8:25am UTC](https://discuss.elastic.co/t/facet-that-returns-a-value-from-the-most-recent-matching-document/9151/1 "2012-09-26T08:25:47Z")

</div>

I'd like to add a facet to an existing query that gives me the value of a  
specific field in the most recent (as determined by a specified timestamp  
field) document.

e.g. given these 3 documents:

timestamp:20120925T01:00 tag:foo score:500  
timestamp:20120925T01:00 tag:bar score:200  
timestamp:20120925T02:00 tag:bar score:300

...and the query tag:bar, I want to have the latest value of the score  
field (i.e. 300) included in the result (regardless of how I'm sorting and  
paging).

Is this possible, or would this require a custom facet? Speaking of which:  
What's the status of the facet API?

--

---

<div class="post-metadata">

**Author:** ![BillyEm](https://avatars.discourse-cdn.com/v4/letter/b/c4cdca/32.png) [@BillyEm](https://discuss.elastic.co/u/BillyEm)\
**Post date:** [September 29, 2012, 12:29am UTC](https://discuss.elastic.co/t/facet-that-returns-a-value-from-the-most-recent-matching-document/9151/2 "2012-09-29T00:29:39Z")

</div>

Recency contribution to scoring is pretty common in News sites and others;  
.

 

That said, there are a number of ways in Lucene to boost on the value of a  
field in a query. Field boosts aren't going to be useful given your query.  
2 others, might be a function-query that reserves a score value, or the  
docid of the most recent, as it aggregates the result set. Function  
queries, in my experience, have widely varying performance characteristics,  
and are not necessarily easy to adopt. That leaves payloads, as another  
poster suggested.

 

I've been part of a team to implement recency weighting directly into a  
large scale search engine. We used a Pade algorithm for what its worth,  
since defining decay of weighting was clear. Getting it right was not so  
clear.

 

Geez, I'm long winded these days. Sorry folks. Anyway Eric -- the 2  
approaches that I would be considering require either, an indexing system  
that does ETL on the documents such that you can readily get the hit you  
want, or a custom scorer.

 

2 resources that might illuminate more current thinking would be: a)  
javasoze/bobo (look at the breadth of facets javadoc for ideas about what  
might fit functionally, even if you're not up to using bobo, and b) lucene  
4.0.x. The latter because the absence of a feature in 3.x does not mean the  
Lucene team hasn't already identified a way to implement it .... albeit for  
future releases. (how do you spell "backport")

 

g'luck

 

On Wednesday, September 26, 2012 4:25:47 AM UTC-4, Eric Jain wrote:

 

> I'd like to add a facet to an existing query that gives me the value of a  
> specific field in the most recent (as determined by a specified timestamp  
> field) document.
> 
> e.g. given these 3 documents:
> 
> timestamp:20120925T01:00 tag:foo score:500  
> timestamp:20120925T01:00 tag:bar score:200  
> timestamp:20120925T02:00 tag:bar score:300
> 
> ...and the query tag:bar, I want to have the latest value of the score  
> field (i.e. 300) included in the result (regardless of how I'm sorting and  
> paging).
> 
> Is this possible, or would this require a custom facet? Speaking of which:  
> What's the status of the facet API?

 

--

---

<div class="post-metadata">

**Author:** ![Eric\_Jain](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eric_jain/32/834_2.png) [@Eric\_Jain](https://discuss.elastic.co/u/Eric_Jain)\
**Post date:** [September 29, 2012, 1:32am UTC](https://discuss.elastic.co/t/facet-that-returns-a-value-from-the-most-recent-matching-document/9151/3 "2012-09-29T01:32:27Z")

</div>

It should still be possible to accomplish this with a custom facet in  
Elasticsearch, right? Doesn't seem more involved than the "date  
histogram with value field" facet...

On Fri, Sep 28, 2012 at 5:29 PM, BillyEm [wmartinusa@gmail.com](mailto:wmartinusa@gmail.com) wrote:

> Recency contribution to scoring is pretty common in News sites and others;  
> .
> 
>  
> 
> That said, there are a number of ways in Lucene to boost on the value of a  
> field in a query. Field boosts aren't going to be useful given your query. 2  
> others, might be a function-query that reserves a score value, or the docid  
> of the most recent, as it aggregates the result set. Function queries, in my  
> experience, have widely varying performance characteristics, and are not  
> necessarily easy to adopt. That leaves payloads, as another poster  
> suggested.
> 
>  
> 
> I've been part of a team to implement recency weighting directly into a  
> large scale search engine. We used a Pade algorithm for what its worth,  
> since defining decay of weighting was clear. Getting it right was not so  
> clear.
> 
>  
> 
> Geez, I'm long winded these days. Sorry folks. Anyway Eric -- the 2  
> approaches that I would be considering require either, an indexing system  
> that does ETL on the documents such that you can readily get the hit you  
> want, or a custom scorer.
> 
>  
> 
> 2 resources that might illuminate more current thinking would be: a)  
> javasoze/bobo (look at the breadth of facets javadoc for ideas about what  
> might fit functionally, even if you're not up to using bobo, and b) lucene  
> 4.0.x. The latter because the absence of a feature in 3.x does not mean the  
> Lucene team hasn't already identified a way to implement it .... albeit for  
> future releases. (how do you spell "backport")
> 
>  
> 
> g'luck
> 
>  
> 
> On Wednesday, September 26, 2012 4:25:47 AM UTC-4, Eric Jain wrote:
> 
>  
> 
> > I'd like to add a facet to an existing query that gives me the value of a  
> > specific field in the most recent (as determined by a specified timestamp  
> > field) document.
> > 
> > e.g. given these 3 documents:
> > 
> > timestamp:20120925T01:00 tag:foo score:500  
> > timestamp:20120925T01:00 tag:bar score:200  
> > timestamp:20120925T02:00 tag:bar score:300
> > 
> > ...and the query tag:bar, I want to have the latest value of the score  
> > field (i.e. 300) included in the result (regardless of how I'm sorting and  
> > paging).
> > 
> > Is this possible, or would this require a custom facet? Speaking of which:  
> > What's the status of the facet API?
> 
>  
> 
> --

 

--

---

<div class="post-metadata">

**Author:** ![dobe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dobe/32/2152_2.png) [@dobe](https://discuss.elastic.co/u/dobe)\
**Post date:** [September 29, 2012, 11:09am UTC](https://discuss.elastic.co/t/facet-that-returns-a-value-from-the-most-recent-matching-document/9151/4 "2012-09-29T11:09:38Z")

</div>

see the "Latest Facet"  
on [GitHub - crate/elasticsearch-timefacets-plugin: Elasticsearch Timebased Facets](https://github.com/lovelysystems/elasticsearch-ls-plugins) it does  
exactly this.

On Wednesday, September 26, 2012 10:25:47 AM UTC+2, Eric Jain wrote:

> I'd like to add a facet to an existing query that gives me the value of a  
> specific field in the most recent (as determined by a specified timestamp  
> field) document.
> 
> e.g. given these 3 documents:
> 
> timestamp:20120925T01:00 tag:foo score:500  
> timestamp:20120925T01:00 tag:bar score:200  
> timestamp:20120925T02:00 tag:bar score:300
> 
> ...and the query tag:bar, I want to have the latest value of the score  
> field (i.e. 300) included in the result (regardless of how I'm sorting and  
> paging).
> 
> Is this possible, or would this require a custom facet? Speaking of which:  
> What's the status of the facet API?

--

---

<div class="post-metadata">

**Author:** ![Eric\_Jain](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eric_jain/32/834_2.png) [@Eric\_Jain](https://discuss.elastic.co/u/Eric_Jain)\
**Post date:** [October 4, 2012, 7:28am UTC](https://discuss.elastic.co/t/facet-that-returns-a-value-from-the-most-recent-matching-document/9151/5 "2012-10-04T07:28:12Z")

</div>

On Sat, Sep 29, 2012 at 4:09 AM, dobe [bernddorn@gmail.com](mailto:bernddorn@gmail.com) wrote:

> see the "Latest Facet" on  
> [GitHub - crate/elasticsearch-timefacets-plugin: Elasticsearch Timebased Facets](https://github.com/lovelysystems/elasticsearch-ls-plugins) it does exactly  
> this.

I'll need to support float values (and don't need to aggregate on a  
key field), but looking through that code should help me a  
lot--thanks!

Does anyone know if there are significant changes to the facet API  
planned for 0.20.0?

--

---

<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:** [October 4, 2012, 5:12pm UTC](https://discuss.elastic.co/t/facet-that-returns-a-value-from-the-most-recent-matching-document/9151/6 "2012-10-04T17:12:41Z")

</div>

Judging by the commits and issues, there was no work done on facets.

> **[Issues · elastic/elasticsearch](https://github.com/elastic/elasticsearch/issues?q=is%3Aclosed%2Blabel%3Av0.20.0)**
>
> Free and Open, Distributed, RESTful Search Engine. Contribute to elastic/elasticsearch development by creating an account on GitHub.

On Thu, Oct 4, 2012 at 12:28 AM, Eric Jain [eric.jain@gmail.com](mailto:eric.jain@gmail.com) wrote:

> Does anyone know if there are significant changes to the facet API  
> planned for 0.20.0?

--

---

<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:10am UTC](https://discuss.elastic.co/t/facet-that-returns-a-value-from-the-most-recent-matching-document/9151/7 "2017-07-06T03:10:05Z")

</div>


