Using a match query

ElasticSearch provides a helper to build complex span queries that depend on simple preconfigured settings. This helper is called a match query.

Getting ready

You need a working ElasticSearch cluster and an index populated with the script chapter_05/populate_query.sh, available in the code bundle for this book.

How to do it...

In order to execute a match query, perform the following steps:

  1. The standard usage of a match query simply requires the field name and the query text:
    curl -XPOST 'http://127.0.0.1:9200/test-index/test-type/_search?pretty=true' -d '{
      "query": {
        "match" : {
          "parsedtext" : {
            "field": "nice guy",
            "operator": "and"
          }
        }
      }
    }'
    
  2. If you need to execute the same query as a phrase query, the type of match changes ...

Get ElasticSearch Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.