I watched Elasticsearch Do's, Don'ts and Pro-Tips by Itamar Syn Hershko
https://www.youtube.com/watch?v=c9O5_a50aOQ
I see multiple conditions in several fields in the following image:
https://imgur.com/a/17zAZ4w
I tried to make it in
my Laravel 5.7 app (with the elasticsearch/elasticsearch plugin) as seen in the following code:
$elasticQuery = [
"bool" => [
'must' => [
'multi_match' => [
'query' => $text,
'fields' => ['name^4', 'description']
],
],
"should" => [
'term' => [
"category_id" => 1,
]
]
]
];
but I got the error :
{"error":{"root_cause":[{"type":"parsing_exception","reason":"[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":130}],"type":"parsing_exception","reason":"[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":130},"status":400}
But when I use a simple condition:
$elasticQuery = [
'multi_match' => [
'query' => $text,
'fields' => ['name^4', 'description'],
],
];
I got a valid result:
[hits] => Array
(
[total] => 1
[max_score] => 7.4126062
[hits] => Array
(
[0] => Array
(
[_index] => select_vote
[_type] => vote
[_id] => 16
[_score] => 7.4126062
[_source] => Array
(
[id] => 16
[slug] => in-the-film-babe-what-type-of-animal-was-babe
[name] => In the film Babe, what type of animal was Babe?
[description] => Babe is a 1995 A...
[created_at] => 2018-11-10 09:14:15
[category_id] => 2
[category] => Array
(
[name] => Movie&Cartoons
[slug] => movie-cartoons
[created_at] => 2018-11-10 09:14:12
)
)
)
)
)
is this a valid format for multi-request?
MODIFIED BLOCK # 2:
Making some search I found work:
$elasticQuery = [
"bool" => [
'should' => [
[
"multi_match" => [
"query" => $text,
"type" => "cross_fields",
"fields" => [
"name^4",
"description"
]
]
],
[
'match' => [
'category_id' => [
'query' => 1,
]
]
],
[
'match' => [
'category_id' => [
'query' => 3,
]
]
],
]
]
];
when I need to make a search by text fields and the array of category (1 and 3) in the example above it works but looks like it works like an "OR" condition, but I need to make a restriction like "AND" using SQL terminology...
Which way is correct in order to make a restriction as like "AND"?
Thanks!
If you just change should to must it is not going to work because category_id cannot have two values at the same time (unless it's an array, but it's not).
You need to use the following query instead:
$elasticQuery = [
"bool" => [
'must' => [
[
"multi_match" => [
"query" => $text,
"type" => "cross_fields",
"fields" => [
"name^4",
"description"
]
]
],
],
'filter' => [
[
'terms' => [
'category_id' => [ 1, 3 ]
]
]
]
]
];
Related
I'm trying to boost a search by the "created" field (an integer / timestamp) but always run into
"{"error":{"root_cause":[{"type":"parsing_exception","reason":"Unknown key for a START_OBJECT in [script].","line":1,"col":181}],"type":"parsing_exception","reason":"Unknown key for a START_OBJECT in [script].","line":1,"col":181},"status":400}"
Without the 'script' the query works fine. But I'm running out of ideas how to write this script correctly. Any ideas?
return [
'index' => 'articles_' . $this->system,
'body' => [
'size' => $this->size,
'from' => $this->start,
'sort' => [
$this->order => 'desc',
],
'query' => [
'query_string' => [
'query' => $this->term,
'fields' => ['title^5', 'caption^3', 'teaser^2', 'content'],
'analyze_wildcard' => true,
],
'script' => [
'script' => [
'lang' => 'painless',
'source' => "doc['#created'].value / 100000",
],
],
],
],
];
EDIT: Updated query, but still running into "{"error":{"root_cause":[{"type":"parsing_exception","reason":"[query_string] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":171}],"type":"parsing_exception","reason":"[query_string] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":171},"status":400}"
Script is not a standalone attribute. It should be part of bool. When you have multiple filters these should be in must/should/filter under bool
'body' => [
'size' => $this->size,
'from' => $this->start,
'sort' => [
$this->order => 'desc'
],
'query' => [
'bool' => [
'must' =>[
'query_string' => [
'query' => $this->term,
'fields' => ['title^5', 'caption^3', 'teaser^2', 'content'],
'analyze_wildcard' => true
],
'script' => [
'script' => [
'lang' => 'painless',
'source' => "doc['#created'].value / 100000"
]
]
]
]
]
]
Above can have syntax issue of brackets(I couldn't test it) , query structure is correct
...
'query' => [
'function_score' => [
'query' => [
'query_string' => [
'query' => $this->term,
'fields' => ['title^10', 'caption^8', 'teaser^5', 'content'],
'analyze_wildcard' => true,
],
],
'script_score' => [
'script' => [
'lang' => 'expression',
'source' => "_score + (doc['created'] / 10000000000000)",
],
],
],
],
Was my solution at the end. Sadly found at the documentation of elasticsearch later. But you really have to divide the timestamp strongly that it doesn't totally overpower the best matches.
I use ES 7 and Laravel implementation, I want to combine a range and a term match, according to documentation, I did this :
$items = $client->search([
'index' => $instance->getSearchIndex(),
'type' => $instance->getSearchType(),
'body' => [
'size' => 50,
'query' => [
'bool' => [
'must' => [
'multi_match' => [
'fields' => config('elasticsearch.fields'),
'query' => $query,
],
],
'filter' => [
'bool' => [
'must' => [
'range' => [
'note' => [
'gte' => config('elasticsearch.note_minimum')
]
],
'term' => [
'type_video_id' => 5
],
],
],
],
]
],
],
]);
And got this error :
"parsing_exception","reason":"[range] malformed query, expected
[END_OBJECT] but found [FIELD_NAME]
I only found documentation and examples for ES 2 about combining queries, did something change ?
I want my query to match the fields, and be filtered according to filter.
Here is the right way to do this:
$items = $client->search([
'index' => $instance->getSearchIndex(),
'type' => $instance->getSearchType(),
'body' => [
'size' => 50,
'query' => [
'bool' => [
'must' => [
'multi_match' => [
'fields' => config('elasticsearch.fields'),
'query' => $query,
]
],
'filter' => [
[
'range' => [
'note' => [
'gte' => config('elasticsearch.note_minimum')
]
]
],
[
'term' => [
'type_video_id' => 5
]
]
]
]
]
]
]);
I don't have a way to test this, but I'm seeing a couple brackets where you need curlies. Also, don't you need you need $ before those "config"?
{
"query" => {
"bool" => {
"must" => [
{
"multi_match" => {
"query" => $query,
"fields" => $config('elasticsearch.fields')
}
}
],
"filter" => {
{
"range" => {
"note" => {
"gte" => $config('elasticsearch.note_minimum')
}
}
},
{
"term" => {
"type_video_id" => {
"value" => "5"
}
}
}
}
}
}
}
If this doesn't work, can you paste what the string looks like after your variables get rendered?
I am trying to calculate average of the result set that is returning me locations from Elastic Search. Here is what i am trying.
'aggs' => [
"avg_location" => [
'avg' => [
'field' => 'location'
]
]
]
This returns error as location itself is an object/array that returns me [lat,long] of the point. I need to calculate average of lats and longs of all the points returned.
How can i do that? I tried quite a few things but none of them worked.
Here is the whole code.
$json = [
'query' => [
'bool' => [
'must_not' => [
'terms' => ['rarity' => []],
],
'must' => [
'range' => [
'disappearTime' => [
'gte' => 'now',
'lte' => 'now+1d'
]
]
],
'filter' => [
[
'geo_bounding_box' => [
'location' => [
'top_left' => [
'lat' => $northWestLat,
'lon' => $northWestLng
],
'bottom_right' => [
'lat' => $southEastLat,
'lon' => $southEastLng
]
]
]
]
]
]
],
'aggs' => [
"avg_location" => [
'avg' => [
'field' => 'location'
]
]
]
];
Thanks
I can't seem to get this query to work
I am trying to match all tags who's name matches $channel_name,
but also, which has a video, with the name of $search.
I think the nested part is messed up. How can I fix this query?
$body = [
'query' =
[
"bool" => [
"must" => [
'match_phrase' => ['name' => $channel_name]
],
'nested' => [
'path' => 'videos',
'query' => [
'bool' => [
[
'must' => [
'match' => [
'videos.name' => $search
]
]
]
]
]
]
],
],
'inner_hits' => [
'videos' => [
'path' => [
'videos' => [
'size' => $this->pageSize,
'sort' => $sortOrder,
'from' => ($pageNumber - 1) * $this->pageSize,
'query' => [
'match_all' => []
]
]
]
]
]
];
I have this aggregators to deduplicate documents :
$searchParams['body'] = [
'aggs' => [
'dedup' => [
'terms' => [
'field' => 'source',
'size' => 50,
'order' => [
'max_hits' => "desc"
]
],
'aggs' => [
'dedup_hits' => [
'top_hits' => [
'size' => 1
]
],
'max_hits' => [
'max' => [
"script" => "doc.score"
]
]
]
]
]
];
This query order by document score. However, i want to order by _timestamp field. It's possible ? I have test with Date Histogram aggregator. But without success.