# How to filter two fields based on a list of values

**URL:** https://discuss.elastic.co/t/how-to-filter-two-fields-based-on-a-list-of-values/292846
**Category:** Elasticsearch
**Created:** [December 23, 2021, 8:55pm UTC](https://discuss.elastic.co/t/how-to-filter-two-fields-based-on-a-list-of-values/292846 "2021-12-23T20:55:38Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Muharez](https://avatars.discourse-cdn.com/v4/letter/m/b5ac83/32.png) [@Muharez](https://discuss.elastic.co/u/Muharez)
#### Post date: [December 23, 2021, 8:55pm UTC](https://discuss.elastic.co/t/how-to-filter-two-fields-based-on-a-list-of-values/292846/1 "2021-12-23T20:55:38Z")

</div>

Hi there!  
I have just started learning Elasticsearch.  
I created an index with four fields that contain information on distances between pairs of postcodes.

```auto
{
  "distances" : {
    "mappings" : {
      "properties" : {
        "dest" : {
          "type" : "keyword"
        },
        "meters" : {
          "type" : "float"
        },
        "seconds" : {
          "type" : "float"
        },
        "src" : {
          "type" : "keyword"
        }
      }
    }
  }
}

```

on 'src' and 'dest' fields I have 29 million pairs of postcodes and on 'meters' and 'seconds' distances between each pair. Assume that these pairs of postcodes are all the postcodes of a city.  
If I have a list of postcodes, for example, a list of 200 postcodes, and if I wanted to filter all the pairs that are in my list and no other postcode out of this list, how can I filter the index and how the query should look like?  
I made a query like this:

```auto
for c in cluster_10:
    search_body = {
        "size":120000,
        "query":{
            "multi_match":{
                "query":c,
                "fields":["src", "dest"]
            }
        }
    }
result = es.search(index="distances", body=search_body)
print(json.dumps(result, indent = 1))

```

where cluster\_10 is the list of the postcodes that I want to filter out, the problem is that the result contains distances between the postcodes in my list, and those out of the list. I can see why, but I don't know how to limit the results to only the codes in the list for both fields 'src' and 'dest'.

---

<div class="post-metadata">

### Author: ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)
#### Post date: [December 24, 2021, 10:48am UTC](https://discuss.elastic.co/t/how-to-filter-two-fields-based-on-a-list-of-values/292846/2 "2021-12-24T10:48:23Z")

</div>

multi\_match query uses best\_fields by default. it matches if any of the field matches the query.  
I suppose boolean query with terms query is a simple solution.

```auto
"query":{
  "bool":{
    "must":[
      {"terms":{"src": c}},
      {"terms":{"dest": c}}
    ]
  }
}

```

---

<div class="post-metadata">

### Author: ![Muharez](https://avatars.discourse-cdn.com/v4/letter/m/b5ac83/32.png) [@Muharez](https://discuss.elastic.co/u/Muharez)
#### Post date: [December 31, 2021, 3:03pm UTC](https://discuss.elastic.co/t/how-to-filter-two-fields-based-on-a-list-of-values/292846/4 "2021-12-31T15:03:34Z")

</div>

Thank you very much Tomo\_M  
It didn't work that way but helped me to find the correct query. I just needed to use 'filter' query instead of 'must and give a list of values to be filtered (and not one by one through a for loop).

```auto
        "query":{
            "bool":{
                "filter":[
                    {"terms":{"src":list_of_values}},
                    {"terms":{"dest":list_of_values}}
                ]
            }
        }

```

Bests,

---

<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: [January 28, 2022, 3:04pm UTC](https://discuss.elastic.co/t/how-to-filter-two-fields-based-on-a-list-of-values/292846/5 "2022-01-28T15:04:09Z")

</div>

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