Aggregations are empty with keyword tokenizer - php

For an aggregation result i have a field (type_name2) in my mapping with a lowercased filter and a keyword tokenizer. But if I set this filter and tokenizer to the field my bucket is now empty. I have set this token and filter to ignoring spaces in words like: Fun X or Viva X.
Here my mapping (in PHP):
'cars' => array(
'properties' => array(
/**'type_name2' => array(
'type' => 'string',
'fields' => array(
'raw' => array(
'type' => 'string',
'analyzer' => 'keyword'
)
)
),**/
'type_name2' => array(
'type' => 'string',
'analyzer' => 'keyword_lower'
),
'type_kw' => array(
'type' => 'float'
),
'type_hp' => array(
'type' => 'float'
)
)
),
and the custom analyzer:
'analysis' => array(
'analyzer' => array(
'keyword_lower' => array(
'tokenizer' => 'keyword',
'filter' => 'lowercase'
),
'nGram_analyzer' => array(
'type' => 'custom',
'tokenizer' => 'whitespace',
'filter' => array(
'lowercase',
'asciifolding',
'nGram_filter'
)
),
'whitespace_analyzer' => array(
'type' => 'custom',
'tokenizer' => 'whitespace',
'filter' => array(
'lowercase',
'asciifolding'
)
)
),
'filter' => array(
'nGram_filter' => array(
'type' => 'ngram',
'min_gram' => 2,
'max_gram' => 20,
'token_chars' => array(
'letter',
'digit',
'punctation',
'symbol'
)
),
)
)
but the result of type_name2 is empty:
'type_name2' =>
array (size=1)
'buckets' =>
array (size=0)
empty
Any ideas, what is wrong?
Thx and regards
Stefan

Related

How to get value of element in the same array when initialize

