Elasticsearch Aggregation using Nested returns empty buckets - elasticsearch

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

Related

How to repair ElasticSearch broken query?

When I upgrade SSL my elasticsearch has stopped working and do not know why?
$params = [
'index' => 'vf_dev',
'size' => $filter["limit"],
'from' => $filter["skip"],
'body' => [
'query' => [
'bool' =>
$search
]
]
];
My query is like above. My error is lilke below. What am I missing?
{"type":"x_content_parse_exception","reason":"[1:93] [bool] failed to parse field [must]"}],"type":"x_content_parse_exception","reason":"[1:93] [bool] failed to parse field [must]","caused_by":{"type":"illegal_state_exception","reason":"Can't get text on a START_ARRAY at 1:93
Tldr
I believe, your pattern for a boolean query is wrong.
As per the documentation you should have either:
filter
must
should
must_not
I believe it is missing.

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.

Elasticsearch Nested search with match

I've got orders index, and It has nested structure like below;
{
'baskets' => [
{
'id' => 123,
'product' => {
'title': 'blabla',
'tags': [
'value': 'Tag1',
'value': 'Tag2'
]
}
},
{
'id' => 1234,
'product' => {
'title': 'blabla2',
'tags': [
'value': 'Tag3',
'value': 'Tag2'
]
}
}
]
}
I want to make "similarity search" inside that informations like;
"Match inside tags, most match is the most closest. (For example If I search with Tag1, Tag2 It must be matched better"
But how can I make matching inside arrays in nested searchs?
I need to search like: where baskets.product.tags.value in ['Tag1', 'Tag2', 'Tag4']
Better matching must be ordered first.
My search query is below. But not working well.
['bool' =>
[
'should' => [
'nested' => [
'path' => 'baskets.product.tags',
'query' => ['terms' => ['baskets.product.tags' => $concept_array]]
]
],
]
]
]
Arrays in ES are not exactly treated as arrays, more like multiple objects in the same field so you can't easily score on multiple matches. Maybe with a score script but it will be very brittle.
Instead you can add a new field to the product, like all_tags, type text (you might need to change the separator and tune analyzers if your tags can contain spaces) and put all your tags in there, it will be "Tag1 Tag2" and "Tag3 Tag2" in your example. Then a simple
"match": {"baskets.product.all_tags": "Tag3 Tag2"}
inside your nested query should do the trick, scoring higher docs with closely matching baskets.

How to mix $query->andFilterWhere and $query->query in Yii2 elasticsearch 6

I updated my yii2 system from yii2-elasticsearch 2.0 to 2.1 and elasticsearch package from 2.2.1 to 6.2.1. In the old system I could mix $query->andFilterWhere and $query->query as follows (the search method is in a class derived from yii\elasticsearch\ActiveRecord):
public function search($params)
{
$query = self::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
$query->andFilterWhere([
'languageCode' => \Yii::$app->locale->languageCode,
]);
$queryPart = [];
if (!empty($this->term)) {
$queryPart['filtered']['query']['multi_match'] = [
// ES6: $queryPart['bool']['must']['multi_match'] = [
'query' => $this->term,
'operator' => 'and',
'type' => $this->getQueryType($this->term),
'fields' => [
'name_*',
'meta_description_*'
]
];
}
if (!empty($queryPart)) {
$query->query($queryPart);
}
return $dataProvider;
}
It worked with ES 2.2.1 without any problem, but now the andFilterWhere overwrites $query->query independently from the sequence. If one of the two parts is removed the other filter works perfectly, only together not.
Any idea?
You must use bool query and put all part of your query in one "query" object...
Something like this:
query => [
bool => [
must => [
multi_match => [
'query' => $this->term,
'operator' => 'and',
'type' => $this->getQueryType($this->term),
'fields' => [
'name_*',
'meta_description_*'
]
]
]
filter => [
'languageCode' => \Yii::$app->locale->languageCode
]
]
]
This problem seems to be bug, as confirmed by other users on github.

How we can integrate Elastic search in yii2 project

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

Resources