Using dismax with filter

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:

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:

{
    "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:

{
    "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 :unamused:. I am using Elasticsearch v2.4.

Can anybody guide me to a better solution if any?

Not to worry, I took a different approach based on cross_fields which gives me the desired results.

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