Book Image

ElasticSearch Cookbook

By : Alberto Paro
Book Image

ElasticSearch Cookbook

By: Alberto Paro

Overview of this book

Table of Contents (20 chapters)
ElasticSearch Cookbook Second Edition
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

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 as a filter in this way:

    curl -XPOST 'http://127.0.0.1:9200/test-index/test-type/_search?pretty=true' -d '{
      "query": {
        "filtered": {
          "filter": {
            "regexp": {
              "parsedtext": "j.*"
           ...