【问题标题】:Builder for ElasticSearch query DSL in Yii2 (or standalone)Yii2(或独立)中 ElasticSearch 查询 DSL 的构建器
【发布时间】:2015-08-06 20:17:47
【问题描述】:

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

查询量很大,很快就会变得难以管理。对于 SQL,Yii2 提供了一个强大的查询构建器类,它支持大量有用的方法,例如 andWhere()。对于 ElasticSearch 来说,一切都集中在一个巨大的查询表达式中,就像手动构建 SQL 表达式字符串一样。

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

【问题讨论】:

  • 我们也在找这个,找到解决方案有进展吗?
  • 我花了一些时间来适应 ES 范式。所有查询几乎都是嵌套的并且是自给自足的。所以我将查询的一部分构建为单独的查询,然后将它们组合在一起。这对我来说足够易于管理。
  • 是的,我们在这里有点类似的模式。谢谢
  • 还有elastica,不过没试过。
  • 我们最终编写了自己的类来生成我们通过 yii/elasticsearch/query 发送到 ES 的 json-query-object。不是超级动态,但它有效。

标签: php elasticsearch yii2 elasticsearch-dsl


【解决方案1】:

如果您打算为 1.6 版的 elastic 构建,我已经为我的公司创建了一个查询构建器并发布了 it here

您将把它用作一个独立的查询构建器,最后您需要获取最终的查询数组并将其传递给查询执行器。

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

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

//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 类,它可能对您有用。看看吧here

希望对你有帮助...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-17
    • 1970-01-01
    • 2022-12-15
    相关资源
    最近更新 更多