# Using field value for weighting

**URL:** <https://discuss.elastic.co/t/using-field-value-for-weighting/3061>\
**Category:** Elasticsearch\
**Created:** [July 1, 2010, 2:56pm UTC](https://discuss.elastic.co/t/using-field-value-for-weighting/3061 "2010-07-01T14:56:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Korbakov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michael_korbakov/32/3199_2.png) [@Michael\_Korbakov](https://discuss.elastic.co/u/Michael_Korbakov)\
**Post date:** [July 1, 2010, 2:56pm UTC](https://discuss.elastic.co/t/using-field-value-for-weighting/3061/1 "2010-07-01T14:56:19Z")

</div>

Hi everyone.

Is it possible to use some numeric field value to rank the documents?  
In my scenario I have field like "views\_count" in my documents and I  
want to use it's value with some weight in ranking. How can I add it  
to the documents total score?

Thank you in advance

-- Michael Korbakov

---

<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:** [July 1, 2010, 3:07pm UTC](https://discuss.elastic.co/t/using-field-value-for-weighting/3061/2 "2010-07-01T15:07:13Z")

</div>

Hiya

> Is it possible to use some numeric field value to rank the documents?  
> In my scenario I have field like "views\_count" in my documents and I  
> want to use it's value with some weight in ranking. How can I add it  
> to the documents total score?

You want to use the new custom\_score query:

> <https://github.com/elastic/elasticsearch/issues/220>
>
> \`custom\_score\` query allows to wrap another query and customize the scoring of i…t optionally with a computation derived from other field values in the doc (numeric ones). Here is a simple sample:
> 
> \`\`\`
> "custom\_score" : {
> "query" : {
> ....
> },
> "script" : "score \* doc\['my\_numeric\_field'\].value"
> }
> \`\`\`
> 
> The \`script\` uses \`mvel\` (http://mvel.codehaus.org/) which gives a lot of flexibility in terms of what can be computed and is \_really\_ fast. Other options considered were building a Json based AST (which will be faster, and might be implemented in the future), or using javascript, which is not as fast on the JVM (rhino).
> 
> There are "extensions" to mvel provided, they include the \`score\` variable, which is the score of the internal query. The idea of providing the score and let the user use it is to allow for complete control over the scoring, without internally multiplying whatever the script produced with the score. This allows for the sub query score to be discarded if needed, or computed on a different "scale" than multiplying.
> 
> It also provides all the Math functions (will be listed in the final docs). The more interesting one is the \`doc\[field\_name\]\`, which actually returns an object that has the following common properties:
> \- \`doc\['field\_name'\].value\`: The native numeric value of the field. For example, if its a \`short\` type, it will be short.
> \- \`doc\['field\_name'\].values\`: The native numeric array values of the field. For example, if its a \`short\` type, it will be short\[\]. Remember, a field can have several values within a single doc. Returns an empty array if the field has no values.
> \- \`doc\['field\_name'\].stringValue\`: The string value of the field.
> \- \`doc\['field\_name'\].doubleValue\`: The converted double of the field. Replace \`double\` with \`int\`, \`long\`, \`float\`, \`short\`, \`byte\`. Returns an empty array if the field has no values.
> \- \`doc\['field\_name'\].doubleValues\`: A converted double values array.
> \- \`doc\['field\_name'\].date\`: Applies only to date / long (timestamp) types, returns a \`MutableDateTime\` (http://joda-time.sourceforge.net/api-release/org/joda/time/MutableDateTime.html) allowing to get date / time specific data.
> \- \`doc\['field\_name'\].dates\`: Applies only to date / long (timestamp) types, returns a \`MutableDateTime\` array (http://joda-time.sourceforge.net/api-release/org/joda/time/MutableDateTime.html) allowing to get date / time specific data.
> \- \`doc\['field\_name'\].empty\`: A boolean indicating if the field has no values within the doc.
> \- \`doc\['field\_name'\].multiValued\`: A boolean indicating that the field has several values within the corpus.
> 
> \`custom\_score\` also allows to accept parameters to scripts (allowing to reuse scripts for better performance). Parameters are simple json object that can then be accessed in the script, for example:
> 
> \`\`\`
> "custom\_score" : {
> "query" : {
> ....
> },
> "params" : {
> "param1" : 2,
> "param2" : 3.1
> }
> "script" : "score \* doc\['my\_numeric\_field'\].value / pow(param1, param2)"
> }
> \`\`\`

Only available in master - will be in 0.9 when it is released in the  
next few weeks

clint

--  
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:** ![Michael\_Korbakov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michael_korbakov/32/3199_2.png) [@Michael\_Korbakov](https://discuss.elastic.co/u/Michael_Korbakov)\
**Post date:** [July 1, 2010, 3:43pm UTC](https://discuss.elastic.co/t/using-field-value-for-weighting/3061/3 "2010-07-01T15:43:02Z")

</div>

Thank you. That's exactly what I was looking for.

On 1 ц▄ц┴ц░. 2010, at 18:07, Clinton Gormley [clinton@iannounce.co.uk](mailto:clinton@iannounce.co.uk) wrote:

> Hiya
> 
> > Is it possible to use some numeric field value to rank the documents?  
> > In my scenario I have field like "views\_count" in my documents and I  
> > want to use it's value with some weight in ranking. How can I add it  
> > to the documents total score?
> 
> You want to use the new custom\_score query:
> 
> [Query DSL: custom score (scripted) query · Issue #220 · elastic/elasticsearch · GitHub](http://github.com/elasticsearch/elasticsearch/issues/220)
> 
> Only available in master - will be in 0.9 when it is released in the  
> next few weeks
> 
> clint
> 
> --  
> 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:** ![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:22am UTC](https://discuss.elastic.co/t/using-field-value-for-weighting/3061/4 "2017-07-06T04:22:47Z")

</div>


