Patrioticcow
Patrioticcow

Reputation: 27058

How to use query_string with the filter in elasticsearch?

I have a simple query

GET data/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "prefix": {
            "last_name": "test"
          }
        },
        {
          "bool": {
            "should": {
              "query_string": {
                "query": "henderson OR Boulder City OR Sloan",
                "fields": [
                  "city_*"
                ]
              }
            }
          }
        }
      ]
    }
  }
}

I would like to change the query_string to a filter. I'm not sure how to convert

      {
          "bool": {
            "should": {
              "query_string": {
                "query": "henderson OR Boulder City OR Sloan",
                "fields": [
                  "city_*"
                ]
              }
            }
          }
        }

into something like

      "filter": {
        "query_string": {
              "query": "henderson OR Boulder City OR Sloan",
              "fields": [
                  "city_*"
              ]
          }
      }

and make sure it filters by all this cities henderson OR Boulder City OR Sloan and by all this fields city_*.keyword

Any ideas?

Upvotes: 1

Views: 1442

Answers (1)

Miek
Miek

Reputation: 1228

Change

{
      "bool": {
        "should": {
          "query_string": {
            "query": "henderson OR Boulder City OR Sloan",
            "fields": [
              "city_*"
            ]
          }
        }
      }
    }

to

  {
      "bool": {
        "filter": {
          "query_string": {
            "query": "henderson OR Boulder City OR Sloan",
            "fields": [
              "city_*"
            ]
          }
        }
      }
    }

Does this get your desired behavior? Using the same query under a filter should give you the same results as should, but the scoring will not be weighted by the query.

Edit - One more recommendation I would make is to adjust your query to this:

GET data/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "prefix": {
            "last_name": "test"
          }
        }
      ],
      "filter": [
        {
          "query_string": {
            "query": "henderson OR Boulder City OR Sloan",
              "fields": [
                "city_*"
                ]
          }
        }
      ]        
    }
  }
}

Edit 2 - You may be able to move away from using query_string, does this give you any speed increase? (you could change should to the "shoulds" to be nested within a filter as well if you want them unscored)

GET data/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "prefix": {
            "last_name": "test"
          }
        }
      ],
      "should": [
        {
          "match": {
            "city_*": "henderson"
          }
        },
        {
          "match": {
            "city_*": "Boulder City"
          }
        },
        {
          "match": {
            "city_*": "Sloan"
          }
        }
      ]        
    }
  }
}

Upvotes: 1

Related Questions