Elasticsearch AND condition with term query - elasticsearch

This is my search filter query ,
I want to give "and" condition for terms.
Now I have given must inside the filter condition
'query' => [
"filtered" => [
"query" => [
"match_all" => []
],
"filter" => [
"bool" => [
"must" => [
"terms" => [
"num" => [
1,2,3
]
],
"terms" => [
"sample" => [
"a", "b"
],
]
]
]
],
'query' => [
'multi_match' => [
'query' => "Hello",
'fields' => ['text'],
'operator' => 'and'
],
]
]
],
but its not working. Any other solution for this?

try this out, when you want to add 'and' condtion, just add that part in the bool,must.
{"query":{"bool":{"must":[{"terms":{"num":[1,2,3]}},{"terms":{"sample":["a","b"]}},{"match":{"text":"hello"}}]}},"from":0,"size":10}

You need to enclose your terms filters in one more array, otherwise your bool/must becomes an associative array and that's not what you want (i.e. one of the terms filter gets discarded).
'query' => [
"filtered" => [
"query" => [
"match_all" => []
],
"filter" => [
"bool" => [
"must" => [
[ <---
"terms" => [
"num" => [
1,2,3
]
]
], <---
[ <---
"terms" => [
"sample" => [
"a", "b"
],
]
] <---
]
]
],
'query' => [
'multi_match' => [
'query' => "Hello",
'fields' => ['text'],
'operator' => 'and'
],
]
]
],

Related

elasticsearch search string with slash

I try to search for a string that contains a slash (in field firstname) but elasticsearch shows me an error "Failed to parse query ...".
Here is my mapping
[
'index' => 'indexA',
'body' => [
'settings' => [
'number_of_shards' => 1,
'number_of_replicas' => 1,
"analysis" => [
"analyzer" => [
"analyzer_asciifolding" => [
"tokenizer" => "standard",
"filter" => [ "lowercase", "my_ascii_folding" ]
]
],
"filter" => [
"my_ascii_folding" => [
"type" => "asciifolding",
"preserve_original" => true
]
]
]
],
'mappings' => [
'dynamic' => false,
'properties' => [
...,
'persons' => [
'type' => 'nested',
'properties' => [
...,
'firstname' => [
'type' => 'text',
'analyzer' => 'analyzer_asciifolding'
]
]
]
]
]
]
];
Can you help me please?
UPDATED 2022-04-01 15:10
here is the php code which query elasticsearch
$response = $this->elasticsearchService->search([
'index' => 'indexA',
'body' => [
'query' => [
'bool' => [
'filter' => [
[
'query_string' => [
'query' => $attributes['person_firstname'],
'fields' => ['persons.firstname']
]
]
],
]
],
'from' => $from,
'size' => $size
]
]);
You should escape them with a leading backslash.
The reserved characters are: + - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \ /
{
"query": {
"query_string": {
"fields": ["persons.firstname"],
"query": "use\\/share"
}
}
}

elasticsearch bool query add logic

$params = [
"size" => 5000,
'index' => 'my_post',
'type' => 'my_post',
'body' => [
'query' => [
'bool' => [
'should' => [
[
'match' => ['poststatus_id' => 'public']
],
[
"bool" => [
"must" => [
"match" => [
'poststatus_id' => 'only_me',
],
"match" => [
'creator.user_id' => $user
]
]
]
],
],
]
]
]
];
its my elastic search query i Need another or condition that is
poststatus_id = "followers" and creator.user_id in (1,2,3,4)
anyone tell me how can i write this query
Try this..
$params = [
"size" => 5000,
'index' => 'my_post',
'type' => 'my_post',
'body' => [
'query' => [
'bool' => [
'should' => [
[
'match' => ['poststatus_id' => 'public']
],
[
"bool" => [
"must" => [
[
"match" => [
'poststatus_id' => 'only_me',
]
],
[
"match" => [
'creator.user_id' => $user
]
]
]
]
],
[
"bool" => [
"must" => [
[
"match" => [
"poststatus_id" => "followers"
]
],
[
"terms" => [
"poststatus_id" => [1,2,3,4]
]
]
]
]
]
]
]
]
]
];
$params = [
"size" => 5000,
'index' => 'my_post',
'type' => 'my_post',
'body' => [
'query' => [
'bool' => [
'should' => [
[
'match' => ['poststatus_id' => 'public']
],
[
"bool" => [
"must" => [
"match" => [
'poststatus_id' => 'only_me',
],
"match" => [
'creator.user_id' => $user
]
]
]
],
[
"bool" => [
"must" => [
[
"match" => [
"poststatus_id" => "followers"
]
],
[
"terms" => [
"creator.user_id" => [1,2,3,4]
]
]
]
]
]
],
]
],
]
];

ElasticSearch 7 - combine filters

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?

Elastic search to match top level field, and a nested field

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' => []
]
]
]
]
]
];

Elasticsearch query for simple category search, sorting by price and getting records by range

I want to write query with following condition :
search by category (like category = 'cat1')
and with price range (and price between 100 to 500)
and sort by price (low to high)
I tried:
$params = [
'index' => 'my_index',
'type' => 'product',
'body' => [
//"from" => 0, "size" => 2,
"sort" => [
["default_product_low_price.sale_price" => ["order" => "asc"]]
],
'query'=> $query,
"aggs" => [
"default_product_low_price" => [
"nested" => [
"path" => "default_product_low_price"
],
"aggs" => [
"range" => ["default_product_low_price.sale_price" => [ "gte" => '790',
"lte" => '1000' ]],
//"max_price" => ["max" => [ "field" => "default_product_low_price.sale_price" ]]
],
]
]
]
];
But I am getting an error
Bad Request 400 Exception in GuzzleConnection.php line 277: error.
Please guide me where I am wrong? What is the right query?
I think this should be your query :
$params = [
'index' => 'my_index',
'type' => 'product',
'body' => [
"sort" =>[
["default_product_low_price.sale_price" => ["order" => "asc"]]
],
"query"=> [
"filtered"=> [
"query"=> [
"match_all"=> []
],
"query"=>[
"term"=> [
"category.name"=> "jeans"
]
],
"filter"=> [
"nested"=> [
"path"=> "default_product_low_price",
"filter"=> [
"bool"=> [
"must"=> [
[
"range"=> [
"default_product_low_price.sale_price"=> [
"gte"=> 100,
"lte"=> 200,
]
]
]
]
]
]
]
]
]
]
]
];

Resources