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

188 Views Asked by At

Yii2 provides an elastic search extension and also elastic search ActiveRecord class.

This elastic search extension seams to read the data from elastic by querying the _source field of elastic search, which is not ideal.

If fields in elastic are set to "store" :true it would be the better way to read the data from the stored field directly, not by querying the _source field which results in an implicit parsing of that field by elastic.

Is there a way to configure or use Yii2 / elaticsearch - extension to get the data from the stored fields? I didn't found any option or something else here: https://www.yiiframework.com/extension/yiisoft/yii2-elasticsearch/doc/api/2.1/yii-elasticsearch-activerecord

1

There are 1 best solutions below

0
The Bndr On BEST ANSWER

To answer my own question in case someone else has the same problem:

If you like to run an query using active record in Yii2 on stored fields and if you like to get the data from the stored entry, not by parsing the _source filed, than you have th explicity specify the stored fields by using the ->storedFields() - function from active record class.

Using active record it looks like this:

/* 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(['*']);