18

I want to get the results that do not match "statusCode": 200

In order to match text from field you use

GET index01/_search?pretty
{
  "query":{
    "match":{
      "statusCode": 200
    }
  }
}

I tried something like this:

GET ucs_heartbeat/_search?pretty
{
  "query":{
    "match":{
      "statusCode":{
        "query": 200,
        "operator": "must_not"
      }
    }
  }
}

According to: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html

1
  • 1
    note that there is no operator called must_not for match query. Only available operators are and(default) and or. Consider using term query over match query for all structured fields like keyword, numbers Commented Apr 6, 2017 at 10:51

2 Answers 2

37

Try this instead

GET ucs_heartbeat/_search?pretty
{
  "query": {
    "bool": {
      "must_not": [
        {
          "term": {
            "statusCode": 200
          }
        }
      ]
    }
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, is it possible to perform this kind of search by using a regexp search(for a String -text and keyword- type field), something like- "bool":{"must":[{"regexp": {"statusCode.keyword": {"value": "~(200)","flags": "ALL"}}}]} , and also, is there any difference in performance, if this query works ?
@soumitragoswami regexp queries are notably slower. If you know in advance what and how you're going to query you can prepare your data at indexing time so that regexp queries are not necessary anymore. Your mileage may vary, so you should definitely try it out and then if you hit performance issues, look for other possible indexing solutions. Feel free to create a new question with your specific issue, though
Hi, I created a question on this - stackoverflow.com/questions/59410594/…
0

A possible string query could be:

{
    "query": {
        "query_string": {
            "query": "NOT statusCode: 200"
        }
    },
    "size": 10,
    "from": 0,
    "sort": []
}

Comments

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.