Yii2 Elastic Search

Hi,

I want to use the yii2-elasticsearch ActiveRecord, but I keep getting errors and I am not quite sure what to do:




$item = new ElasticSearchItem();

$item->primaryKey = 1;

$item->Name = 'Test';

$item->save();



This works, and initializes a record.


$record = ElasticSearchItem::get('1');

Works as expected.


$records = ElasticSearchItem::find()->all();

Also works as expected.

But this does not work and returns nothing:


$record = ElasticSearchItem::find()->where(['Name' => 'Test']->all();

…and this does not work either:


$record = ElasticSearchItem::find()->query(['field' => ['Name' => 'Test']])->all();

The last query even crashes with the following error log:




 [responseBody] => Array

        (

            [error] => SearchPhaseExecutionException[

               Failed to execute phase [query], all shards failed; shardFailures {[xdOvowGGS0eZ9-WXisJfiQ][elastic-search-items][4]: SearchParseException[

               [elastic-search-items][4]: from[-1],size[10]: Parse Failure [Failed to parse source [{"size":10,"query":{"field":{"Name":"Test"}}}]]]; nested: QueryParsingException[

               [elastic-search-items] No query registered for [field]]; }{[xdOvowGGS0eZ9-WXisJfiQ][elastic-search-items][3]: SearchParseException[

               [elastic-search-items][3]: from[-1],size[10]: Parse Failure [Failed to parse source [{"size":10,"query":{"field":{"Name":"Test"}}}]]]; nested: QueryParsingException[

               [elastic-search-items] No query registered for [field]]; }{[xdOvowGGS0eZ9-WXisJfiQ][elastic-search-items][0]: SearchParseException[

               [elastic-search-items][0]: from[-1],size[10]: Parse Failure [Failed to parse source [{"size":10,"query":{"field":{"Name":"Test"}}}]]]; nested: QueryParsingException[

               [elastic-search-items] No query registered for [field]]; }{[xdOvowGGS0eZ9-WXisJfiQ][elastic-search-items][1]: SearchParseException[

               [elastic-search-items][1]: from[-1],size[10]: Parse Failure [Failed to parse source [{"size":10,"query":{"field":{"Name":"Test"}}}]]]; nested: QueryParsingException[

               [elastic-search-items] No query registered for [field]]; }{[xdOvowGGS0eZ9-WXisJfiQ][elastic-search-items][2]: SearchParseException[

               [elastic-search-items][2]: from[-1],size[10]: Parse Failure [Failed to parse source [{"size":10,"query":{"field":{"Name":"Test"}}}]]]; nested: QueryParsingException[

               [elastic-search-items] No query registered for [field]]; }]

            [status] => 400

        )



Am I missing something? This is more or less exactly the same as the how-to from github.com/yiisoft/yii2-elasticsearch.

Can anybody tell me, what I am doing wrong?

Thank you very much!

You should become familiar with elasticsearch itself before using the AR layer. If you have not defined a mapping for your index and types the where query can not give good results.

Will check what’s wrong with the field query tomorrow.

You are right, I am new to ElasticSearch, but I think my AR maps correctly the index and types:




$item = new ElasticSearchItem();

$item->primaryKey = 1;

$item->Name = 'Test';

$item->save();


//prints 'Test', 

echo ElasticSearchItem::get('1')->Name;



Else this would not work correctly, right?

Thanks for your help!

Your index and type are correct but you have to configure how fields are indexed for text fields to ensure you can find them by certain conditions. If a field is analyzed you can not do simple string comparison with WHERE anymore.

I also adjusted the README. The example query was based on outdated information and is not supported with this syntax anymore.