# Search and filter with the lists

**URL:** https://discuss.elastic.co/t/search-and-filter-with-the-lists/72154
**Category:** Elasticsearch
**Created:** [January 19, 2017, 10:53am UTC](https://discuss.elastic.co/t/search-and-filter-with-the-lists/72154 "2017-01-19T10:53:59Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![sacherus](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sacherus/32/14723_2.png) [@sacherus](https://discuss.elastic.co/u/sacherus)
#### Post date: [January 19, 2017, 10:53am UTC](https://discuss.elastic.co/t/search-and-filter-with-the-lists/72154/1 "2017-01-19T10:53:59Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![GyllingSW](https://avatars.discourse-cdn.com/v4/letter/g/6f9a4e/32.png) [@GyllingSW](https://discuss.elastic.co/u/GyllingSW)
#### Post date: [January 20, 2017, 1:14pm UTC](https://discuss.elastic.co/t/search-and-filter-with-the-lists/72154/2 "2017-01-20T13:14:49Z")

</div>

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"]
```

---

<div class="post-metadata">

### Author: ![sacherus](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sacherus/32/14723_2.png) [@sacherus](https://discuss.elastic.co/u/sacherus)
#### Post date: [January 21, 2017, 10:23pm UTC](https://discuss.elastic.co/t/search-and-filter-with-the-lists/72154/3 "2017-01-21T22:23:59Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![GyllingSW](https://avatars.discourse-cdn.com/v4/letter/g/6f9a4e/32.png) [@GyllingSW](https://discuss.elastic.co/u/GyllingSW)
#### Post date: [January 23, 2017, 9:24am UTC](https://discuss.elastic.co/t/search-and-filter-with-the-lists/72154/4 "2017-01-23T09:24:57Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![GyllingSW](https://avatars.discourse-cdn.com/v4/letter/g/6f9a4e/32.png) [@GyllingSW](https://discuss.elastic.co/u/GyllingSW)
#### Post date: [January 23, 2017, 9:30am UTC](https://discuss.elastic.co/t/search-and-filter-with-the-lists/72154/5 "2017-01-23T09:30:12Z")

</div>

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.

> **[Elasticsearch replaces string type with two new types text and keyword.](https://www.elastic.co/blog/strings-are-dead-long-live-strings)**
>
> On using text types for full text search and keyword type for keyword search in Elasticsearch 5.0.

---

<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: [February 20, 2017, 9:30am UTC](https://discuss.elastic.co/t/search-and-filter-with-the-lists/72154/6 "2017-02-20T09:30:25Z")

</div>

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