# CustomScore and biggest value from all docs

**URL:** https://discuss.elastic.co/t/customscore-and-biggest-value-from-all-docs/9152
**Category:** Elasticsearch
**Created:** [September 26, 2012, 8:26am UTC](https://discuss.elastic.co/t/customscore-and-biggest-value-from-all-docs/9152 "2012-09-26T08:26:37Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![ESFan](https://avatars.discourse-cdn.com/v4/letter/e/c67d28/32.png) [@ESFan](https://discuss.elastic.co/u/ESFan)
#### Post date: [September 26, 2012, 8:26am UTC](https://discuss.elastic.co/t/customscore-and-biggest-value-from-all-docs/9152/1 "2012-09-26T08:26:37Z")

</div>

Hi now i have customscore "\_score + ((parseInt(doc.ad\_when.value) -  
oldestAd) / paramNow) \* 2". Is possible to use in custom score biggest  
value from all docs.

Example:  
in all docs i have popularity field and i want use biggest value from all  
docs to customscore. is it possible?

Thanks for answers. 🙂

--

---

<div class="post-metadata">

### Author: ![Chris\_Male](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chris_male/32/2607_2.png) [@Chris\_Male](https://discuss.elastic.co/u/Chris_Male)
#### Post date: [September 27, 2012, 2:33am UTC](https://discuss.elastic.co/t/customscore-and-biggest-value-from-all-docs/9152/2 "2012-09-27T02:33:27Z")

</div>

Hi,

Just so I understand, you want to include the largest value from your  
popularity field, in the custom score of the documents matching your query?  
Do you want the largest value from all documents with a popularity field,  
or just those which match your query?

On Wednesday, September 26, 2012 8:26:37 PM UTC+12, ESFan wrote:

> Hi now i have customscore "\_score + ((parseInt(doc.ad\_when.value) -  
> oldestAd) / paramNow) \* 2". Is possible to use in custom score biggest  
> value from all docs.
> 
> Example:  
> in all docs i have popularity field and i want use biggest value from all  
> docs to customscore. is it possible?
> 
> Thanks for answers. 🙂

--

---

<div class="post-metadata">

### Author: ![ESFan](https://avatars.discourse-cdn.com/v4/letter/e/c67d28/32.png) [@ESFan](https://discuss.elastic.co/u/ESFan)
#### Post date: [September 27, 2012, 5:30am UTC](https://discuss.elastic.co/t/customscore-and-biggest-value-from-all-docs/9152/3 "2012-09-27T05:30:44Z")

</div>

Yes. i want from all docs get biggest value.  
Example:  
I have docs:  
A | 12  
B | 41  
C | 88  
D | 21  
E | 14  
F | 15

Formula will be like (it will be use 88, because it's biggest value):  
\_score + ((parseInt(doc.ad\_when.value) - 88) / doc.id.value)) \* 2.

It will be easy if i send 2 requests to server. But i don't want to make  
that. 🙂

--

---

<div class="post-metadata">

### Author: ![Chris\_Male](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chris_male/32/2607_2.png) [@Chris\_Male](https://discuss.elastic.co/u/Chris_Male)
#### Post date: [September 28, 2012, 5:14am UTC](https://discuss.elastic.co/t/customscore-and-biggest-value-from-all-docs/9152/4 "2012-09-28T05:14:02Z")

</div>

Unfortunately computing this won't be easy. I took a look at how the  
maximum value is computed in the Statistical facets  
([Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/statistical-facet.html))  
and it uses a Collector to compute the maximum value (which basically  
involves iterating over all the values for each document).

Does your data change often? Could you compute the value and cache it  
somehow?

On Thursday, September 27, 2012 5:30:44 PM UTC+12, ESFan wrote:

> Yes. i want from all docs get biggest value.  
> Example:  
> I have docs:  
> A | 12  
> B | 41  
> C | 88  
> D | 21  
> E | 14  
> F | 15
> 
> Formula will be like (it will be use 88, because it's biggest value):  
> \_score + ((parseInt(doc.ad\_when.value) - 88) / doc.id.value)) \* 2.
> 
> It will be easy if i send 2 requests to server. But i don't want to make  
> that. 🙂

--

---

<div class="post-metadata">

### Author: ![ESFan](https://avatars.discourse-cdn.com/v4/letter/e/c67d28/32.png) [@ESFan](https://discuss.elastic.co/u/ESFan)
#### Post date: [September 28, 2012, 10:53am UTC](https://discuss.elastic.co/t/customscore-and-biggest-value-from-all-docs/9152/5 "2012-09-28T10:53:48Z")

</div>

You are recommend make two different queries? Nope data not change often  
(in day we adding about -3 000 new docs). Yes i can cache it.

On Friday, September 28, 2012 8:14:02 AM UTC+3, Chris Male wrote:

> Unfortunately computing this won't be easy. I took a look at how the  
> maximum value is computed in the Statistical facets (  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/statistical-facet.html))  
> and it uses a Collector to compute the maximum value (which basically  
> involves iterating over all the values for each document).
> 
> Does your data change often? Could you compute the value and cache it  
> somehow?
> 
> On Thursday, September 27, 2012 5:30:44 PM UTC+12, ESFan wrote:
> 
> > Yes. i want from all docs get biggest value.  
> > Example:  
> > I have docs:  
> > A | 12  
> > B | 41  
> > C | 88  
> > D | 21  
> > E | 14  
> > F | 15
> > 
> > Formula will be like (it will be use 88, because it's biggest value):  
> > \_score + ((parseInt(doc.ad\_when.value) - 88) / doc.id.value)) \* 2.
> > 
> > It will be easy if i send 2 requests to server. But i don't want to make  
> > that. 🙂

--

---

<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:43am UTC](https://discuss.elastic.co/t/customscore-and-biggest-value-from-all-docs/9152/6 "2012-09-29T00:43:23Z")

</div>

I'm a little confused about why you need to cache it. If you use an  
OpenBitSet based field cache, it remains there for the duration of your  
index currency, doesn't it? How do you get the largest value? You sub-class  
the cached field and add the numeric value field you're after. (If its  
represented as a string forget this idea). When the indexreader is  
opened/re-opened you re-init your the subCacheClass, and compute the  
largest value 1x. That value remains current until the docs that have the  
value are removed, or the cache needs a full reinit.

We did this at a large video site successfully (_however my description may  
be unsuccessful_). Inside a custom scorer (with no sort fields needed) it  
was straightforward to pull the needed value from the bitset based object  
(since the bits represent the docids)

now, if that's confusing an older idea: generate a  
GlobalStatisticsDocument, that has values in the fields that can _never_ be  
retrieved. And store the latest big value in a field there. Always re-index  
that document into an "invisible" docid, that can only be instantiated (and  
should need to be 1x per index open) for your scoring use (and other  
nefarious non-user purposes.)

On Wednesday, September 26, 2012 4:26:37 AM UTC-4, ESFan wrote:

> Hi now i have customscore "\_score + ((parseInt(doc.ad\_when.value) -  
> oldestAd) / paramNow) \* 2". Is possible to use in custom score biggest  
> value from all docs.
> 
> Example:  
> in all docs i have popularity field and i want use biggest value from all  
> docs to customscore. is it possible?
> 
> Thanks for answers. 🙂

--

---

<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/customscore-and-biggest-value-from-all-docs/9152/7 "2017-07-06T03:10:48Z")

</div>