I Think it's a strange question :
i dont need answer with loop or push to array i'm just wondering about the idea
I'm trying ti initialize an array ,, but i need to read the previous element like this,
return $recourd_list = array(
'clients'=> array(
'route' => 'clients.index',
'title' => 'fullname',
'list' => Clients::orderBy('id', 'desc')->take(5)->get(),
'count' => Clients::count(),
'class' => 'bgm-lightgreen',
'diff' => 5 - $recourd_list['clients']['count'],
),
'sliders'=> array(
'route' => 'sliders',
'title' => 'title',
'list' => Sliders::orderBy('id', 'desc')->take(5)->get(),
'count' => Sliders::count(),
'class' => 'bgm-rightBlue Oil-color',
'diff' => 5 - $recourd_list['sliders']['count'],
),
'sponsors'=> array(
'route' => 'sponsors.index',
'title' => 'title',
'list' => Sponsors::orderBy('id', 'desc')->take(5)->get(),
'count' => Sponsors::count(),
'class' => 'bgm-nave dark-green',
'diff' => 5 - $recourd_list['sponsors']['count'],
),
'packages'=> array(
'route' => 'packages',
'title' => 'title',
'list' => Packages::orderBy('id', 'desc')->take(5)->get(),
'count' => Packages::count(),
'class' => 'bgm-bluegray',
'diff' => 5 - $recourd_list['packages']['count'],
),
'event_schedule'=> array(
'route' => 'event_schedule.index',
'title' => 'title',
'list' => EventSchedule::orderBy('id', 'desc')->take(5)->get(),
'count' => EventSchedule::count(),
'class' => 'bgm-orange',
'diff' => 5 - $recourd_list['event_schedule']['count'],
),
);
I need to read the count element in diff element .. is that way to do that when initialize array
Thanks All
No, this isn't possible as the array technically won't be defined at that point.
One way to achieve this would be to use something like array_map.
http://php.net/manual/en/function.array-map.php
$recourd_list = array(
'clients'=> array(
'route' => 'clients.index',
'title' => 'fullname',
'list' => Clients::orderBy('id', 'desc')->take(5)->get(),
'count' => Clients::count(),
'class' => 'bgm-lightgreen',
),
'sliders'=> array(
'route' => 'sliders',
'title' => 'title',
'list' => Sliders::orderBy('id', 'desc')->take(5)->get(),
'count' => Sliders::count(),
'class' => 'bgm-rightBlue Oil-color',
),
'sponsors'=> array(
'route' => 'sponsors.index',
'title' => 'title',
'list' => Sponsors::orderBy('id', 'desc')->take(5)->get(),
'count' => Sponsors::count(),
'class' => 'bgm-nave dark-green',
),
'packages'=> array(
'route' => 'packages',
'title' => 'title',
'list' => Packages::orderBy('id', 'desc')->take(5)->get(),
'count' => Packages::count(),
'class' => 'bgm-bluegray',
),
'event_schedule'=> array(
'route' => 'event_schedule.index',
'title' => 'title',
'list' => EventSchedule::orderBy('id', 'desc')->take(5)->get(),
'count' => EventSchedule::count(),
'class' => 'bgm-orange',
),
);
then
return array_map(function ($item) {
$item['diff'] = $item['count'] - 5;
return $item;
}, $recourd_list);
Alternatively, you could use map with Collections
https://laravel.com/docs/5.3/collections#method-map
return collect($recourd_list)->map(function ($item) {
$item['diff'] = $item['count'] - 5;
return $item;
})->toArray();
Hope this helps!
short answer:
no.
But since you are, at init time, defining the Count, why not do this:
'diff' => 5 - Clients::count(),
so with the entire example:
return $recourd_list = array(
'clients' => array(
'route' => 'clients.index',
'title' => 'fullname',
'list' => Clients::orderBy('id', 'desc')->take(5)->get(),
'count' => Clients::count(),
'class' => 'bgm-lightgreen',
'diff' => 5 - Clients::count(),
),
'sliders' => array(
'route' => 'sliders',
'title' => 'title',
'list' => Sliders::orderBy('id', 'desc')->take(5)->get(),
'count' => Sliders::count(),
'class' => 'bgm-rightBlue Oil-color',
'diff' => 5 - Sliders::count(),
),
'sponsors' => array(
'route' => 'sponsors.index',
'title' => 'title',
'list' => Sponsors::orderBy('id', 'desc')->take(5)->get(),
'count' => Sponsors::count(),
'class' => 'bgm-nave dark-green',
'diff' => 5 - Sponsors::count(),
),
'packages' => array(
'route' => 'packages',
'title' => 'title',
'list' => Packages::orderBy('id', 'desc')->take(5)->get(),
'count' => Packages::count(),
'class' => 'bgm-bluegray',
'diff' => 5 - Packages::count(),
),
'event_schedule' => array(
'route' => 'event_schedule.index',
'title' => 'title',
'list' => EventSchedule::orderBy('id', 'desc')->take(5)->get(),
'count' => EventSchedule::count(),
'class' => 'bgm-orange',
'diff' => 5 - EventSchedule::count(),
),
);

Can’t filter search result

