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:
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" } } } }'
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.*" ...