# Elasticsearch ranking aggregation with multiple terms query

**URL:** <https://discuss.elastic.co/t/elasticsearch-ranking-aggregation-with-multiple-terms-query/201219>\
**Category:** Elasticsearch\
**Created:** [September 26, 2019, 11:27am UTC](https://discuss.elastic.co/t/elasticsearch-ranking-aggregation-with-multiple-terms-query/201219 "2019-09-26T11:27:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Azeem\_Akhtar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/azeem_akhtar/32/54924_2.png) [@Azeem\_Akhtar](https://discuss.elastic.co/u/Azeem_Akhtar)\
**Post date:** [September 26, 2019, 11:27am UTC](https://discuss.elastic.co/t/elasticsearch-ranking-aggregation-with-multiple-terms-query/201219/1 "2019-09-26T11:27:55Z")

</div>

I have two indices documents and recommendations with the following mappings:

**Documents:**

```auto
{
    "id": string,
    "document_text" : string,
    "author" : { "name": string }
    ...other fields
}

```

**Recommendations:**

```auto
{
    "id": string,
    "recommendation_text" : string,
    "author" : { "name": string }
    ...other fields
}

```

The problem I am solving is to have top authors for query terms.

This works quite well with `multimatch` for a single query term like this:

```auto
{
  "size": 0,
  "query": {
    "multi_match": {
      "query": "science",
      "fields": [
        "document_text",
        "recommendation_text"
      ],
      "type": "phrase",
    }

  },
  "aggs": {
    "search-authors": {
      "terms": {
        "field": "author.name.keyword",
        "size": 50
      },
      "aggs": {
        "top-docs": {
          "top_hits": {
            "size": 100
          }
        }
      }
    }
  }
}

```

But when I have multiple keywords, let's say `zoology, botany` , I want the aggregation ranking to place the authors who have talked about both zoology and botany higher than those who have used either of them.

having multiple multi\_match with bool doesn't help since this isn't exactly an and/or situation.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [September 26, 2019, 11:42am UTC](https://discuss.elastic.co/t/elasticsearch-ranking-aggregation-with-multiple-terms-query/201219/2 "2019-09-26T11:42:29Z")

</div>

For this sort of analysis I'd say there's two options:

A) Tighten the query (e.g use ANDs to ensure you only find authors covering all the searched topics) or  
B) Get smarter about how you look at looser match results

For option B this example of using the `significant_terms` and `sampler` aggs will be relevant: [https://www.youtube.com/watch?v=azP15yvbOBA](https://www.youtube.com/watch?v=azP15yvbOBA)

---

<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:** [October 24, 2019, 11:42am UTC](https://discuss.elastic.co/t/elasticsearch-ranking-aggregation-with-multiple-terms-query/201219/3 "2019-10-24T11:42:40Z")

</div>

Closed on October 24, 2019, 11:42am UTC

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