# Is it possible to search only when there are a certain number of terms in the query?

**URL:** https://discuss.elastic.co/t/is-it-possible-to-search-only-when-there-are-a-certain-number-of-terms-in-the-query/341420
**Category:** Elasticsearch
**Created:** [August 23, 2023, 2:56am UTC](https://discuss.elastic.co/t/is-it-possible-to-search-only-when-there-are-a-certain-number-of-terms-in-the-query/341420 "2023-08-23T02:56:39Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![gony](https://avatars.discourse-cdn.com/v4/letter/g/e47c2d/32.png) [@gony](https://discuss.elastic.co/u/gony)
#### Post date: [August 23, 2023, 2:56am UTC](https://discuss.elastic.co/t/is-it-possible-to-search-only-when-there-are-a-certain-number-of-terms-in-the-query/341420/1 "2023-08-23T02:56:39Z")

</div>

When using a match query, is it possible to search only when there are a certain number of terms in the query?

I need that functionality, not for the entire query, but as part of a subquery that I will put inside a boolean query.

For example, is it possible to create a query so that it only returns results if there are at least 3 terms in the query?  
With the standard analyzer, I shouldn't get any results for the query below(because the query contains only two terms).

```auto
"query": {
  "match": {
    "content": {
      "query": "dog cat"
    }
  }
}

```

I should get search results for the queries below. (since there are 4 terms in the query, I'm assuming there are matching documents)

```auto
"query": {
  "match": {
    "content": {
      "query": "dog cat puppy kitty"
    }
  }
}

```

Is there any way to determine if a match is made by the number of terms contained in the query as shown above?

It's undocumented, but I was able to write a similar query using the "or" operator and minimum\_should\_match.

```auto
"query": {
  "match": {
    "content": {
      "query": "term1 term2 term3",
      "operator": "or",
      "minimum_should_match": 3
    }
  }
}

```

However, it's not perfect, and won't work in the following cases

```auto
"query": {
  "match": {
    "content": {
      "query": "term1",
      "operator": "or",
      "minimum_should_match": 3
    }
  }
}

```

In the above case, since there is only 1 term, it appears that minimum\_should\_match does not apply.

I was wondering if there is any other way to determine whether to match by the number of terms in the query.  
The ES version used is 7.15.2.

---

<div class="post-metadata">

### Author: ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)
#### Post date: [August 27, 2023, 10:27am UTC](https://discuss.elastic.co/t/is-it-possible-to-search-only-when-there-are-a-certain-number-of-terms-in-the-query/341420/2 "2023-08-27T10:27:03Z")

</div>

> [@gony](#):
>
> For example, is it possible to create a query so that it only returns results if there are at least 3 terms in the query?

Why do you not add a check in your code and only run the query if the number of terms are sufficient? If you would expect the query to not return anything anyway I do not see the point of running the query in the first place.

---

<div class="post-metadata">

### Author: ![gony](https://avatars.discourse-cdn.com/v4/letter/g/e47c2d/32.png) [@gony](https://discuss.elastic.co/u/gony)
#### Post date: [August 27, 2023, 11:05am UTC](https://discuss.elastic.co/t/is-it-possible-to-search-only-when-there-are-a-certain-number-of-terms-in-the-query/341420/3 "2023-08-27T11:05:01Z")

</div>

@Christian_Dahlqvist

I used a simple example for understanding, but I think the main example would be using a query like the above as a query in a filter query for the weight of a function score query. If what you meant was to combine queries based on the number of terms in the code (e.g., API server code), then accurately determining the number of terms for the analyzed field would require an additional network call because it would require a call to the \_analyze api, and I don't want this situation.

---

<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: [September 24, 2023, 11:05am UTC](https://discuss.elastic.co/t/is-it-possible-to-search-only-when-there-are-a-certain-number-of-terms-in-the-query/341420/4 "2023-09-24T11:05:27Z")

</div>

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