Elasticsearch多个前缀关键字

Kou*_*sha 4 elasticsearch

我需要使用prefix过滤器,但允许使用多个不同的前缀,即

{"prefix": {"myColumn": ["This", "orThis", "orEvenThis"]}}
Run Code Online (Sandbox Code Playgroud)

这是行不通的。而且,如果我将每个单独添加,prefix显然也行不通。

感谢帮助。

更新资料

我尝试过should但没有运气:

$this->dsl['body']['query']['bool']['should'] = [
    ["prefix" => ["myColumn" =>  "This"]],
    ["prefix" => ["myColumn" =>  "orThis"]]
];
Run Code Online (Sandbox Code Playgroud)

当我添加这两个约束时,我得到所有响应(就像过滤器不起作用一样)。但是,如果我使用must这些子句中的任何一个,那么我的确会收到带有正确前缀的响应。

pic*_*ypg 5

根据您的评论,听起来可能只是语法问题。对于所有ES查询(就像SQL查询一样),我建议从简单开始,然后将它们作为原始DSL提交给ES,而不是代码之外的原始DSL(尽管这在您看来并不容易做到)。对于请求,这是一个非常简单的请求:

{
  "query" : {
    "bool" : {
      "must" : [ ... ],
      "filter" : [
        {
          "bool" : {
            "should" : [
              {
                "prefix" : {
                  "myColumn" : "This"
                }
              },
              {
                "prefix" : {
                  "myColumn" : "orThis"
                }
              },
              {
                "prefix" : {
                  "myColumn" : "orEvenThis"
                }
              }
            ]
          }
        }
      ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

filter之所以添加它,是因为前缀的可选性质并不能改善相关性:这实际上是在要求它们之一必须匹配。在这种情况下,如果问题是“是否符合条件?是/否”,那么您应该使用过滤器(附加的额外奖励是可缓存的!)。如果您要问“这匹配吗,哪个匹配更好?” 那么您需要查询(因为这是相关性 /得分)。

注意:最初的问题似乎是bool/ must未被提及,建议使用bool/ should

{
  "bool" : {
    "should" : [
      {
        "prefix" : {
          "myColumn" : "This"
        }
      },
      {
        "prefix" : {
          "myColumn" : "orThis"
        }
      },
      {
        "prefix" : {
          "myColumn" : "orEvenThis"
        }
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

表现与

{
  "bool" : {
    "must" : [ ... ],
    "should" : [
      {
        "prefix" : {
          "myColumn" : "This"
        }
      },
      {
        "prefix" : {
          "myColumn" : "orThis"
        }
      },
      {
        "prefix" : {
          "myColumn" : "orEvenThis"
        }
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

因为must影响要求的性质should如果没有 must,则should表现为布尔值OR。但是,使用时must,它充当完全可选的功能以提高相关性(得分)。为了让它回到布尔OR行为 must,您必须添加minimum_should_matchbool复合查询。

{
  "bool" : {
    "must" : [ ... ],
    "should" : [
      {
        "prefix" : {
          "myColumn" : "This"
        }
      },
      {
        "prefix" : {
          "myColumn" : "orThis"
        }
      },
      {
        "prefix" : {
          "myColumn" : "orEvenThis"
        }
      }
    ],
    "minimum_should_match" : 1
  }
}
Run Code Online (Sandbox Code Playgroud)

请注意,它是bool查询的组成部分,而不是shouldmust!的组成部分。