我想对弹性搜索执行查询,如下所示:
(cond1)AND(cond2或cond3或cond4或cond5或cond6)
我可以通过使用
创建一个bool过滤器来实现此结果bool {
filter {
bool {
must [ cond1 ]
should [ cond2, cond3, cond4, cond5, cond6]
}
}
}
但我应用的字段条件是嵌套字段,并在2个括号中共享常见的嵌套祖先。
为了解决这个问题,我将条件转换为产品形式的总和并创建了像这样的过滤器
bool {
filter {
bool {
should{
bool { must [cond1, cond2] },
bool { must [cond1, cond3] },
bool { must [cond1, cond4] },
bool { must [cond1, cond5] },
bool { must [cond1, cond6] }
}
}
}
这解决了按预期使条件正确的问题。
但是,如果cond1在地理距离滤波器上,则会导致查询效率非常低,因为弹性搜索不会缓存地理距离滤波器的位集,因此会导致每个文档计算地理距离条件5次。
如何才能使查询更有效?
我的一个疑问是
{
"bool" : {
"filter" : {
"bool" : {
"should" : [ {
"bool" : {
"must" : [ {
"nested" : {
"query" : {
"geo_distance_range" : {
"loc.address.lat_lon" : [ 67.0, -40.9 ],
"from" : "0mi",
"to" : "100mi",
"include_lower" : true,
"include_upper" : false,
"distance_type" : "plane"
}
},
"path" : "loc"
}
}, {
"match" : {
"name" : {
"query" : "name",
"type" : "boolean"
}
}
} ]
}
}, {
"bool" : {
"must" : [ {
"nested" : {
"query" : {
"geo_distance_range" : {
"loc.address.lat_lon" : [ 67.0, -40.9 ],
"from" : "0mi",
"to" : "100mi",
"include_lower" : true,
"include_upper" : false,
"distance_type" : "plane"
}
},
"path" : "loc"
}
}, {
"match" : {
"altName" : {
"query" : "alt",
"type" : "boolean"
}
}
} ]
}
}, {
"bool" : {
"must" : [ {
"nested" : {
"query" : {
"geo_distance_range" : {
"loc.address.lat_lon" : [ 67.0, -40.9 ],
"from" : "0mi",
"to" : "100mi",
"include_lower" : true,
"include_upper" : false,
"distance_type" : "plane"
}
},
"path" : "loc"
}
}, {
"nested" : {
"query" : {
"match" : {
"trees.specialty" : {
"query" : "specialty"
}
}
},
"path" : "trees"
}
} ]
}
}, {
"bool" : {
"must" : [ {
"nested" : {
"query" : {
"geo_distance_range" : {
"loc.address.lat_lon" : [ 67.0, -40.9 ],
"from" : "0mi",
"to" : "100mi",
"include_lower" : true,
"include_upper" : false,
"distance_type" : "plane"
}
},
"path" : "loc"
}
}, {
"nested" : {
"query" : {
"match" : {
"trees.group" : {
"query" : "group",
"type" : "boolean"
}
}
},
"path" : "trees"
}
} ]
}
}, {
"nested" : {
"query" : {
"bool" : {
"must" : [ {
"geo_distance_range" : {
"loc.address.lat_lon" : [ 67.0, -40.9 ],
"from" : "0mi",
"to" : "100mi",
"include_lower" : true,
"include_upper" : false,
"distance_type" : "plane"
}
}, {
"nested" : {
"query" : {
"match" : {
"loc.trees.specialty" : {
"query" : "special",
"type" : "boolean"
}
}
},
"path" : "loc.trees"
}
} ]
}
},
"path" : "loc"
}
}, {
"nested" : {
"query" : {
"bool" : {
"must" : [ {
"geo_distance_range" : {
"loc.address.lat_lon" : [ 67.0, -40.9 ],
"from" : "0mi",
"to" : "100mi",
"include_lower" : true,
"include_upper" : false,
"distance_type" : "plane"
}
}, {
"nested" : {
"query" : {
"match" : {
"loc.trees.group" : {
"query" : "groupA",
"type" : "boolean"
}
}
},
"path" : "loc.trees"
}
} ]
}
},
"path" : "loc"
}
}, {
"nested" : {
"query" : {
"bool" : {
"must" : [ {
"geo_distance_range" : {
"loc.address.lat_lon" : [ 67.0, -40.9 ],
"from" : "0mi",
"to" : "100mi",
"include_lower" : true,
"include_upper" : false,
"distance_type" : "plane"
}
}, {
"nested" : {
"query" : {
"match" : {
"loc.forest.trees.specialty" : {
"query" : "searchText",
"type" : "boolean"
}
}
},
"path" : "loc.forest.trees"
}
} ]
}
},
"path" : "loc"
}
}, {
"nested" : {
"query" : {
"bool" : {
"must" : [ {
"geo_distance_range" : {
"loc.address.lat_lon" : [ 67.0, -40.9 ],
"from" : "0mi",
"to" : "100mi",
"include_lower" : true,
"include_upper" : false,
"distance_type" : "plane"
}
}, {
"nested" : {
"query" : {
"match" : {
"loc.forest.trees.group" : {
"query" : "forge",
"type" : "boolean"
}
}
},
"path" : "loc.forest.trees"
}
} ]
}
},
"path" : "loc"
}
} ]
}
}
}
}