Elasticsearch Match Query - Syntax, Example, and Tips

Pulse - Elasticsearch Operations Done Right

On this page

What it does Syntax Example Query Common Issues Best Practices Frequently Asked Questions

What it does

The Match query is a standard query for performing full-text searches in Elasticsearch. The Match query analyzes the input text and creates a boolean query from the provided text. It's used for both full-text searches and for exact-value matches on text, numeric, date, or boolean fields.

Syntax

GET /<index>/_search
{
  "query": {
    "match": {
      "<field>": {
        "query": "<text>",
        "operator": "and|or",
        "fuzziness": "AUTO",
        "zero_terms_query": "all|none"
      }
    }
  }
}

For more details, refer to the official Elasticsearch documentation on Match Query.

Example Query

GET /my-index/_search
{
  "query": {
    "match": {
      "message": {
        "query": "this is a test",
        "operator": "and"
      }
    }
  }
}

This query searches for documents in the "my-index" index where the "message" field contains all the words "this", "is", "a", and "test".

Common Issues

  1. Unexpected results: Sometimes, the Match query might return unexpected results due to the analyzer used. Make sure you understand how your fields are analyzed.

  2. Performance issues: For large datasets, Match queries can be slow. Consider using other query types or optimizing your index for better performance.

  3. Relevance scoring: The default scoring might not always provide the most relevant results for your use case. You may need to adjust boost parameters or use function scores.

Best Practices

  1. Use the operator parameter to control how the query terms should be matched. Use "and" for stricter matching and "or" for more lenient matching.

  2. Utilize the minimum_should_match parameter to fine-tune the number of terms that should match.

  3. Consider using fuzziness for handling typos and misspellings, but be cautious as it can impact performance.

  4. For multi-field searching, consider using multi_match query instead of multiple match queries.

Frequently Asked Questions

Q: How does the Match query differ from Term query?
A: The Match query analyzes the input text before searching, while the Term query does not. This means Match query is better for full-text search on analyzed fields, while Term query is better for exact matches on not_analyzed fields.

Q: Can I use Match query for partial matching?
A: Yes, you can use Match query for partial matching by setting the operator to "or". However, for more control over partial matching, consider using the Match Phrase query or Wildcard query.

Q: How can I improve the performance of Match queries?
A: You can improve performance by optimizing your index (using appropriate mappings and settings), using filters where possible, and considering other query types like Term query for exact matches.

Q: Can Match query be used for numeric fields?
A: Yes, Match query can be used on numeric fields. However, for range queries on numeric fields, it's better to use Range query.

Q: How does the 'fuzziness' parameter work in Match query?
A: The 'fuzziness' parameter allows for fuzzy matching based on the Levenshtein edit distance. When set to "AUTO", it automatically adjusts the fuzziness based on the term length. It's useful for handling typos and misspellings in the search terms.

Subscribe to the Pulse Newsletter

Get early access to new Pulse features, insightful blogs & exclusive events , webinars, and workshops.