Using logical AND query in Kibana

I am trying to implement a simple query in Kibana and then in C# NEST. I am from a SQL background and am struggling to find documentation to perform a relatively simple SQL query in Elasticsearch. The query I am trying to implement is effectively......

SELECT * FROM table 
WHERE logdate > STR_TO_DATE('2020-02-01 23:59:59','%Y-%m-%d %H:%i:%s')
AND logdate < STR_TO_DATE('2020-02-03 00:00:00','%Y-%m-%d %H:%i:%s')
AND username = 'user01'

I can do them as individual kibana queries....

GET table/_search
{
  "query": {
    "range": {
      "logdate": {
        "gt": "2020-02-01T23:59:59",
        "lt": "2020-02-03T00:00:00"
      }
    }
  }
}

GET table/_search
{
  "query": {
    "match": {
      "username": "user01"
    }
  }
}

But when I try and combine them into one query I get a "parsing_exception" error...

GET table/_search
{
  "query" : {
    "bool" : {
      "must": [
        {
          "match": {
            "username": "user01"
          },
          "range": {
            "logdate": {
              "gt": "2020-02-01T23:59:59",
              "lt": "2020-02-03T00:00:00"
            }
          }
        }
      ]
    }
  }
}

I have looked at the Elasticsearch documentation and can't seem to find the answer so any help would be appreciated, thanks in advance

Try this one:

{
  "query" : {
    "bool" : {
      "must": [
         {
          "match": {
            "username": "user01"
          }
         },
         {
           "range": {
            "logdate": {
              "gt": "2020-02-01T23:59:59",
              "lt": "2020-02-03T00:00:00"
            }
          }
        }
      ]
    }
  }
}

Excellent works a treat, thanks you for your help. Now to try and get it working with NEST.

Have tried converting to NEST but have errors as string to double conversion on dates, then cannot convert lambda expressions to types 'Nest.ISearchRequest'
& 'Nest.QueryContainer

Client.SearchAsync<AuditRecord>(s => s
.From(0)
.Take(10)
.Query(q => q
.Bool(b => b
.Must(m => m
.Range(r => r
.Field("Timestamp")
.GreaterThan("2017-03-07T13:30:00")
.LessThan("2017-03-07T13:40:00")
)
)
.MatchPhrase(mp => mp   
.Field("Action")
.Query("Week No: 30")
)                                    
)
)).Result.Documents.ToList();

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.