Elasticsearch query works in kibana but not code

I have a query string source.keyword: "chewy" and is_discontinued:"false" that if I run in the Kibana UI, returns the correct number: enter image description here

If I try to run the query in code though, it returns an incorrect number:

GET /indexname*/_count
{
        "query": {
          "bool": {
            "must": [
              {
                "query_string": {
                  "query":  "is_discontinued:'false' and source.keyword: 'chewy'"
                }
              }
           ]
          }
        } 
}

It returns a much higher number:

{
  "count" : 164978205,
  "_shards" : {
    "total" : 174,
    "successful" : 174,
    "skipped" : 0,
    "failed" : 0
  }
}

Previously I was getting an err for the is_discontinued field, so I refreshed the index like it said but my code query still doesnt seem to be recognizing the is_discontinued field enter image description here

Have you looked at the Inspect option to see exactly which query Kibana is generating? I'm pretty sure you're showing the KQL syntax, which is a little easier to use than the Lucene syntax that is expected in the query_string part of ES

good advice, i didnt know the inspect tab had that option; if I change my code query to be:

GET /indexname*_count
{
 "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "filter": [
              {
                "bool": {
                  "should": [
                    {
                      "match_phrase": {
                        "source.keyword": "chewy"
                      }
                    }
                  ],
                  "minimum_should_match": 1
                }
              },
              {
                "bool": {
                  "should": [
                    {
                      "match_phrase": {
                        "is_discontinued": "false"
                      }
                    }
                  ],
                  "minimum_should_match": 1
                }
              }
            ]
          }
        }
      ],
      "should": [],
      "must_not": []
    }
  }
  }

I can get the same number, although I'm still wondering what I could do to get this same result with a single queryString line

You'd have to convert your query to the right syntax, which is documented here. Single quotes might not work, there are no examples here: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#query-string-syntax

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