I want to create top search query using elasticsearch.
I want to match category_name, brand_name and title from elasticsearch table. The match should be phrase and with or condition.
My query:
$query = [
"bool" => [
"must" => [
["match" => ["is_published" => 1]],
[
"multi_match" => [
"query" => $searchKey,
"fields" => ["category_name", "brand_name", "title"]
]
],
[
'nested' => [
'path' => 'category_with_in_title.parent_cat',
'query' => [
'bool' => [
'must' => [
['match' => [
'category_with_in_title.parent_cat.status' => 1,
]],
],
],
],
],
],
[
'nested' => [
'path' => 'brand',
'query' => [
'bool' => [
'must' => [
'match' => [
'brand.approved_status' => 1,
],
],
],
],
],
],
[
'nested' => [
'path' => 'default_product_low_price_with_seller.seller_detail',
'query' => [
'bool' => [
'must' => [
'match' => [
'default_product_low_price_with_seller.seller_detail.approve_status' => 1,
],
],
],
],
],
],
[
'nested' => [
'path' => 'default_product_low_price_with_seller',
'query' => [
'bool' => [
'must' => [
'match' => [
'default_product_low_price_with_seller.status' => 1,
],
],
],
],
],
],
],
],
];
I use multi_match for that but how to use pharse in this query? I have to write whole word to search.
For example :
I want to search the record whose category_name = Tops
I want result if i write "tops" or "top" or "to". But right now I have to write "Tops" the exact word.
Thanks in advance...
You could use match_phrase_prefix, which is the same as match_phrase, except that it allows for prefix matches on the last term in the text.
All that you need to do, is add "type": "phrase_prefix" to your multi_match query, like this:
"multi_match" => [
"query" => $searchKey,
"type": "phrase_prefix",
"fields" => ["category_name", "brand_name", "title"]
]
Let me know if this is what you're looking for.
Ideally you should use multi_match with cross_fields type, which will treat all fields as ONE big field and run your query on it. However, due to fuzziness being disabled on these, only the exact match will show.
See Revelant Github Issue https://github.com/elastic/elasticsearch/issues/6866
Hence, my recommendation is to replace multi_match with fuzzy_like_this (elasticsearch version 1.x) or more_like_this (elasticsearch version 2.x).
$query = [
"bool" => [
"must" => [
["match" => ["is_published" => 1]],
[
"fuzzy_like_this" => [
"fields" => ["category_name", "brand_name", "title"],
"like_text" => $searchKey
]
],
....
Related
I cannot figure out the following:
A database with address fields (country, county, city) and an "operate" field, options: locally, nationally internationally
I want to find matches based on location and the "operate" status. So I am basically searching for how to use MySQL OR in ElasticSearch. To my understanding this is "should".
So, any entry that operates internationally should be in the results, any entry that operates nationally in the same country and any entry that operates locally in the same county regardless which city is selected
Just as a test case, I tried several things among which the following:
$arrayinternationally[] = array('match' => array('operate' => 'internationally'));
$arrayNationally[] = array('match' => array('operate' => 'nationally'));
$arrayNationally[] = array('match' => array('country' => '60'));
$arrayLocally[] = array('match' => array('operate' => 'locally'));
$arrayLocally[] = array('match' => array('country' => 'UK'));
$arrayLocally[] = array('match' => array('county' => '60'));
$params = [
'index' => 'bzlistings',
'body' => [
'from' => 0,
'size' => 80,
'query' => [
'bool' => [
'should' => [
'bool' => [
'should' => $arrayinternationally
]
],
'should' => [
'bool' => [
'should' => $arrayNationally
]
],
'should' => [
'bool' => [
'should' => $arrayLocally
]
],
],
],
],
];
Those entries in another country with "operate" set to "internationally", are not included, which is wrong.
How can this be done in ElasticSearch?
Thanks,
Peter
I think there is something wrong with the bool queries. Could you test with following query :
$params = [
'index' => 'bzlistings',
'body' => [
'from' => 0,
'size' => 80,
'query' => [
'bool' => [
'should' => [
[
'bool' => [
'must' => $arrayinternationally
]
],
[
'bool' => [
'must' => $arrayNationally
]
],
[
'bool' => [
'must' => $arrayLocally
]
],
],
],
],
],
];
But I think, in your case, inner bools need to be must instead of should. Also, I recommend that if you are looking for exact matching, you can use term query instead of match.
Typically, the bool query structure is something like the below :
GET sample-index/_search
{
"query": {
"bool": {
"should": [
{},
{}
]
}
}
}
I have been reading through elastic search docs over the last few months and have continued to optimize my query, but I can't seem to get a search query below 500-600ms. Locally with less data I can get responses in ~80-200ms.
To outline what I am trying to accomplish:
I have 12 different models in Laravel that are searchable from a single search bar. As someone types it is searched and returned in a list of results.
Currently, I have this for my search query. Are there any references for how I can improve this? I looked into multi_match, but I was having issues with partial matches and specifying all fields.
$results = $this->elastic->search([
'index' => config('scout.elasticsearch.index'),
'type' => $type ?? implode(',', array_keys($this->permissions, true, true)),
'body' => [
'query' => [
'bool' => [
'must' => [
[
'query_string' => [
'query' => "$searchQuery*",
],
],
],
'filter' => [
[
'term' => [
'account_id' => $accountId,
],
],
],
'should' => [
[
'term' => [
'_type' => [
'value' => 'customers',
'boost' => 1.3,
],
],
],
[
'term' => [
'_type' => [
'value' => 'contacts',
'boost' => 1.3,
],
],
],
[
'term' => [
'_type' => [
'value' => 'users',
'boost' => 1.3,
],
],
],
[
'term' => [
'_type' => [
'value' => 'chart_accounts',
'boost' => 1.2,
],
],
],
],
],
],
'from' => $from,
'size' => $size,
],
]);
Trying to query in Elasticsearch w/ the PHP client and give priority to partial words matches but still include fuzzy matches. If I remove the address.company match block, the query works as expected, but is broken with it present no matter how I seem to frame it. I am lost on the formatting to also include the fuzzy searches with a lower priority?
$search_data = [
"from" => (int) $start, "size" => (int) $count,
'query' => [
'bool' => [
'filter' => [
['term' => ['active' => 1]],
['term' => ['type' => 2]],
],
'must' => [
'wildcard' => [
'address.company' => '*' . $search_query . '*'
],
'match' => [
'address.company' => [
'query' => $search_query,
'operator' => 'and',
'fuzziness' => 'AUTO',
],
],
],
],
],
];
While I am still new to ES as likely is apparent, this solution seems to get the data I'm after. I only mention that because there may be a more ideal way if someone views this in the future. Switching from must to should and wrapping the arrays a bit differently did the trick.
$search_data = [
"from" => (int) $start, "size" => (int) $count,
'query' => [
'bool' => [
'filter' => [
['term' => ['active' => 1]],
['term' => ['type' => 2]],
],
'should' => [
[
'match' => ['address.company' => ['query'=>$search_query,'boost'=>10]],
],
[
'match' =>
[
'address.company' =>
[
'query' => $search_query,
'fuzziness' => 'AUTO',
],
],
],
],
'minimum_should_match'=>1,
],
],
];
I'm new to ElasticSearch so sorry if it's noob question. I'm trying to get users who has salary less than some value but I'm getting this error:
query_parsing_exception: No query registered for [salary]
My other queries works fine, only range query is failing, this is my code:
$items = $this->client->search([
'index' => 'offerprofiles',
'type' => 'profile',
'body' => [
'query' => [
'bool' => [
"must" => [
"match" => [
"jobcategories.name" => [
"query" => $query['category']
]
],
"range" => [
"salary" => [
"lt" => 20
]
]
],
"should" => [
"match" => [
"skills.name" => [
"query" => $query['skills']
]
]
],
"minimum_should_match" => 1
]
],
'size' => 50,
]
]);
If I remove range query then everything works fine, also I checked indexed values and salary is there (integer).
Thanks
The query is not a valid DSL. In the particular you are missing a bunch of brackets in the must clause. The must in the bool query should be an array of clauses instead in the above it is an object with key match and range.
Example :
$items = $this->client->search([
'index' => 'offerprofiles',
'type' => 'profile',
'body' => [
'query' => [
'bool' => [
"must" => [
[
"match" => [
"jobcategories.name" => [
"query" => $query['category']
]
]
],
[
"range" => [
"salary" => [
"lt" => 20
]
]
]
],
"should" => [
"match" => [
"skills.name" => [
"query" => $query['skills']
]
]
],
"minimum_should_match" => 1
]
],
'size' => 50,
]
]);
I am using ES for my Laravel app.
What I want to do is a search filtering.
I do a fulltext search on the title field and then check that the price is between 0 - 9999 and that active is set to 1.
But both these queries seems to work fine on my test data. But what is the difference between them? Does the order "query" comes in play any diffrence?
Ignore the syntax, just take a look at the query structure.
First query
'filtered' => [
'query' => [
'match' => ['title' => Input::get('query')]
],
'filter'=> [
'bool' => [
'must' => [
['term' => [ 'active' => 1] ],
[ 'range' => [
'price' => [
'gte' => 1,
'lte' => 99999,
]
]
]
]
]
],
],
Second query
'filtered' => [
'filter' => [
'bool' => [
'must' => [
['term' => [ 'status' => 1] ],
[
'range' => [
'price' => [
'gte' => 1,
'lte' => 99999,
]
]
]
]
]
],
'query' => [
'match' => [
'title' => Input::get('query', '')
]
]
]
Thanks in advance.
It makes no difference at all in which order filter and query are mentioned in a filtered query. What dictates if query or filter is executed first for a document depends on an expert-level optional field called strategy of filtered query. For more information, read this.