Basic Search Queries in Elasticsearch: A Comprehensive Guide

Keyword-based Query (Match Query)

The Match query is used to search for documents containing specific keywords. It will return documents that have at least one corresponding keyword.

Example: Find products with the name containing the keyword laptop in the products Index.

GET /products/_search
{
  "query": {
    "match": {
      "name": "laptop"
    }
  }
}

 

Must Contain All Keywords (Match Phrase Query)

The Match Phrase query requires all keywords in the query to appear consecutively and in the correct order within the document text.

Example: Find products with the description containing the phrase HP laptop.

GET /products/_search
{
  "query": {
    "match_phrase": {
      "description": "HP laptop"
    }
  }
}

 

Must Contain Entire Phrase Prefix (Match Phrase Prefix Query)

The Match Phrase Prefix query is similar to Match Phrase, but it allows for a partial match of the last keyword.

Example: Find products with the description starting with laptop.

GET /products/_search
{
  "query": {
    "match_phrase_prefix": {
      "description": "laptop"
    }
  }
}

 

Term-based Query (Term Query)

The Term query is used to search for documents with a field having an exact value as specified.

Example: Find products with the category field having the value laptop.

GET /products/_search
{
  "query": {
    "term": {
      "category": "laptop"
    }
  }
}

 

Range-based Query (Range Query)

The Range query helps search for documents with a field value within a specified range.

Example: Find products with prices between 500 and 1000.

GET /products/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 500,
        "lte": 1000
      }
    }
  }
}

 

Term Level Query

Term Level queries allow searching for documents based on specific conditions like Exact, Prefix, Range, Wildcard, and Fuzzy queries.

Example: Find products with the name starting with laptop and prices between 500 and 1000.

GET /products/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "prefix": {
            "name": "laptop"
          }
        },
        {
          "range": {
            "price": {
              "gte": 500,
              "lte": 1000
            }
          }
        }
      ]
    }
  }
}

 

Full-Text Query

Full-Text queries allow searching text fields using text analysis algorithms to find similar words or synonyms.

Example: Find products with descriptions containing either computer or laptop.

GET /products/_search
{
  "query": {
    "match": {
      "description": "computer laptop"
    }
  }
}

 

Boolean Query

Boolean queries allow combining multiple sub-queries with diverse search conditions, such as must contain all, must contain at least one, or must not contain, to achieve precise search results.

Example: Find products with the category being laptop and prices between 500 and 1000.

GET /products/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "category": "laptop"
          }
        },
        {
          "range": {
            "price": {
              "gte": 500,
              "lte": 1000
            }
          }
        }
      ]
    }
  }
}

 

These are the basic search queries in Elasticsearch, along with illustrated examples for each query type. When using Elasticsearch, you can combine these queries to search for data flexibly and efficiently.