My Elasticsearch query is like that:
array(
'index' => 'myindex',
'type' => 'myindextype',
'from' => 0,
'size' => 500,
'body' => array(
'query' => array(
'filtered' => array(
'filter' => NULL,
'query' => array(
'query_string' => array(
'default_operator' => 'AND',
'query' => 'atm*',
'minimum_should_match' => '80%',
'default_field' => 'index'
)
)
)
)
)
);
Result is like that:
array(
'took' => 6,
'timed_out' => false,
'_shards' => array(
'total' => 5,
'successful' => 5,
'failed' => 0
),
'hits' => array(
'total' => 492,
'max_score' => 1,
'hits' => array(
0 => array(
'_index' => 'myindex',
'_type' => 'myindextype',
'_id' => 'Branch-571',
'_score' => 1,
'_source' => array(
'indexId' => 'Branch-571',
'objId' => '571',
'index' => 'atm Baki CNaxcivanski lisey Baki sheh Zig shossesi Admiral Naximov kuc 1 ',
'type' => 'Branch',
'title' => 'Bakı C.Naxçıvanski lisey'
)
),
.................................................................................................
196 => array(
'_index' => 'myindex',
'_type' => 'myindextype',
'_id' => 'Page-114',
'_score' => 1,
'_source' => array(
'indexId' => 'Page-114',
'objId' => 'Page-114',
'index' => 'atm Baki CNaxcivanski lisey Baki sheh Zig shossesi Admiral Naximov kuc 1 ',
'type' => 'Page',
'title' => 'Kreditlər'
)
)
.................................................................................................
)
)
);
I want to filter result and get results where 'type' => 'Branch' and I change query like that
array(
'index' => 'myindex',
'type' => 'myindextype',
'from' => 0,
'size' => 10,
'body' => array(
'query' => array(
'filtered' => array(
'filter' => array(
'bool' => array(
'must' => array(
'term' => array(
'type' => 'Branch'
)
)
)
),
'query' => array(
'query_string' => array(
'default_operator' => 'AND',
'query' => 'atm*',
'minimum_should_match' => '80%',
'default_field' => 'index'
)
)
)
)
)
);
But this search return nothing.
array(
'took' => 2,
'timed_out' => false,
'_shards' => array(
'total' => 5,
'successful' => 5,
'failed' => 0
),
'hits' => array(
'total' => 0,
'max_score' => NULL,
'hits' => array()
)
);
I think query is right but no idea why it didn't get any result.
Mapping is
array(
'myindex' => array(
'mappings' => array(
'myindextype' => array(
'properties' => array(
'index' => array(
'type' => 'string'
),
'indexId' => array(
'type' => 'string'
),
'objId' => array(
'type' => 'string'
),
'title' => array(
'type' => 'string'
),
'type' => array(
'type' => 'string'
)
)
)
)
)
)
I have find answer myself. The search query must be like that
POST _search
{
"query": {
"bool" : {
"must" : {
"query_string" :{
"query":"atm*",
"default_operator":"AND",
"minimum_should_match":"80%",
"default_field":"index"
}
},
"filter": {
"term": {
"type.keyword": "Branch"
}
}
}
}
}
All is working good now.

Cannot search string in elastic search after adding synonym

I have implemented synonym in elasticsearch but now i am facing problem. Whole string is splited on whitespace and i can not search with "see all result option".
Here is my settings.
$indexSettings['analysis']['analyzer'] = array(
'std' => array( // Will allow query 'shoes' to match better than 'shoe' which the stemmed version
'tokenizer' => 'standard',
'char_filter' => 'html_strip', // strip html tags
'filter' => array('standard', 'elision', 'asciifolding', 'lowercase', 'stop', 'length'),
),
'keyword' => array(
'tokenizer' => 'keyword',
'filter' => array('asciifolding', 'lowercase'),
),
'keyword_prefix' => array(
'tokenizer' => 'keyword',
'filter' => array('asciifolding', 'lowercase', 'edge_ngram_front'),
),
'text_prefix' => array(
'tokenizer' => 'standard',
'char_filter' => 'html_strip', // strip html tags
'filter' => array('standard', 'elision', 'asciifolding', 'lowercase', 'stop', 'edge_ngram_front'),
),
'text_suffix' => array(
'tokenizer' => 'standard',
'char_filter' => 'html_strip', // strip html tags
'filter' => array('standard', 'elision', 'asciifolding', 'lowercase', 'stop', 'edge_ngram_back'),
),
'synonym' => array(
'tokenizer' => 'standard',
'type' => 'custom',
'char_filter' => 'html_strip', // strip html tags
'filter' => array('standard','synonym', 'elision', 'asciifolding', 'kstem', 'length','snowball' ,'lowercase', 'stop',)
),
);
$indexSettings['analysis']['filter'] = array(
'edge_ngram_front' => array(
'type' => 'edgeNGram',
'min_gram' => 2,
'max_gram' => 10,
'side' => 'front',
),
'edge_ngram_back' => array(
'type' => 'edgeNGram',
'min_gram' => 2,
'max_gram' => 10,
'side' => 'back',
),
'stop' => array(
'type' => 'stop',
'stopwords' => '_none_',
),
'length' => array(
'type' => 'length',
'min' => 2,
),
'synonym' => array(
'type' => 'synonym',
'expand' => true,
'synonyms_path' => 'synonyms.txt',
),
);
$properties[$key]['analyzer'] = 'synonym';
In dropdown i give 5 result to show and one option see all result.
On searching of "Anti Ssl like protein" this word i got q=Anti ssl="" like="" protein="" in anchor tag of see all result.Cant get full word with see all result option.

