Answer the question
In order to leave comments, you need to log in
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
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": ["видео", "скачать"]
}
}
}
}
}'
curl -XGET 'http://localhost:9200/_validate/query?explain' -d '{
"query": {
"regexp": {
"word": "мульт.*"
}
}
}'
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question