最近使用yii2.0查詢es數據,一般查找語句用的yii2.0的query類,遇到模糊查詢使用like的時候竟然報
like conditions are not supported by elasticsearch.
在QueryBuilder.php中查找到這個函數
private function buildLikeCondition($operator, $operands)
{
throw new NotSupportedException('like conditions are not supported by elasticsearch.');
}
修改此函數為:
private function buildLikeCondition($operator, $operands)
{
if (!isset($operands[0], $operands[1])) {
throw new InvalidParamException("Operator '$operator' requires two operands.");
}
if($operator=="like"){
return [
'regexp' => [
$operands[0]=>".*".$operands[1].".*",
],
];
}else{
throw new NotSupportedException('like conditions are not supported by elasticsearch.');
}
}
解決了like模糊查詢,用到了正則匹配語句。暫時解決了項目模糊查詢的需要。用正則”regexp“應該還可以用wildcards查詢,后者沒用過,用過再補上
