Score document by same value of the field

Let's say you have 5 products from same users and u you list them
If a user_id (field) have same value in 5 documents, i want the fifth document to have the least document score
the score should gradually decrease from the first to the fifth

expected score

"hits": [
      {
        "_type": "product",
        "_id": "74162",
        "_score": 1,
        "_source": {
          "user_id": 90
        } 
     },
     {
        "_type": "product",
        "_id": "6",
        "_score": 1,
        "_source": {
          "user_id": 35
        } 
     }
     {
        "_type": "product",
        "_id": "2",
        "_score": 0.9,
        "_source": {
          "user_id": 90
        } 
     },
     {
        "_type": "product",
        "_id": "3",
        "_score": 0.8,
        "_source": {
          "user_id": 90
        } 
     },
    {
        "_type": "product",
        "_id": "4",
        "_score": 0.7,
        "_source": {
          "user_id": 90
        } 
     },
    {
        "_type": "product",
        "_id": "5",
        "_score": 0.6,
        "_source": {
          "user_id": 90
        } 
     }

]

You need to compute at index time a field that adds to your doc its position number and then use that field in a function score query for example.

You can't do that at search time I think as documents are not relatively scored based on other document counts.

what about if i want to do it in search time because there's so many filters like city filter or town filter or category filter and i have to calculate all of this and score one by one its too hard to manage

I don't think you can. May be others have ideas.

what if i scripted field like this

 "script_fields" : {
    "test1" : {
        "script" : {
            "lang": "painless",
            "source": "doc['count_of_users_result'].value +1"
        }
    }

}
i just dont know how to count them when scripting how to reach other documents fields of result and count them

i just dont know how to count them when scripting how to reach other documents fields of result and count them

That's the thing. Everything which is happening per document is doable. Everything which is related to a previous one is not doable.

May be by writing your own query as a Java plugin but I'm not sure how all that would work in a distributed way.

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