How to use filters in a query

Hello.
I am working with Elasticsearch 6.8.0 and am trying to follow the example on this page filter but cannot get the filter clause to produce the results I want. I am working with the sample dataset in the bank index and am trying to do something like the SQL statement SELECT firstname, lastname FROM BANK WHERE STATE = 'IL';. My query looks like this:

GET /bank/_search
{
  "query": {
    "bool" : {
      "must: {
        "query_string": {
          "default_field": "state",
          "query": "MD"
        }
      },
      "filter": {
        "term": {
          "gender": "F"
        }
      }
    }
  }
}

If I remove the filter clause, I get quite a few hits; with the filter clause, I get zero. What is going on with the filter?

Thanks in advance to all who respond.

your query (typo: only add " after must) worked well.

Response:

{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10,
      "relation" : "eq"
    },
    "max_score" : 3.5588508,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "HgZp6X4Bf0nakUP8t9-m",
        "_score" : 3.5588508,
        "_source" : {
          "account_number" : 126,
          "balance" : 3607,
          "firstname" : "Effie",
          "lastname" : "Gates",
          "age" : 39,
          "gender" : "F",
          "address" : "620 National Drive",
          "employer" : "Digitalus",
          "email" : "effiegates@digitalus.com",
          "city" : "Blodgett",
          "state" : "MD"
        }
      },...

Thank you! I will correct the typo and try again on Monday - headed out for the day in a few minutes! :smiley:

1 Like

Hi, Tomo - the typo exists in the code I typed into the post, since I can't copy the code from the Kibana console, as it is on a separate network. Having said that, the original code which DOES NOT work HAS the operator "must" properly quoted. Is there anything else I can look at? Thanks!

Another puzzling result: when I remove the filter, I get 28 hits but see only 10 entries. Why is that?

Elasticsearch returns only top 10 hits by default.

Thank you, that's what I thought. Is there a parameter I can set which controls it for this particular search? I see such a parameter in other search types, like in the Java API with the size() method.

See here. size is that.

Okay, I'll look in the docs I'm working with. That link points to the latest ES; I'm on 6.8 at the moment.

Is there anything else I can look at to see what is going on here? Thanks!

You can google "Elasticsearch 6.8 size" to find From / Size | Elasticsearch Guide [6.8] | Elastic

1 Like

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