ElasticSearch - Range query not working - php

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,
]
]);

Related

How to pass null filter in elastic search + PHP

I want to filter search result. I want to search that if 'manuf_id' is not specified than is should return all records. below is my search query in elastic.
$params = [
'index' => $this->client->getIndex(),
'type' => $this->client->getType(),
"from" => $from, "size" => $productPerPage,
'body' => [
"query" => [
"bool" => [
"must" => [
[
"multi_match" => [
"fields" => ["prod_name", "prod_seo_name"],
"type" => "phrase_prefix",
"query" => 'samsung'
]
],
//$conditionArr
[
"term"=> ["manuf_id"=>null]
]
]
]
]
],
];
Above query is not running. is there something I am missing? ANy help would be great.
You can pass this condition only in case when it necessary, like this:
$params = [
'index' => $this->client->getIndex(),
'type' => $this->client->getType(),
"from" => $from, "size" => $productPerPage,
'body' => [
"query" => [
"bool" => [
"must" => [
[
"multi_match" => [
"fields" => ["prod_name", "prod_seo_name"],
"type" => "phrase_prefix",
"query" => 'samsung'
]
],
]
]
]
],
];
if ($manufId > 0) {
$params['body']['query']['bool']['must'][] = [
'term' => ['manuf_id' => $manufId],
];
}

Aggregation by nested field after filter

I'm triying to get all the logs around a expecific geopoint, and group them by a subfield (context.id) but I'm having problems. I tried the nested aggregation, etc.. but I'm having no luck. I'm using the PHP library so I wrote the query as a php array. All is working until addign the aggregation query.
The exception throw is :
illegal_state_exception: Field data loading is forbidden on [context.id]
$params = [
'index' => 'logstash-*',
'type' => 'INFO',
'body' => [
'query' => [
'bool' => [
"must" => [
["term" => ["tags" => "producer"]],
["term" => ["tags" => "statistics"]],
["term" => ["message" => "view"]],
],
"filter" => [
"geo_distance" => [
"distance" => "10km",
"distance_type" => "plane",
"geoip.location" => [
"lat" => 40.4326058,
"lon" => -3.6996032
]
]
]
]
],
"aggs" => [
"context" => [
"nested" => [
"path" => "context"
],
"aggs" => [
"group_by_id" => [
"terms" => ["field" => "context.id"]
]
]
]
],
]
];
Can someone point me to the right query?
Finally, the problem here was the field. It was a indexed field I need to use the "context.id.raw" field instead.

How to write global query in elasticsearch?

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
]
],
....

Elastic search filter query with bool returns invalid results

I'm using Elastic search in Laravel.
So I have query that returns correct result:
$results = Es::search(array(
'index' => 'testindex',
'type' => $type,
'body' => [
'query' => [
'filtered' => [
'query' => [
'match' => [
'_all' => '2015-02'
]
],
'filter' => [
'bool' => [
'must' => [
'term' => [
'type' => 11
]
],
]
]
]
],
'size' => 5,
'from' => 0
]
));
What it does is:
searches for $query in all fields AND
field "type" must be 11.
Here is output: http://pastebin.com/icWniix4
Total 9 results which is correct.
But when I add another must term then it returns invalid results
$results = Es::search(array(
'index' => 'testindex',
'type' => $type,
'body' => [
'query' => [
'filtered' => [
'query' => [
'match' => [
'_all' => '2015-02'
]
],
'filter' => [
'bool' => [
'must' => [
'term' => [
'type' => 11
],
'term' => [
'public' => 1
]
],
]
]
]
],
'size' => 5,
'from' => 0
]
));
So this only add term for "public".
What it does is:
searches for $query in all fields AND
field "type" must be 11 AND
field "public" must be 1
So now results are in total 429. It ignores "type" term and returns everything with "public" = 1. But as per docs if I use MUST then it should match all of them. Search result http://pastebin.com/cVcatcyi
So how can I write query that I need? $query + type + public
Official doc's fail to answer my questions.
Any advice?
This is your trouble spot:
'must' => [
'term' => [
'type' => 11
],
'term' => [
'public' => 1
]
]
Here, you're assigning the value of must as an associative array that has only one distinct key - term actually gets assigned twice, so presumably only one of the assignments will "survive" (presumably public survives because it appears last in the definition). The end result is that must ends up pointing at an associative array with only one key-value pair.
What I suspect you have to do is this:
'must' => [
[
'term' => [
'type' => 11
]
],
[
'term' => [
'public' => 1
]
]
]
Now must is actually pointing at an array with two items.

Elasticsearch not sure if filter or query should be first

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.

Categories