Basic Search Queries
Let’s start by exploring some basic search queries that you can use to search for documents in Elasticsearch.
Match Query
The match query is one of the simplest and most commonly used queries in Elasticsearch. It allows you to search for documents that contain a specific term or phrase.
GET /products/_search
{
"query": {
"match": {
"name": "iphone"
}
}
}
In this example:
- We’re searching for documents in the products index where the name field contains the term “iphone“.
- Elasticsearch will return all documents that match this criteria, along with their relevant information.
Term Query
The term query is used for exact matching of terms. It’s useful when you want to find documents that contain an exact value in a particular field.
GET /products/_search
{
"query": {
"term": {
"category": "electronics"
}
}
}
In this example:
- We’re searching for documents in the products index where the category field exactly matches “electronics“.
- Elasticsearch will return all documents that meet this criteria.
Searching Documents in Elasticsearch
Searching documents in Elasticsearch is a foundational skill for anyone working with this powerful search engine. Whether you’re building a simple search interface or conducting complex data analysis, understanding how to effectively search and retrieve documents is essential.
In this article, we’ll walk through the basics of searching in Elasticsearch, providing clear explanations, examples, and outputs to help you get started.
Contact Us