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

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).

"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)

"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.

"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

"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.

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.

@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.

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