How does a common terms query work in Elasticsearch?

When a common terms query is executed in Elasticsearch, it first identifies the terms that are common or frequent in the specified field, using a list of stopwords or a minimum document frequency threshold. The common terms query then searches for documents that contain at least one informative term and at least one common term.

Here’s an example of a common terms query in Elasticsearch:

GET /my_index/_search
{
  "query": {
    "common": {
      "title": {
        "query": "elasticsearch search",
        "cutoff_frequency": 0.001
      }
    }
  }
}

In this example, we are searching the `title` field in the `my_index` index for documents that contain the terms “elasticsearch” and “search”, but filter out common terms in the field. The `cutoff_frequency` parameter is set to 0.001 to exclude terms that occur in more than 0.1% of the documents.

The common terms query also supports a variety of other features, such as specifying a minimum should match parameter, using a custom list of stopwords, and using a minimum document frequency threshold instead of a percentage.

The common terms query can also be used in combination with other query types, such as the bool query, to construct more complex search queries. By allowing for filtering out common terms or stopwords from search results, the common terms query provides a powerful and flexible way to search for text in Elasticsearch.

However, it’s important to note that common terms queries can be computationally expensive and may not be suitable for large datasets or high-traffic applications. Additionally, common terms queries can be less precise than other query types, such as the term or match query, because they match a broader range of terms. Therefore, it’s important to carefully consider the use case and performance implications before using common terms queries in Elasticsearch.