elastalert icon indicating copy to clipboard operation
elastalert copied to clipboard

Reproduce a query in elastalert with should/must/must_not

Open gpoire opened this issue 8 years ago • 3 comments

Hello everyone, I'm trying to adapt this query in a rules in elastalert but it doesn't really work for the moment

Elasticsearch request : GET /_search { "query": { "bool" : { "should" : [ {"match_phrase" : { "message" : "one sentence" }}, {"match_phrase" : { "message" : "another" }} ] } } }

Elastalert rules :

filter:

  • query: bool: should: match_phrase: "message": "one sentence" match_phrase: "message": "another"

Have someone already solve this kind of problem ?

Thanks for your help

gpoire avatar Dec 18 '17 16:12 gpoire

ElastAlert only does filter based queries, not scored, so should is not really used.

You probably want something like:

filter:
 - query:
      query_string:
        query: "\"one sentence\" OR another"
        default_field: message

Qmando avatar Dec 18 '17 18:12 Qmando

Finally i find how to do it, this how I do to resolve my problem : filter:

  • bool: should: - match_phrase: "message": "one sentence" - match_phrase: "message": "another"

The problem with using query_string instead of match_phrase is that it will match if there is "one" "another" or "sentence" for example :

  • "one lsdkfjskljflksdjf" -> will match
  • "sentence" -> will match

but what I need is to exactly match if the sentence contains "one sentence" or "another" :

  • "one sentence qqslkdjsqkldjslkj" -> will match
  • "one mqlkdqsmldkqmldksq" -> will not match

Maybe there is another way to do it but it's work for me, thanks for your help Qmando :)

gpoire avatar Dec 19 '17 10:12 gpoire

If you keep the escaped quotation marks in the query string like I gave in my example, it will only match on full phrases. see https://www.elastic.co/guide/en/elasticsearch/reference/5.5/query-dsl-query-string-query.html

Qmando avatar Dec 19 '17 17:12 Qmando