Can't have a term and geo_distance_range in the same must filter - php

I have some data and I am trying to get all the results that have a certain month and are less than 1.6km from the target point. I am using the PHP client so my query looks like this.
$crimeSearch = [
'size' => 0,
'query' => [
'filtered' => [
'filter' => [
'bool' => [
'must' => [
'term' => [
'month' => $date,
],
'geo_distance_range' => [
'location' => [
'lat' => $lat,
'lon' => $lng,
],
'lt' => '1.6km',
],
],
],
],
],
],
'aggs' => [
'group_by_category' => [
'terms' => [
'field' => 'category',
],
],
],
];
I am currently seeing the following error:
query_parsing_exception: No query registered for [location]
My mapping looks like this:
"properties": {
"location": {
"type": "geo_point"
},
"category": {
"type": "string",
"index": "not_analyzed"
},
"month": {
"type": "string",
"index": "not_analyzed"
}
}
Now if I comment out either the term value or the geo_distance_range value from the must array then I get the correct results back. This error only occurs when they are both present.
Can anyone see what I wrong with my query?
I have tried moving the geo_distance_range into its own must block but this seems to bring back all results that match either of the the must filters and not them both.
If you need any more information please ask!
Thank you.

I do not know anything about PHP but If I try to convert equivalent ES json query then this might work. I guess you need to put every must clause in array like this
[
'size' => 0,
'query' => [
'filtered' => [
'filter' => [
'bool' => [
'must' => [
[
'term' => [
'month' => $date,
]
],
[
'geo_distance_range' => [
'location' => [
'lat' => $lat,
'lon' => $lng,
],
'lt' => '1.6km',
],
],
],
],
],
],
],
'aggs' => [
'group_by_category' => [
'terms' => [
'field' => 'category',
],
],
],
];
This is equivalent to
{
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"term": {
"month": "June"
}
},
{
"geo_distance_range": {
"lt": "1.6km",
"location": {
"lat": 37.9174,
"lon": -122.305
}
}
}
]
}
}
}
}
}
Does this work?

Related

Filter on multi match ES PHP

