A
A
Alexander Yelagin2015-04-18 14:18:51
PHP
Alexander Yelagin, 2015-04-18 14:18:51

How to build an Elasticsearch query to filter by a regex list?

Hello.
There is a task - to form a request to fetch data from Elasticsearch, provided that a list of word values ​​can be specified in the form of regular expressions + a list of exception words (without using regular expressions).
I didn’t figure out how to pass the list of values ​​for the regular expression condition to elastic, so I separated each regular expression condition with the “|” symbol, I’m wondering how it can be done differently.
In the example, we are looking for matches for "cartoon.*" and "movie.*", and in the "must_not" block - exception words. Tell me how to write a query so as not to be smart with the symbol "|". And another off-topic question, is it possible to somehow check the correct regular expression, make it validate, for example, in PHP?

"query": { "regexp":{
            "word" : "мульт.*|фильм.*"
        }},    
    "filter": {
      "bool": {
            "must_not": [
             {"terms": {
                "word": ["видео", "скачать"]
             }}
          ]                  
      }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MintTea, 2015-04-18
@juniorspecialist

For "or" type filtering, you can use the "should" section of the "bool" filter you are using. More or less like this:

curl -XGET 'http://localhost:9200/_search' -d '{
    "filter": {
        "bool": {
            "should": [
                {
                    "regexp": {
                        "word": "мульт.*"
                    }
                },
                {
                    "regexp": {
                        "word": "фильм.*"
                    }
                }
            ],
            "must_not": {
                "terms": {
                    "word": ["видео", "скачать"]
                }
            }
        }
    }
}'

Validation of regular expressions is possible through elastic ( Validating api ):
curl -XGET 'http://localhost:9200/_validate/query?explain' -d '{
    "query": {
        "regexp": {
            "word": "мульт.*"
        }
    }
}'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question