elastalert icon indicating copy to clipboard operation
elastalert copied to clipboard

HELP!! Negation, and, or does not work

Open throwawaythis opened this issue 9 years ago • 10 comments

Hi All,

Thanks in advanced for any help provided. I am having some very frustrating issues writing yaml rules with ElastAlert and Negations, and, or.

I cannot get Negations, and or to work, I have tried an exact copy of the example on the docs (ensuring every space is correct), I have tried other examples from issues listed here. Every time I get a similar error which is:

RequestError(400, u'parsing_exception', {u'status': 400, u'error': {u'line': 1, u'root_cause': [{u'reason': u'[and] query malformed, no start_object after query name', u'type': u'parsing_exception', u'line': 1, u'col': 206}], u'type': u'parsing_exception', u'reason': u'[and] query malformed, no start_object after query name', u'col': 206}})

This works:

filter:
    - term:
        field1: "value"
    - term:
        field2: "value"

Any of the following DO NOT work:

filter:
- and:
    - term:
        field1: "value"
    - term:
        field2: "value"

OR

filter:
- or:
    - term:
        field: "value"
    - wildcard:
        field: "foo*bar"
    - and:
        - not:
            term:
              field: "value"
        - not:
            term:
              _type: "something"

I have checked the correct spacing in front of every line (magical 4 spaces) no matter what I do I get the same error. I am using the latest version of ElastAlert and Elastic Search 5.2 (just upgraded from 5.x but no difference)

Many thanks

throwawaythis avatar Mar 03 '17 15:03 throwawaythis

This is a 5.x issue. You can use the workaround here #912

kureus avatar Mar 03 '17 15:03 kureus

Thanks is there any plans to fix this?

throwawaythis avatar Mar 03 '17 16:03 throwawaythis

Is a easily fixable by elastalert I wonder? The not, and and or queries have been completely removed from the DSL as of 5.0 (having been deprecated previously in 2.0), to be replaced by the appropriate combinations of bool queries with must, should or must_not clauses as appropriate

https://www.elastic.co/guide/en/elasticsearch/reference/current/breaking_50_search_changes.html#_deprecated_queries_removed

Something like the following should work (may be typos as no ES cluster to hand), unless there is are still other issues with in the ES5 filter code I don't know about. (I haven't actually tested complex conditional queries personally as of yet)


filter:
- bool:
    must:
        - term:
            field1: "value"
        - term:
            field2: "value"

  
filter:
- bool:
    should:
         - term:
             field: "value"
         - wildcard:
             field: "foo*bar"
         - bool:
             must_not:
                 - term:
                     field: "value"
                 - term:
                     _type: "something

mircopolo avatar Mar 04 '17 02:03 mircopolo

Is this issue fixed? How to use must or must_not?

zxq1002 avatar Nov 20 '17 08:11 zxq1002

@mircopolo As you said and, or and not won't work with an ES5+ cluster. In ES they should be replaced by should, must, must_not from the bool filter type.

I tried a lot of bool filters, some wre complex, some were very simple, similar to the first example you gave:

filter:
  - bool:
    should:
      - term:
        field1: 'value'
      - term:
        field2: /regex/

But none worked. I always have this error message:

Error running query: TransportError(400, u'parsing_exception', u'[bool] query malformed, no start_object after query name')

And the documentation is still very outdated https://elastalert.readthedocs.io/en/latest/recipes/writing_filters.html#negation-and-or, only talking about the old syntax.

The documention should give a compatibility map, of which queries are supported for each ES version supported.

bool doesn't seem to be supported or if someone can happen to make it works please share your valid filter.

noraj avatar Jun 13 '18 12:06 noraj

The examples and documentation are a bit dated. I'd recommend using query strings if you are using ES5 or ES6. You should be able to construct boolean logic.

Example:

filter:
- query_string:
     query: "(NOT field1: value OR field2: value) AND field3: value"

If someone wants to add better support for using the ES6 syntax for bool queries, I'd happily accept the changes.

Qmando avatar Jun 13 '18 18:06 Qmando

[Hi Qmando

I am facing same issue after elastic search upgrade from 2.x to 5.x filter not working.

Filter which was in 2.x below same is not working after elastic search upgrad to 5.x

filter:
- or:
  - term:
        sp-message: "Got exception:"
  - term:
        sp-message: "Exception while trying to run program"
  - wildcard:
       sp-message: "*Unexpected error:*"
  - wildcard:
       sp-message: "*Thrift error for org.apache.tephra.distributed.*"

So now i am trying to change the filter to acheive the same which is not working.

filter:
   - query:
      query_string:
         query: 'sp-message: \"Got exception\:\" OR sp-message: \"*Unexpected error\:*\" OR sp-message: \"*Thrift error for org.apache.tephra.distributed.*\"'

So when i use the wildcard in the query it is not pulling all matches its only pulling latest match. Could you please help me acheiving same. i used * inside query but github not showing * when i submit it.]

Please check attached link since correct text file attached needhelp.txt

Prabhakarg1 avatar Aug 24 '18 11:08 Prabhakarg1

Try:

filter:
 - query:
      query_string:
        query: "\"Got exception\" OR \"Unexpected error\" OR \"Exception while trying to run program\" OR \"Thrift error for org.apacje.tephra.distributed\""
        default_field: sp-message

This should work if sp-message is using the default analyzer. You don't actually need the *s unless it's an unanalyzed field or there's no space in between words (like somethingUnexpected or error:Timeout).

By the way, you can remove formatting on github by surrounding text with triple backticks, ```

Qmando avatar Aug 24 '18 16:08 Qmando

It would be a great feature to be able to put directly the kibana syntax in the rule.yaml

I think of something like directly the query object in order to pass it directly to ES, elastalert would juste add the range for the date

Maybe a simple way (that wouldn't break anything) would be to have a new entry query in the yaml

If this query is there, it is passed directly to ES

in

elastalert/elastalert.py
    def get_query

        es_filters = {'filter': {'bool': {'must': filters}}}

... if query is present... 
        es_filters = {'filter': query}

didi-1101 avatar Jun 22 '20 06:06 didi-1101

Hi @Qmando , I am facing similar problems with AND in filters. Mentioned in #3119. I am trying with

filter:                                                                                                                                      
- query:                                                                                                                                     
      query_string:                                                                                                                            
          query: "r1_ AND r2_ AND r3_"                                                                                                 
          default_field: rule_name

and I don't get any hits, but if I just put

filter:                                                                                                                                      
- query:                                                                                                                                     
      query_string:                                                                                                                            
          query: "r1_ "                                                                                                 
          default_field: rule_name

I get 1 hit (as expected) but not working with more values. Neither working this way (again for only 1 term it gets 1 hit but not for more values)

filter:
- term:                                                                                                                                     
      rule_name: "r1_"                                                                                                              
- term:                                                                                                                                     
      rule_name: "r2_"                                                                                                                
- term:                                                                                                                                     
      rule_name: "r3_"

Because as I say in #3119, using

terms: 
rule_name:                                                                                                                              
   - r1_                                                                                                                   
   - r2_                                                                                                                        
   - r3_

filter as OR, not AND that is what I need

Thanks, Bea

beaesteban01 avatar Feb 16 '21 12:02 beaesteban01