# Adding additional weight without matching

**URL:** <https://discuss.elastic.co/t/adding-additional-weight-without-matching/270865>\
**Category:** Elasticsearch\
**Created:** [April 21, 2021, 3:49pm UTC](https://discuss.elastic.co/t/adding-additional-weight-without-matching/270865 "2021-04-21T15:49:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Silas\_Hansen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/silas_hansen/32/67521_2.png) [@Silas\_Hansen](https://discuss.elastic.co/u/Silas_Hansen)\
**Post date:** [April 21, 2021, 3:49pm UTC](https://discuss.elastic.co/t/adding-additional-weight-without-matching/270865/1 "2021-04-21T15:49:41Z")

</div>

I want to be able to

1. Match a subset of the index with a _query\_string_ query.
2. Add additional weight to documents that match a set of post-evaluated criterias.

**Example:**

Let's say I have these two documents:

```auto
[
    {
        "Title": "lord of the rings 1",
        "Tags": ["book", "movie", "adventure"]
    },
    {
        "Title": "lord of the rings 2",
        "Tags": ["book", "adventure"]
    }
]

```

I make a search with this query:

```auto
{
  "query": {
    "function_score": {
      "query": {
        "query_string": {
          "fields": [
            "Title"
          ],
          "query": "lord of the rings adventure movie"
        }
      },
      "functions": [
        {
          "filter": {
            "match": {
              "Tags": {
                "query": "lord of the rings adventure movie"
              }
            }
          },
         "weight": 2.0
        }
      ]
    }
  }
}

```

The specific mappings/settings should not be relevant to this question, so I skipped them.

**Question:**  
How do I make the match query inside the functionscore filter pay attention to how well the _match_ query matches the query instead of just a boolean decision whether to apply the static weight or not? In this case, the first document is a better match, since it matches both tags (adveture, movie), whereas the second document only matches one of the tags (adventure), but they come out being equal matches because of the boolean nature of functionscore filters.

I can't move the match into the root query, as I don't want to include other documents with those tags in the output, but I still want the most closely matched document to come first.

Maybe there is a completely different approach to this issue that I'm just not aware of.

Any help is appreciated.

---

<div class="post-metadata">

**Author:** ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)\
**Post date:** [April 22, 2021, 8:48pm UTC](https://discuss.elastic.co/t/adding-additional-weight-without-matching/270865/2 "2021-04-22T20:48:04Z")

</div>

`function_score` query accepts only a single main query, all other queries inside functions can only be filters with a just boolean decision.

One way you can structure your query is to do a boolean query with `must` clause as your main query, and `should` clause as your additional query, something like this:

```auto
{
  "query": {
    "bool": {
      "must": {
        "query_string": {
          "fields": [
            "Title"
          ],
          "query": "lord of the rings adventure movie"
        }
      },
      "should": {
        "match": {
          "Tags": {
            "query": "lord of the rings adventure movie"
          }
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Silas\_Hansen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/silas_hansen/32/67521_2.png) [@Silas\_Hansen](https://discuss.elastic.co/u/Silas_Hansen)\
**Post date:** [April 23, 2021, 7:10am UTC](https://discuss.elastic.co/t/adding-additional-weight-without-matching/270865/3 "2021-04-23T07:10:50Z")

</div>

Hi mayya - thanks for the reply

I could construct the query that way, but that would lead to the _match_ on the Tags field in the should-clause pulling more documents into the resultset, if a Tag happens to match any of the terms in the query.

I'm trying to implement the same behavior as the rescore functionality, but in a way that would always take the entire resultset into consideration and not just a defined window.

---

<div class="post-metadata">

**Author:** ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)\
**Post date:** [April 23, 2021, 3:47pm UTC](https://discuss.elastic.co/t/adding-additional-weight-without-matching/270865/4 "2021-04-23T15:47:45Z")

</div>

> the _match_ on the Tags field in the should-clause pulling more documents into the results set

No, this will not happen. The results set is determined by the `must` clause; `should` clause is used only for adding extra points to scores.

---

<div class="post-metadata">

**Author:** ![Silas\_Hansen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/silas_hansen/32/67521_2.png) [@Silas\_Hansen](https://discuss.elastic.co/u/Silas_Hansen)\
**Post date:** [April 26, 2021, 10:07am UTC](https://discuss.elastic.co/t/adding-additional-weight-without-matching/270865/5 "2021-04-26T10:07:14Z")

</div>

Hi mayya

This works exactly the way you describe and solves the problem beautifully.  
Re-reading the bool-query documentation makes this clear as well.

Thank you!

---

<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:** [May 24, 2021, 10:08am UTC](https://discuss.elastic.co/t/adding-additional-weight-without-matching/270865/6 "2021-05-24T10:08:11Z")

</div>

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