在Yii2中为ElasticSearch查询DSL构建器(或独立)


Builder for ElasticSearch query DSL in Yii2 (or standalone)

据我所知,在Yii2中查询ElasticSearch的唯一方法是运行ElasticModel::find()->query($query),其中$query是一个复杂的数组,包含在ElasticSearch查询DSL中编写的实际查询。

查询是巨大的,很快变得难以管理。对于SQL, Yii2提供了一个强大的查询构建器类,它支持大量有用的方法,如andWhere()。对于ElasticSearch,所有内容都包含在一个巨大的查询表达式中,非常类似于手工构建SQL表达式字符串。

是否有任何高级包装的ElasticSearch查询DSL为Yii2?如果没有,是否有具有类似功能的独立库?

如果您打算构建1.6版本的elastic,我已经为我的公司创建了一个查询生成器,并在这里发布

您将使用它作为一个独立的查询生成器,最后您将需要获得最终的查询数组并将其传递给查询执行程序。

要安装它,您可以简单地使用composer composer require itvisionsy/php-es-orm或在这里下载压缩版本。

上面的链接包含了一些例子,这里是一个副本:

//build the query using different methods
$query = 'ItvisionSy'EsMapper'QueryBuilder::make()
            ->where('key1','some value') //term clause
            ->where('key2',$intValue,'>') //range clause
            ->where('key3','value','!=') //must_not term clause
            ->where('key4', ['value1','value2']) //terms clause
            ->where('email', '@hotmail.com', '*=') //wildcard search for all @hotmail.com emails
            ->sort('key1','asc') //first sort option
            ->sort('key2',['order'=>'asc','mode'=>'avg']) //second sort option
            ->from(20)->size(20) //results from 20 to 39
            ->toArray();
//modify the query as you need
$query['aggs']=['company'=>['terms'=>['field'=>'company']]];
//then execute it against a type query
$result = TypeQuery::query($query);
//i am not sure about Yii way to execute, according to the question, it should be:
$result = ElasticModel::find()->query($query); 

这个包还包括一个简单的ElasticSearch ORM类,它可能对你有用。请看这里