1

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?

1 Answer 1

1

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"
          }
        }
      ]        
    }
  }
}
Sign up to request clarification or add additional context in comments.

5 Comments

it does give me the same results.. my idea for using filter was to make the query a bit faster, but it seems it takes the same about of time
Filter and should, in the example you provided, will give you the same results (and I assume be approximately the same in expense). The only difference is that should will be scored and filter will not.
@Patrioticcow So that is a slightly different question than just updating the existing query to function as a filter, but there are a few approaches you could take. It may be best to swap out the query string operation entirely and search individual terms as an array.
I edited it one more possible approach you could take if you can get away from the query_string query. Let me know if this has a performance improvement for you if you're able to do so.
seems like query_string is a bit faster than breaking it down using should

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.