Starting to work on ES. I would like to be able to make a multi_match with a filter in PHP. I followed the official ES documentation but I don't understand my mistake.
Here is the code:
public function search_data_into_index($array)
{
$params = [
'index' => 'skills',
'type' => 'people',
'body' => [
'query' => [
'multi_match' => [
'query' => 'react',
'fields' => [$array[2]],
'fuzziness' => 'AUTO',
],
'filter' => [
'geo_distance' => [
'distance' => '300m',
'location' => '-25, -49'
]
]
]
]
];
$response = $this->client->search($params);
print_r($response);
}
Here my error :
{"error":{"root_cause":[{"type":"parsing_exception","reason":"[multi_match] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":94}],"type":"parsing_exception","reason":"[multi_match] malformed query, expected [END_OBJECT] but found [FIELD_NAME]
The multi_match query must be located inside bool/must:
public function search_data_into_index($array)
{
$params = [
'index' => 'skills',
'type' => 'people',
'body' => [
'query' => [
'bool' => [
'must' => [
'multi_match' => [
'query' => 'react',
'fields' => [$array[2]],
'fuzziness' => 'AUTO',
]
],
'filter' => [
'geo_distance' => [
'distance' => '300m',
'location' => '-25, -49'
]
]
]
]
]
];
$response = $this->client->search($params);
print_r($response);
}
You need to combine multiple queries using boolean query
In JSON format your query will look like
{
"query": {
"bool": {
"must": {
"multi_match": {
"query": "react",
"fields": [$array[2]]
}
},
"filter": {
"geo_distance": {
"distance": "300m",
"location": [
"-25, -49"
]
}
}
}
}
}

PHP ElasticSearch compound query with has_child

When I query Elasticsearch for products from a specific manufacturer, this works:
$params = ['index' => 'products',
'type' => 'product',
'body' => ['query' =>
['match' => ['manufacturers_id' => $query],],
],
];
But when I also want to add on a condition that the product comes in color Silver, which I have added as a child record to the product record, I get a syntax error:
$params = ['index' => 'products',
'type' => 'product',
'body' => ['query' =>
['match' => ['manufacturers_id' => $query],],
['query' =>
['has_child' =>
['type' => 'attributes',
['query' =>
['color' => 'Silver'],],
],
],
],
],
];
The error is
{
"error": {
"col": 49,
"line": 1,
"reason": "Unknown key for a START_OBJECT in [0].",
"root_cause": [
{
"col": 49,
"line": 1,
"reason": "Unknown key for a START_OBJECT in [0].",
"type": "parsing_exception"
}
],
"type": "parsing_exception"
},
"status": 400
}
Also tried
$params = ['index' => 'products',
'type' => 'product',
'body' => ["query"=> [
"match"=> [
"manufacturers_id"=> [11]
],
"has_child"=> [
"type"=> "attributes",
"query"=> [
"match"=> [
"color"=> "silver"
],
],
],
],
],
];
I get "Can't get text on a START_ARRAY at 1:39."
Try this:
"query"=> [
"match"=> [
"manufacturers_id"=> [1,2,3]
],
"has_child"=> [
"type"=> "attributes",
"query"=> [
"match"=> [
"color"=> "silver"
]
]
]
]
I also recommend Sense, it's a plugin for Chrome browser which helps writing ES queries.
See the screenshot
Finally got this to work. Big thanks to #pawle for his suggestion of Sense, which really helped.
$params = ['index' => 'products',
'type' => 'product',
'body' =>
[
"query" => [
"bool" => [
"must" => [[
"has_child" => [
"type" => "attributes",
"query" => [
"match" => [
"attributes_value" => "silver"
]
]
]
],
[
"match" => [
"manufacturers_id" => 4
]
]
]
]
]
],
];

ElasticSearch match query multiple terms PHP

I am trying to construct must query on multiple terms, the array looks like this:
$params = [
'body' => [
'query' => [
"bool" => [
"must" => [
"terms" => [
"categories" => [
"Seating",
],
],
"terms" => [
"attributes.Color" => [
"Black",
],
]
],
"filter" => [
"range" => [
"price" => [
"gte" => 39,
"lte" => 2999,
],
],
],
],
],
'from' => 0,
'size' => 3,
],
];
Which is represented in JSON like this:
{
"query": {
"bool": {
"must": {
"terms": {
"attributes.Color": ["Black"]
}
},
"filter": {
"range": {
"price": {
"gte": "39",
"lte": "2999"
}
}
}
}
},
"from": 0,
"size": 3
}
The problem is, JSON objects are represented as arrays in PHP so if I setup key for one array, it is rewritten. Do you have any idea on how to create multiple terms query in PHP?
Thanks in advance.
You need to add an additional array to enclose all your terms queries
$params = [
'body' => [
'query' => [
"bool" => [
"must" => [
[
"terms" => [
"categories" => [
"Seating",
],
]
],
[
"terms" => [
"attributes.Color" => [
"Black",
],
]
]
],
"filter" => [
"range" => [
"price" => [
"gte" => 39,
"lte" => 2999,
],
],
],
],
],
'from' => 0,
'size' => 3,
],
];

Whats wrong with the Elastic Search PHP search query?

I've been trying to get the following JSON working in PHP Arrays but I don't seem to get any hits.
The JSON is as follows:
{
"query": {
"filtered": {
"query": {
"query_string": {
"query": "search"
}
}
}
},
"fields": [
"body",
"title",
"postDate",
"user",
"name"
],
"from": 0,
"size": 50,
"sort": {
"_score": {
"order": "asc"
}
},
"explain": true
}
And the PHP I managed to create is like this:
$docs = $client->search([
'index' => 'blog',
'type' => 'posts',
'body' => [
'query' => [
'filtered' => [
'query' => [
'query_string' => [
'query' => $search_query
]
]
]
],
'fields' => [
'body',
'title',
'postDate',
'user',
'name'
],
'from' => 0,
'size' => 50,
'sort' => [
'_score' => [
'order' => 'asc'
]
]
]
]);
It returns an response but no hits, even though it should (and it does in case of the JSON request)
What is going on here?
The post type wasn't required at all... I somehow thought it was. I used a tool called ElasticHQ to generate the JSON and i didn't realize it wasnt using Posts as a type.
Changed it to
$docs = $client->search([
'index' => 'blog',
'body' => [
'query' => [
'filtered' => [
'query' => [
'query_string' => [
'query' => $search_query
]
]
]
],
'fields' => [
'body',
'title',
'postDate',
'user',
'name'
],
'from' => 0,
'size' => 50,
'sort' => [
'_score' => [
'order' => 'asc'
]
]
]
]);

Allowing case-insensitive search with snowball_analyzer in Elasticsearch

First of all I'm completly new to ES. I created ES search criteria below for searching items which works fine but what I now need is, I want to turn make field into case-insensitive so that the search result would be the same for hello, HeLlo, HELLO so on.
I've read post below couldn't quiet apply to my example below because of my very limited knowledge:
Case insensitivity does not work
Elasticsearch Map case insensitive to not_analyzed documents
Elasticsearch Snowball Analyzer wants exact word
Removing not_analyzed from make doesn't help.
'indexes' => [
'my_project' => [
'client' => 'default',
'index_name' => 'hello',
'settings' => [
'index' => [
'analysis' => [
'analyzer' => [
'snowball_analyzer' => [
'type' => 'snowball',
'language' => 'English',
],
],
],
],
],
'types' => [
'item' => [
'mappings' => [
'uuid' => ['type' => 'string', 'index' => 'not_analyzed'],
'name' => ['type' => 'string', 'boost' => 8, 'analyzer' => 'snowball_analyzer'],
'make' => ['type' => 'string', 'index' => 'not_analyzed'],
]
],
],
],
],
These is the query that I created:
1
{
"query": {
"filtered": {
"query": {
"bool": {
"must": [
{
"term": {
"make": "HeLlo"
}
}
]
}
}
}
}
}
You have to add the "lowercase" filter. Here is an extract for a similar configuration I use:
settings:
index:
analysis:
analyzer:
custom_search_analyzer:
type: custom
tokenizer: standard
filter: [stopwords, asciifolding ,lowercase, snowball, elision, worddelimiter]
In your case, I guess you should change like this:
...
'settings' => [
'index' => [
'analysis' => [
'analyzer' => [
'snowball_analyzer' => [
'type' => 'snowball',
'language' => 'English',
'filter' => [ 'lowercase' ]
],
],
],
],
],
...
I went thru the answer in the first link I posted with my eyes open this time and it solved my problem too so my case-insensitive working example is:
{
"query": {
"filtered": {
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "HeLlo*"
}
}
]
}
}
}
}
}

Categories