# Retrieve items sorted by mutual-terms match

**URL:** <https://discuss.elastic.co/t/retrieve-items-sorted-by-mutual-terms-match/333604>\
**Category:** Elasticsearch\
**Created:** [May 16, 2023, 9:00pm UTC](https://discuss.elastic.co/t/retrieve-items-sorted-by-mutual-terms-match/333604 "2023-05-16T21:00:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![K\_K2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/k_k2/32/103075_2.png) [@K\_K2](https://discuss.elastic.co/u/K_K2)\
**Post date:** [May 16, 2023, 9:00pm UTC](https://discuss.elastic.co/t/retrieve-items-sorted-by-mutual-terms-match/333604/1 "2023-05-16T21:00:21Z")

</div>

We have index mapping like this:

```auto
{
   "mappings": {
      "_source": {
         "includes": [
            "uid"
         ]
      },
      "properties": {
         "follow": {
            "doc_values": true,
            "eager_global_ordinals": true,
            "index_options": "docs",
            "type": "keyword"
         },
         "staticRank": {
            "index": false,
            "type": "long"
         },
         "id": {
            "doc_values": false,
            "norms": false,
            "type": "keyword"
         }
      }
   },
   "settings": {
      "index": {
         "knn": false,
         "merge": {
            "policy": {
               "segments_per_tier": 2
            }
         },
         "number_of_replicas": 0,
         "number_of_shards": "3",
         "refresh_interval": "-1",
         "sort": {
            "field": "staticRank",
            "order": "desc"
         },
         "store": {
            "type": "mmapfs"
         }
      }
   }
}

```

where "follow" is users which the candidate 'id' is connected to. When we make a search query, we do match on the "follow" terms and that way we retrieve candidates which are 2-levels away from the searcher.

Our query is like this:

```auto
{
   "query": {
     "terms": {
       "follow": ["id5", "id99", "id9"],
       }
   }
 }

```

This gives me any candidates where scores are all 1.

How can i run a query so that i get results where the scores are sorted in descending order and score signifies the overlap b/w searcher's "follow" and candidate's "follow" fields?

---

<div class="post-metadata">

**Author:** ![K\_K2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/k_k2/32/103075_2.png) [@K\_K2](https://discuss.elastic.co/u/K_K2)\
**Post date:** [May 23, 2023, 12:06am UTC](https://discuss.elastic.co/t/retrieve-items-sorted-by-mutual-terms-match/333604/2 "2023-05-23T00:06:08Z")

</div>

can someone help here?

---

<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:** [June 20, 2023, 12:07am UTC](https://discuss.elastic.co/t/retrieve-items-sorted-by-mutual-terms-match/333604/3 "2023-06-20T00:07:03Z")

</div>

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