Using a regexp query/filter

In the previous recipes, we saw different term queries (terms, fuzzy, and prefix). Another powerful terms query is the regexp (regular expression) query.

Getting ready

You will 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 regexp query/filter, perform the following steps:

  1. Execute a regexp term query from the command line:
    curl -XPOST 'http://127.0.0.1:9200/test-index/test-type/_search?pretty=true' -d '{
      "query": {
        = "regexp": {
          "parsedtext": {
            "value": "j.*",
            "flags" : "INTERSECTION|COMPLEMENT|EMPTY"
          }
        }
      }
    }'
    
  2. If scoring is not important, it's better to reformulate the query ...

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.