Elasticsearch Elasticsearch Query Field Contains Text

By Opster Expert Team - May 2023

Updated: Aug 17, 2023

| 2 min read

Before you dig into the details of this technical guide, have you tried asking OpsGPT?

You'll receive concise answers that will help streamline your Elasticsearch/OpenSearch operations.


Try OpsGPT now for step-by-step guidance and tailored insights into your Elasticsearch/ OpenSearch operation.

Before you begin reading this guide, we recommend you try running the Elasticsearch Error Check-Up which analyzes 2 JSON files to detect many configuration errors.

To easily resolve issues in your deployment and locate their root cause, try AutoOps for ElasticSearch. It diagnoses problems by analyzing hundreds of metrics collected by a lightweight agent and offers guidance for resolving them.

Quick links

Introduction

When working with Elasticsearch, one of the most common tasks is searching for text within specific fields of your documents. This article will provide a comprehensive guide on how to construct and optimize queries for searching text in specific fields using Elasticsearch.

1. Query String Query

The query_string Query is a versatile and flexible way to search for text in specific fields. It allows you to use the Lucene query syntax, which provides a rich set of options for text search.

Example:

GET /_search
{
  "query": {
    "query_string": {
      "default_field": "content",
      "query": "elasticsearch"
    }
  }
}

In this example, we are searching for the term “elasticsearch” within the “content” field.

2. Match Query

The match query is a simple and straightforward way to search for text in a specific field. It is suitable for full-text search and works well with analyzed fields.

Example:

GET /_search
{
  "query": {
    "match": {
      "title": "elasticsearch"
    }
  }
}

In this example, we are searching for the term “elasticsearch” within the “title” field.

3. Multi-Match Query

The multi_match query allows you to search for text in multiple fields at once. You can specify multiple fields and a query string, and Elasticsearch will search for the text in all specified fields.

Example:

GET /_search
{
  "query": {
    "multi_match": {
      "query": "elasticsearch",
      "fields": ["title", "content"]
    }
  }
}

In this example, we are searching for the term “elasticsearch” within both the “title” and “content” fields.

4. Match Phrase Query

The match_phrase query is used when you want to search for an exact phrase in a specific field. It is useful when the order of the words in the query is important.

Example:

GET /_search
{
  "query": {
    "match_phrase": {
      "content": "distributed search engine"
    }
  }
}

5. Boosting Fields

You can boost the importance of specific fields in your query by using the caret (^) symbol followed by a boost value. This allows you to give more weight to certain fields when calculating the relevance score.

Example:

GET /_search
{
  "query": {
    "multi_match": {
      "query": "elasticsearch",
      "fields": ["title^3", "content"]
    }
  }
}

In this example, we are searching for the term “elasticsearch” within both the “title” and “content” fields, but the “title” field has three times the weight of the “content” field.

If you want to learn more about the difference between text and keyword fields, you can take a look at this guide from Opster. You should also take a look at this guide, which contains a very detailed explanation on how the match, multi_match and match_phrase work.

How helpful was this guide?

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?


Get expert answers on Elasticsearch/OpenSearch