What is Text Type in Elasticsearch: Explanation and Example
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.
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"
}
}
}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
keywordfor that.