# Elasticsearch query works in kibana but not code

**URL:** <https://discuss.elastic.co/t/elasticsearch-query-works-in-kibana-but-not-code/249032>\
**Category:** Kibana\
**Created:** [September 17, 2020, 8:17pm UTC](https://discuss.elastic.co/t/elasticsearch-query-works-in-kibana-but-not-code/249032 "2020-09-17T20:17:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![martinradio](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@martinradio](https://discuss.elastic.co/u/martinradio)\
**Post date:** [September 17, 2020, 8:17pm UTC](https://discuss.elastic.co/t/elasticsearch-query-works-in-kibana-but-not-code/249032/1 "2020-09-17T20:17:41Z")

</div>

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](https://us1.discourse-cdn.com/elastic/original/3X/5/c/5c14269e815893c71048c0d0ed8655299d6f7656.png)](https://i.stack.imgur.com/sqHnO.png)

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

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

```

It returns a much higher number:

```auto
{
  "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](https://us1.discourse-cdn.com/elastic/original/3X/8/f/8f4046eaf85dcd85a8520482cb6ac2def09c53a6.png)](https://i.stack.imgur.com/k9kYW.png)

---

<div class="post-metadata">

**Author:** ![wylie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wylie/32/81794_2.png) [@wylie](https://discuss.elastic.co/u/wylie)\
**Post date:** [September 17, 2020, 8:22pm UTC](https://discuss.elastic.co/t/elasticsearch-query-works-in-kibana-but-not-code/249032/2 "2020-09-17T20:22:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![martinradio](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@martinradio](https://discuss.elastic.co/u/martinradio)\
**Post date:** [September 17, 2020, 8:35pm UTC](https://discuss.elastic.co/t/elasticsearch-query-works-in-kibana-but-not-code/249032/3 "2020-09-17T20:35:00Z")

</div>

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

```auto
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

---

<div class="post-metadata">

**Author:** ![wylie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wylie/32/81794_2.png) [@wylie](https://discuss.elastic.co/u/wylie)\
**Post date:** [September 17, 2020, 8:50pm UTC](https://discuss.elastic.co/t/elasticsearch-query-works-in-kibana-but-not-code/249032/4 "2020-09-17T20:50:26Z")

</div>

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](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#query-string-syntax)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 15, 2020, 8:50pm UTC](https://discuss.elastic.co/t/elasticsearch-query-works-in-kibana-but-not-code/249032/5 "2020-10-15T20:50:34Z")

</div>

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