0

I am working on translating a Splunk query to Elasticsearch DSL. I want to check if a URL in the logs contains something like:

"script>" OR "UNION ALL SELECT"

Fair enough I thought, went to the doc, and:

{
  "regexp": {
    "http.url": "script>"
  }
}

Elasticsearch (2.3) replies:

"root_cause": [ { "reason": "failed to parse search source. unknown search element [regexp]", "type": "search_parse_exception", "line": 2,

Could someone enlighten me please about these kinds of queries?

1 Answer 1

2

This is a pretty straightforward mistake when starting out with the documentation. In the docs, we generally only show the raw query (and its parameters). Queries are either compound queries or leaf queries. regexp is an example of a leaf query.

However, that's not enough to actually send the query. You're missing a simple wrapper part of the DSL for any query:

{
  "query": {
    "regexp": {
      "http.url": "script>"
    }
  }
}

To use a compound query, the best way is to use the bool compound query.

It has must, must_not, should, or filter and each accept an array of queries (or filters, which are just scoreless, cacheable queries). should is the OR-like aspect of it, but do read the docs on how it behaves when you add must alongside it. The gist is that should by itself is exactly like an OR (as shown below), but if you combine it with must, then it becomes completely optional without using "minimum_should_match": 1.

{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "http.url": "script>"
          }
        },
        {
          "term": {
            "http.url": "UNION ALL SELECT"
          }
        }
      ]
    }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I get that now. Sorry for the confusion. This query seems to work. It would be great if you could tell me how to apply OR logic to the queries as well. I didn't made that too explicit in my question though.
There you go. I took the liberty of changing regexp to term, which assumes exact match behavior.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.