elasticsearch search string with slash - elasticsearch

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"
}
}
}

Related

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?

Elasticsearch should operator on multiple fields

Currently, I am trying to query the elasticsearch with should clause on multiple fields along with must clause in one field.
with SQL I would write this query:
SELECT * FROM test where ( titleName='Business' OR titleName='Bear') AND (status='Draft' OR status='Void') AND creator='bob'
I tried this:
$params = [
'index' => myindex,
'type' => mytype,
'body' => [
"from" => 0,
"size" => 1000,
'query' => [
'bool' => [
'must' => [
'bool' => [
'should' => [
['match' => ['titleName' => 'Business']],
['match' => ['titleName' => 'Bear']]
]
],
'should' => [
['match' => ['status' => 'Draft']],
['match' => ['status' => 'Void']]
]
'must' => [
['match'] => ['creator' => 'bob']
]
]
]
]
]
];
The above query string working with single status field or single title field. But it's not working with both the fields.
Does anyone have a solution?
You need to AND both of your bool/should pairs. This should work:
$params = [
'index' => myindex,
'type' => mytype,
'body' => [
"from" => 0,
"size" => 1000,
'query' => [
'bool' => [
'must' => [
[
'bool' => [
'should' => [
['match' => ['titleName' => 'Business']],
['match' => ['titleName' => 'Bear']]
]
]
],
[
'bool' => [
'should' => [
['match' => ['status' => 'Draft']],
['match' => ['status' => 'Draft']]
]
]
],
[
'match' => ['creator' => 'bob']
]
]
]
]
]
];
You can write your query something like this. Add a Must inside that you add Should
{
"query": {
"filter": {
"bool": {
"must": [{
"bool": {
"should": [{
"term": {
"titleName": "business"
}
},
{
"term": {
"titleName": "bear"
}
}
]
}
},
{
"bool": {
"should": [{
"term": {
"status": "draft"
}
},
{
"term": {
"status": "void"
}
}
]
}
},
{
"bool": {
"must": [{
"term": {
"creator": "bob"
}
}]
}
}
]
}
}
},
"from": 0,
"size": 25
}

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