后过滤
您可以通过布尔过滤器或使用 post_filter 参数来实现后过滤。
布尔过滤器配合近似最近邻搜索
布尔过滤器由一个包含 k-NN 查询和过滤器的布尔查询构成。例如,以下查询首先搜索距离指定location最近的酒店,然后过滤结果以返回评分在 8 到 10 之间(含)且提供停车场的酒店:
POST /hotels-index/_search
{
"size": 3,
"query": {
"bool": {
"filter": {
"bool": {
"must": [
{
"range": {
"rating": {
"gte": 8,
"lte": 10
}
}
},
{
"term": {
"parking": "true"
}
}
]
}
},
"must": [
{
"knn": {
"location": {
"vector": [
5,
4
],
"k": 20
}
}
}
]
}
}
}
响应包含匹配酒店的文档:
{
"took" : 95,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 0.72992706,
"hits" : [
{
"_index" : "hotels-index",
"_id" : "3",
"_score" : 0.72992706,
"_source" : {
"location" : [
4.9,
3.4
],
"parking" : "true",
"rating" : 9
}
},
{
"_index" : "hotels-index",
"_id" : "6",
"_score" : 0.3012048,
"_source" : {
"location" : [
6.4,
3.4
],
"parking" : "true",
"rating" : 9
}
},
{
"_index" : "hotels-index",
"_id" : "5",
"_score" : 0.24154587,
"_source" : {
"location" : [
3.3,
4.5
],
"parking" : "true",
"rating" : 8
}
}
]
}
}
post_filter 参数
如果您将 knn 查询与过滤器或其他子句(例如 bool、must、match)一起使用,返回的结果可能会少于 k 个。在此示例中,post_filter 将结果数量从 2 个减少到 1 个:
GET my-knn-index-1/_search
{
"size": 2,
"query": {
"knn": {
"my_vector2": {
"vector": [2, 3, 5, 6],
"k": 2
}
}
},
"post_filter": {
"range": {
"price": {
"gte": 5,
"lte": 10
}
}
}
}