0
0
ElasticsearchConceptBeginner · 3 min read

What is Text Type in Elasticsearch: Explanation and Example

In Elasticsearch, the text type is used to store full-text strings that need to be analyzed for search. It breaks down the text into words or tokens to enable efficient searching and matching of phrases or keywords.
⚙️

How It Works

The text type in Elasticsearch is designed to handle long strings of text, like sentences or paragraphs, that you want to search through. Instead of storing the text as a single block, Elasticsearch breaks it into smaller pieces called tokens using an analyzer. This process is like cutting a book into words so you can quickly find any word or phrase later.

When you search, Elasticsearch compares your search words to these tokens, making it easy to find matches even if the exact phrase isn't stored. This is different from storing exact values, where the whole text must match perfectly.

💻

Example

This example shows how to define a text field in an Elasticsearch index mapping and how to index and search a document.

json
PUT /my_index
{
  "mappings": {
    "properties": {
      "description": {
        "type": "text"
      }
    }
  }
}

POST /my_index/_doc/1
{
  "description": "Elasticsearch is a powerful search engine"
}

GET /my_index/_search
{
  "query": {
    "match": {
      "description": "powerful search"
    }
  }
}
Output
{ "hits": { "total": { "value": 1, "relation": "eq" }, "hits": [ { "_id": "1", "_source": { "description": "Elasticsearch is a powerful search engine" } } ] } }
🎯

When to Use

Use the text type when you want to search within large pieces of text, such as articles, product descriptions, or user comments. It is ideal for full-text search where you need to find documents containing certain words or phrases, even if they appear in different forms or orders.

If you only need to filter or sort by exact values, use the keyword type instead.

Key Points

  • Text type stores analyzed full-text strings for search.
  • It breaks text into tokens for flexible matching.
  • Best for searching words or phrases inside large text fields.
  • Not suitable for exact matching or sorting; use keyword for that.

Key Takeaways

The text type is for storing and searching full-text strings in Elasticsearch.
It analyzes text by breaking it into tokens to enable flexible search.
Use text type for searching inside large text fields like descriptions or comments.
For exact matches or sorting, use the keyword type instead.
Text type enables powerful full-text search capabilities in Elasticsearch.