How we can integrate Elastic search in yii2 project - elasticsearch

How do we implement elastic search in yii2. Done with installation, How do I search by database using elastic search?
This is my config file:
'components' => [
'elasticsearch' => [
'class' => 'yii\elasticsearch\Connection',
'nodes' => [
['http_address' => '127.0.0.1:80'],
],
],

Hope the below link give u some idea about it
http://voormedia.com/blog/2014/06/four-ways-to-index-relational-data-in-elasticsearch

Related

Laravel elastic search implement soundex

I'm facing an issue on the elastic search that it's not able to search if someone types wrong spelling. I have done some R & D about Soundex. Now I'm facing an issue to implement Soundex on elastic search. Please help me to do that, I've already installed Phonetic Anaalysis plugin on elastic search but how to configure the plugin with elastic search that will work with the search results.
'title' => [
'type' => 'text',
'analyzer' => $language . '_analyzer',
'index' => true,
'norms' => false,
'term_vector' => 'with_positions_offsets',
'fields' => [
'raw' => [
'type' => 'keyword',
'normalizer' => 'lowercase_normalizer',
'index' => true,
'norms' => false,
],
],
],
You need to create a custom analyzer using phonetic token filter and the apply this custom analyzer to your text field.
Alternatively, if you want to search with mistypes you can use fuzzy matches.

How to start using Slug URLs without breaking old in Yii2

i want to rewrite my URLs from
/view?id=100
to
/view/100-article-title
But the site already has several thousand search pages. As far as I know, such a change can be bad for SEO. Is there a way to keep the old URLs working when switching to the new routing.
*I am creating links as follows:
Url::to(['view', 'id' => $item->id])
Is it possible to create links further through ID?
you can create getLink() function on your model and use it on. Then, when function runs you can check id if id <= 100 then return Url::to(['view', 'id' => $item->id]) else return Url::to(['view', 'id' => $item->id, 'slug' => $this->slug])
And add route with slug on your main.php
Look at my example.
First config urlManager in config.php in 'app/config' folder. In my case look like:
'components' => [
...
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
''=>'home/index',
'<slugm>-<id:\d+>-' => 'home/view',
],
],
.
...
],
and create link as fallow:
Url::to(['/home/view', 'id' => $model->home_id, 'slugm' =>$model->title_sr_latn])
and finaly url look like:
https://primer.izrada-sajta.rs/usluge-izrade-sajtova-1-

Elasticsearch Aggregation using Nested returns empty buckets

Elasticsearch: v7.2
Application: Laravel v.5.7
Hello and good day! I'm using Elasticsearch to make a report of my documents, I stumbled upon the need of presenting data fields that are nested in nature.
I have the following mappings, I have my index web with a field called ent:
Now I have the following query, using the aggs, my goal is to present the entities that have the MOST counts that can be found in my documents:
'aggs' => [
'ENT' => [
'nested' => [
'path' => 'ent'
],
'aggs' => [
'TOP_ENTITIES' => [
'terms' => [
'field' => 'ent.ent_count'
]
]
]
]
]
What I'm finding weird about this, is that when I'm targeting the ent.ent_count field, the buckets works perfectly fine, finding the distinct ent_count together with its respective doc_counts which portrays the total number of occurence of that ent_count:
BUT when I'm targeting the ent.ent_name field, it returns empty:
'aggs' => [
'ENT' => [
'nested' => [
'path' => 'ent'
],
'aggs' => [
'TOP_ENTITIES' => [
'terms' => [
'field' => 'ent.ent_name.keyword'
]
]
]
]
]
RESULTS TO
With non-nested fields, this works perfectly fine, am I doing something wrong with my query? because even the examples from the documentation shows the same scripts
There's no other way to solve this problem unless you change the mappings
So instead of letting the ent.ent_name nested field to be of text field, we have found out that short words in nested fields should be of KEYWORD type:
After changing the _mappings to keyword, everything worked perfectly fine

yii2 frontend and backend uses different sessions

Despite I did not separate sessions of frontend and backend apps on config files of those apps using session => ... option, my apps uses different sessions and when I login one of them then the other one is logouts. I could not find the source of problem. I want them using same session. What can be the problem?
Try this one
Add session in frontend->config->main.php
'components' => [
'session' => [
'name' => 'PHPFRONTSESSID',
'savePath' => sys_get_temp_dir(),
],
]
same in backend->config->main.php
'components' => [
'session' => [
'name' => 'PHPBACKSESSID',
'savePath' => sys_get_temp_dir(),
],
]

How to use DbMessageSource in yii2

i am using yii2 advanced template.and i dont understand how to use DbMessageSource.i read the guide and i created two tables source_message and message and i wrote in my common/config/main.php file this code
'*'=> [
'class' => 'yii\i18n\DbMessageSource',
'sourceMessageTable'=>'{{%source_message}}',
'messageTable'=>'{{%message}}',
'enableCaching' => true,
'cachingDuration' => 3600
],
and what i have to write in brackets when i using <?= Yii::t()?>
P.S. i am also changed language in my config.
P.P.S. i generated models and cruds for this tables
try this:
'i18n' => [
'translations' => [
'*' => [
'class' => 'yii\i18n\DbMessageSource',
'forceTranslation'=>true,
]
],
],
set parameter forceTranslation as true. This trick helps me.

Resources