Search and filter with the lists

I would like to search ES and then filter it by the list of name:

    "query_string": {
        "query": "car"
    }

and I have list of names which I want to match: l = ["super name 1", "super name 2"]. So I will use terms:

  "query": {
    "bool": {
      "must": {
        "query_string": {
            "query": "car"
        }
      },
      "filter": {
        "terms": {
          "name": ["super_name", "super name 1"]
        }
      }
    }
  }

so this query will find super_name, but not "super name 1" - I think because of the analyzer. I could change the mapping but I'm not sure if this not disturb the search of "name" field. So I could use match + should, but I don't feel this right approach:

  "query": {
    "bool": {
      "must": {
        "query_string": {
            "query": "car"
        }
      },
      "filter": {
        "match": {
          "name": {
            "query": "super name 1",
            "operator": "and"
          }
        }
      }
    }
  }

now it will match all the words in query.

I feel that I do everything wrong... What could I improve?

I would copy the "name" field into a field called "name.raw", where you use this field for filtering and aggregations. This is done in your mappings and a re-index operation.

For mapping a change like this:

"name": {
  "type": "text",
  "fields": {
    "raw": {
      "type": "text",
      "index": "not_analyzed"
    }
  },
  "analyzer": "your name analyzer goes here"
}

Then modify your first query to use the name.raw field in the terms query.

 "name.raw": ["super_name", "super name 1"]

Thanks for the answer!

It the one solution that I was thinking about, but I am sure if like duplication of the data (Am I right?). But indeed I want to have field that is analyzed some times and some times not analyzed.

Why is it better than creation of multiple matches + should in DSL query?

I prefer solutions where the complete query is as simple as possible as these queries are scaling better. So if your indices are small, then go for both approaches and see what suites you best.

That said, I would prefer not to mingle with an analyzer, that's works for your basic search cases unless I do have the time needed to do it properly. This is a time consuming task.

BTW

If you are using ES v. 5.X, then this blog presents the idea about how to organize text for both classic full-text search and keyword search as you are needing in your case.

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