Searching with synonym in elastic search

I am implementing search with synonym.
These are changes for searching with synonym.
This is my setting in index. I used bubble search extension for that and modified that code.
$indexSettings['analysis']['analyzer'] = array(
'std' => array(
'tokenizer' => 'standard',
'char_filter' => 'html_strip', // strip html tags
'filter' => array('standard', 'elision', 'asciifolding', 'lowercase', 'stop', 'length'),
),
'keyword' => array(
'tokenizer' => 'keyword',
'filter' => array('asciifolding', 'lowercase'),
),
'keyword_prefix' => array(
'tokenizer' => 'keyword',
'filter' => array('asciifolding', 'lowercase', 'edge_ngram_front'),
),
'text_prefix' => array(
'tokenizer' => 'standard',
'char_filter' => 'html_strip', // strip html tags
'filter' => array('standard', 'elision', 'asciifolding', 'lowercase', 'stop', 'edge_ngram_front'),
),
'text_suffix' => array(
'tokenizer' => 'standard',
'char_filter' => 'html_strip', // strip html tags
'filter' => array('standard', 'elision', 'asciifolding', 'lowercase', 'stop', 'edge_ngram_back'),
),
/*This is my analyzer for synonym*/
'synonym' => array(
'tokenizer' => 'standard',
'type' => 'custom',
// 'char_filter' => 'html_strip', // strip html tags
'filter' => array('synonym','standard', 'elision', 'asciifolding', 'lowercase', 'stop',"kstem", "length" )
),
);
$indexSettings['analysis']['filter'] = array(
'edge_ngram_front' => array(
'type' => 'edgeNGram',
'min_gram' => 2,
'max_gram' => 10,
'side' => 'front',
),
'edge_ngram_back' => array(
'type' => 'edgeNGram',
'min_gram' => 2,
'max_gram' => 10,
'side' => 'back',
),
'stop' => array(
'type' => 'stop',
'stopwords' => '_none_',
),
'length' => array(
'type' => 'length',
'min' => 2,
),
/*This is filter for synonym */
'synonym' => array(
'type' => 'synonym',
'expand' => true,
'synonyms_path' => 'synonyms.txt',
),
);
$properties[$key]['analyzer'] = 'synonym';
I am getting result but not up to mark.
eg lkjhgf => arylacetamide deacetylase like 3
If i search for lkjhgf then it will give result for
arylacetamide deacetylase like 1
arylacetamide deacetylase like 2
arylacetamide deacetylase like 3
arylacetamide deacetylase like 4
I want exact match and my fuzzy login is also disabled.

Elasticsearch range filter not working (No filter registered for)

I'm trying ta add a range filter to my search (only articles with workflow status <= 5 should appear) and getting errors
what's wrong with my range?
$searchParams = array(
'index' => $config->search->index,
'type' => 'xxxxxxxxxx',
'size' => 10,
'from' => ($page - 1) * 10,
'body' => array(
'query' => array(
'filtered' => array(
'query' => array(
'multi_match' => array(
'query' => $query,
'fields' => array('title^8', 'caption^5', 'teasertext^4', 'articletext^2') // ^x = Gewichtung des Felds
),
),
'filter' => array(
'bool' => array(
'must' => array(
'term' => array(
'deleted' => 0,
'visible' => 1
),
'range' => array(
'workflow_status' => array('lte' => '5')
)
)
)
)
)
)
),
'sort' => array('_score', '_id:desc')
here my mapping for status:
"workflow_status": {
"type": "integer",
"index": "not_analyzed"
}
What you have is not a valid Query DSL , must should take in an array.
The body should be something on these lines :
body =>
array(
'query' => array(
'filtered' => array(
'query' => array(
'multi_match' => array(
'query' => $query,
'fields' => array('title^8', 'caption^5', 'teasertext^4', 'articletext^2') // ^x = Gewichtung des Felds
),
),
'filter' => array(
'bool' => array(
'must' => array(
array(
'term' => array(
'deleted' => 0
)
),
array('term' => array(
'visible' => 1
)
),
array(
'range' => array(
'workflow_status' => array('lte' => '5')
)
)
)

Categories