Jb R.
Jb R.

Reputation: 125

geo_distance filter only on specific terms of my query?

so here is my problem, I have query builder that builds a query according to the params that are sent to an API, here is one example of queries that it builds

{
"from": 0,
"size": 50,
"query": {
    "filtered": {
        "query": {
            "bool": {
                "should": [
                    {
                        "multi_match": {
                            "query": "nrc",
                            "fields": [
                                "name",
                                "sector.group.name",
                                "description",
                                "professionDescription"
                            ]
                        }
                    },
                    {
                        "term": {
                            "alternate": "true"
                        }
                    },
                    {
                        "term": {
                            "flagInitial": "true"
                        }
                    },
                    {
                        "term": {
                            "flagDistance": "true"
                        }
                    }
                ],
                "must": [],
                "minimum_should_match": 2
            }
        },
        "filter": {
            "geo_distance": {
                "distance": "80km",
                "institute.location": {
                    "lat": "48.866667",
                    "lon": "2.333333"
                }
            }
        }
    }
},
"track_scores": true,
"sort": {
    "institute.premium": {
        "order": "desc"
    },
    "_geo_distance": {
        "location": {
            "lat": "48.866667",
            "lon": "2.333333"
        }
    }
}

}

As you can see we have a multi-match, and different flags, and the whole query is filtered using geo_distance around given coords. In this case, the issue is that we don't want this filter to apply to the term "flagDistance", because if flagDistance=true, we want all the institutes that have flagDistance=true regardless where they are, but we still want this filter to apply to the other flags/multi_match of the query.

Any ideas how to do that ?

I was hoping I could add another query after the first (filtered one), but it returns an error

"query": {
    "filtered": {
        "query": {
            "bool": {
                "should": [
                    {
                        "multi_match": {
                            "query": "nrc",
                            "fields": [
                                "name",
                                "sector.group.name",
                                "description",
                                "professionDescription"
                            ]
                        }
                    },
                    {
                        "term": {
                            "alternate": "true"
                        }
                    },
                    {
                        "term": {
                            "flagInitial": "true"
                        }
                    }
                ],
                "must": [],
                "minimum_should_match": 2
            }
        },
        "filter": {
            "geo_distance": {
                "distance": "80km",
                "institute.location": {
                    "lat": "48.866667",
                    "lon": "2.333333"
                }
            }
        }
    },
    "bool": {
        "should": [
            {
                "term": {
                    "flagDistance": "true"
                }
            }
        ]
    }
}

Upvotes: 0

Views: 55

Answers (1)

Jb R.
Jb R.

Reputation: 125

so this is how I solved my problem, that should clause in my filter allows me to filter either if it's a flagDistance = true document, or if it's 80km's from the given coords

{
    "from": 0,
    "size": 50,
    "query": {
        "filtered": {
            "query": {
                "bool": {
                    "should": [
                        {
                            "multi_match": {
                                "query": "muc",
                                "fields": [
                                    "name",
                                    "sector.group.name",
                                    "description",
                                    "professionDescription"
                                ]
                            }
                        },
                        {
                            "term": {
                                "alternate": "true"
                            }
                        },
                        {
                            "term": {
                                "flagDistance": "true"
                            }
                        }
                    ],
                    "must": [],
                    "minimum_should_match": 2
                }
            },
            "filter": {
                "bool": {
                    "should": {
                        "geo_distance": {
                            "distance": "80km",
                            "institute.location": {
                                "lat": "48.866667",
                                "lon": "2.333333"
                            }
                        },
                        "term": {
                            "flagDistance": "true"
                        }
                    }
                }
            }
        }
    },
    "track_scores": true,
    "sort": {
        "institute.premium": {
            "order": "desc"
        },
        "_geo_distance": {
            "location": {
                "lat": "48.866667",
                "lon": "2.333333"
            }
        }
    }
}

Upvotes: 1

Related Questions