# How to calculate weighted score depending on order of an array field

**URL:** <https://discuss.elastic.co/t/how-to-calculate-weighted-score-depending-on-order-of-an-array-field/258071>\
**Category:** Elasticsearch\
**Created:** [December 9, 2020, 4:44am UTC](https://discuss.elastic.co/t/how-to-calculate-weighted-score-depending-on-order-of-an-array-field/258071 "2020-12-09T04:44:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![taichi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/taichi/32/46431_2.png) [@taichi](https://discuss.elastic.co/u/taichi)\
**Post date:** [December 9, 2020, 4:44am UTC](https://discuss.elastic.co/t/how-to-calculate-weighted-score-depending-on-order-of-an-array-field/258071/1 "2020-12-09T04:44:47Z")

</div>

I would like to search by an array field, and give weight depending on where the value appears in the arrays.

For example, I have 3 documents like this:

```
{ "foo": [1, 2, 3] }
{ "foo": [2, 3, 4] }
{ "foo": [3, 4, 5] }

```

and I send a query like:

```
{ "query": { "term": { "foo": 3 } } }

```

Then, I want the response to be sorted like this:

```
{ "foo": [3, 4, 5] }
{ "foo": [2, 3, 4] }
{ "foo": [1, 2, 3] }

```

I mean, the earlier `3` appears in the arrays, the higher the `_score` gets.  
Is there any efficient ways to do this?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 9, 2020, 6:33am UTC](https://discuss.elastic.co/t/how-to-calculate-weighted-score-depending-on-order-of-an-array-field/258071/2 "2020-12-09T06:33:52Z")

</div>

I don't think you can do that.  
Unless you defined another data structure, using nested documents like:

```auto
{ "foo": [
 {
  "value": 3,
  "position": 1 
 },
 {
  "value": 4,
  "position": 2 
 },
 {
  "value": 5,
  "position": 3 
 }
] }

```

And then try to increase the score by taking into account the `foo.position` nested field using a function score may be...

May be you can do that with a script score but this might be super slow if you have to read all the documents to compute that score.

My 2 cents.

---

<div class="post-metadata">

**Author:** ![taichi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/taichi/32/46431_2.png) [@taichi](https://discuss.elastic.co/u/taichi)\
**Post date:** [December 9, 2020, 8:12am UTC](https://discuss.elastic.co/t/how-to-calculate-weighted-score-depending-on-order-of-an-array-field/258071/3 "2020-12-09T08:12:26Z")

</div>

Thank you! I think you are right.

---

<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:** [January 6, 2021, 8:12am UTC](https://discuss.elastic.co/t/how-to-calculate-weighted-score-depending-on-order-of-an-array-field/258071/4 "2021-01-06T08:12:32Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
