我需要使用 prefix
过滤器,但允许多个不同的前缀,即
{"prefix": {"myColumn": ["This", "orThis", "orEvenThis"]}}
这是行不通的。如果我将每个添加为单独的 prefix
显然也行不通。
感谢您的帮助。
更新
我尝试了 should
但没有任何运气:
$this->dsl['body']['query']['bool']['should'] = [
["prefix" => ["myColumn" => "This"]],
["prefix" => ["myColumn" => "orThis"]]
];
当我添加这两个约束时,我得到了所有响应(好像过滤器不起作用)。但是,如果我将 must
与这些子句中的任何一个一起使用,那么我确实会收到带有正确前缀的响应。
最佳答案
根据您的评论,这听起来可能只是语法问题。对于所有 ES 查询(就像 SQL 查询一样),我建议从简单开始并将它们作为代码之外的原始 DSL 提交给 ES(尽管在您的情况下这并不容易做到)。对于请求,这是一个非常简单的请求:
{
"query" : {
"bool" : {
"must" : [ ... ],
"filter" : [
{
"bool" : {
"should" : [
{
"prefix" : {
"myColumn" : "This"
}
},
{
"prefix" : {
"myColumn" : "orThis"
}
},
{
"prefix" : {
"myColumn" : "orEvenThis"
}
}
]
}
}
]
}
}
}
我将其添加为 filter
因为您的前缀的可选性质并没有提高相关性:它实际上是在要求其中一个必须匹配。在这种情况下,如果问题是“这匹配吗?是/否”,那么您应该使用过滤器(还有可缓存的额外好处!)。如果你问“这个匹配吗,哪个匹配更好?”那么您需要一个查询(因为那是相关性/评分)。
注意:最初的问题似乎是 bool
/must
没有被提及,建议只使用 bool
/应该
。
{
"bool" : {
"should" : [
{
"prefix" : {
"myColumn" : "This"
}
},
{
"prefix" : {
"myColumn" : "orThis"
}
},
{
"prefix" : {
"myColumn" : "orEvenThis"
}
}
]
}
}
行为不同于
{
"bool" : {
"must" : [ ... ],
"should" : [
{
"prefix" : {
"myColumn" : "This"
}
},
{
"prefix" : {
"myColumn" : "orThis"
}
},
{
"prefix" : {
"myColumn" : "orEvenThis"
}
}
]
}
}
因为 must
影响了 should
的必要性质。 没有 must
,should
的行为类似于 bool 值OR
。但是,对于 must
,它作为一个完全可选函数来提高相关性(分数)。要使其返回到 bool OR
行为 with must
,您必须将 minimum_should_match
添加到 bool
复合查询。
{
"bool" : {
"must" : [ ... ],
"should" : [
{
"prefix" : {
"myColumn" : "This"
}
},
{
"prefix" : {
"myColumn" : "orThis"
}
},
{
"prefix" : {
"myColumn" : "orEvenThis"
}
}
],
"minimum_should_match" : 1
}
}
请注意,它是 bool
查询的组成部分,而不是 should
或 must
的组成部分!
关于Elasticsearch 多前缀关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38211104/