简体   繁体   中英

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
    } 
 }

]

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM