在使用 yii2 的弹性搜索中使用存储字段而不是 _source

Use stored fields instead of `_source` in elastic search using yii2

Yii2 提供了弹性搜索扩展和弹性搜索 ActiveRecord class.

这个 elastic search 扩展通过查询 elastic search 的 _source 字段接缝从 elastic 中读取数据,这是不理想的。

如果 elastic 中的字段设置为 "store" :true,那么直接从存储字段中读取数据将是更好的方法,而不是通过查询 _source 字段,这会导致隐式解析该字段由 elastic.

有没有办法配置或使用 Yii2 / elaticsearch - 扩展从存储字段中获取数据?我在这里没有找到任何选项或其他选项:https://www.yiiframework.com/extension/yiisoft/yii2-elasticsearch/doc/api/2.1/yii-elasticsearch-activerecord

回答我自己的问题以防其他人遇到同样的问题:

如果您想 运行 在 Yii2 中使用活动记录对存储字段进行查询,并且如果您想从存储的条目中获取数据,而不是通过解析 _source 字段,那么您使用 ->storedFields() - 来自活动记录的函数 class.

明确指定存储的字段

使用活动记录看起来像这样:

/* new instance from User model,
   which is based on `\yii\elasticsearch\ActiveRecord`*/
$user = new User();


/* '*' for all fileds instead of an array of fieldnames */
$query = $user -> find()
                    -> query('<your query object')
                    -> storedFields(['*']);