yii2.0 elasticsearch模糊查詢


 最近使用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查詢,后者沒用過,用過再補上
 
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM