# Using dismax with filter

**URL:** <https://discuss.elastic.co/t/using-dismax-with-filter/73801>\
**Category:** Elasticsearch\
**Created:** [February 3, 2017, 7:14am UTC](https://discuss.elastic.co/t/using-dismax-with-filter/73801 "2017-02-03T07:14:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gmajivu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gmajivu/32/12878_2.png) [@gmajivu](https://discuss.elastic.co/u/gmajivu)\
**Post date:** [February 3, 2017, 7:14am UTC](https://discuss.elastic.co/t/using-dismax-with-filter/73801/1 "2017-02-03T07:14:15Z")

</div>

I have a scenarion where I need to use dismax for better scoring against each field and also be able to filter. Here is a simple example:

```bash
Person First_Name Last_Name State
1 Sally Simla DE
2 Rick Yala NY
3 Kevo Sita DE
4 Didi Jones NY
5 Simla Kyle DC

```

Searching by name only `first_name=simla&last_name=simla` should give persons 1 and 2. I have the following query to achieve this:

```auto
{
    "query": {
        "dis_max" : {
            "queries" : [
                { 
                    "bool" : {
                        "must" : [{"term" : {"first_name" : "simla"}}]
                    }
                },
                { 
                    "bool" : {
                        "must" : [{"term" : {"last_name" : "simla"}}]
                    }
                }
          ]
        }
    }
}

```

The other case is when further to the search above, I need to filter based on state `state=DE` ton only get person 1. For this I have the following query:

```auto
{
    "query": {
        "dis_max" : {
            "queries" : [
                { 
                    "bool" : {
                        "must" : [{"term" : {"first_name" : "simla"}}],
                        "filter": [
                            {
                                "or": [
                                    {"missing": {"field": "state"}},
                                    {"match": {"state": "DE"}}
                                ]
                            }
                        ]
                    }
                },
                { 
                    "bool" : {
                        "must" : [{"term" : {"last_name" : "simla"}}],
                        "filter": [
                            {
                                "or": [
                                    {"missing": {"field": "state"}},
                                    {"match": {"state": "DE"}}
                                ]
                            }
                        ]
                    }
                }
          ]
        }
    }
}

```

I feel there is a bit of repetition especially when applying the filter. In case we have more filters this query could get really ugly 😒. I am using Elasticsearch v2.4.

Can anybody guide me to a better solution if any?

---

<div class="post-metadata">

**Author:** ![gmajivu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gmajivu/32/12878_2.png) [@gmajivu](https://discuss.elastic.co/u/gmajivu)\
**Post date:** [February 7, 2017, 11:43am UTC](https://discuss.elastic.co/t/using-dismax-with-filter/73801/2 "2017-02-07T11:43:19Z")

</div>

Not to worry, I took a different approach based on [cross\_fields](https://www.elastic.co/guide/en/elasticsearch/guide/2.x/_cross_fields_queries.html) which gives me the desired results.

---

<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:** [March 7, 2017, 11:43am UTC](https://discuss.elastic.co/t/using-dismax-with-filter/73801/3 "2017-03-07T11:43:41Z")

</div>

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