Mustafa Acar
Mustafa Acar

Reputation: 422

Elasticsearch Score document by the 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
    } 
 }

]

Upvotes: 0

Views: 55

Answers (2)

Piotr Pradzynski
Piotr Pradzynski

Reputation: 4535

Maybe sorting by _score and user_id will be OK for you:

GET /my_index/_search
{
  "sort" : [
    { "_score"  : "desc" },
    { "uder_id" : "asc" }
  ],
  "query" : {
    //your query here
  }
}

Source: https://www.elastic.co/guide/en/elasticsearch/reference/6.2/search-request-sort.html

Upvotes: 0

aclowkay
aclowkay

Reputation: 3887

Are you using the score other than ordering results? if not then you can just order by user_id

Also can you specify what defines the relevance of these results?

Upvotes: 1

Related Questions