geo_distance仅过滤我查询的特定条款?

时间:2017-12-11 06:59:06

标签: elasticsearch

所以这是我的问题,我有查询构建器根据发送到API的参数构建查询,这里是它构建的查询的一个示例

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

}

正如您所看到的,我们有一个多匹配和不同的标志,整个查询使用围绕给定坐标的geo_distance进行过滤。在这种情况下,问题是我们不希望此过滤器应用于术语" flagDistance",因为如果flagDistance = true,我们希望所有具有flagDistance = true的机构,无论它们在哪里是,但我们仍然希望此过滤器应用于查询的其他flags / multi_match。

任何想法如何做到这一点?

我希望我可以在第一个(已过滤的)之后添加另一个查询,但它会返回错误

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

1 个答案:

答案 0 :(得分:1)

所以这就是我解决问题的方法,我的过滤器中的if子句允许我过滤它是否是一个flagDistance = true文档,或者它是否来自给定的coords 80km

 select * from table WHERE date < now() - '1 day'::interval;
相关问题