Aggregation by nested field after filter - php

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.

Related

Defining search rules in elasticsearch

How can I combine these two queries so I can search by name, publisher with one type of rules and ean by different set of rules, all in one query?
'must'=> [
[
"multi_match" => [
"query" => $this->builder->query,
"fields" => ['name', 'publisher', 'platform'],
"fuzziness" => 'auto',
"prefix_length" => 1,
],
"match" => [
"query" => $this->builder->query,
"fields" => ['ean'],
"prefix_length" => 1
],
]
]
I used this code to solve my problem
'should'=> [
[
"multi_match" => [
"query" => $this->builder->query,
"fields" => ['name', 'publisher', 'platform'],
"fuzziness" => 'auto',
"prefix_length" => 1,
'operator' => 'or'
],
],
[
"match" => [
"ean" => $this->builder->query,
],
]
]

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

ElasticSearch Delete by query not working in PHP

I am using Elastic search 5.x and the following code is working fine:
curl -XPOST "http://localhost:9200/test_index/test_info/_delete_by_query" -d'
{
"query": {
"match": {
"category_id": "21"
}
}
}'
But when I am trying the same in my php code, its not working:
$client->deleteByQuery([
'index' => 'test_index',
'type' => 'test_info',
'query' => [
'match' => [
['category_id' => 21]
]
]
]);
You need to provide your query array inside body array of your parameters:
$client->deleteByQuery([
'index' => 'test_index',
'type' => 'test_info',
'body' => [
'query' => [
'match' => [
['category_id' => 21]
]
]
]
]);
this an old question, previous comments don't work anymore in 2020 :
$client->deleteByQuery([
'index' => 'test_index',
(there were a type here) 'type' => 'test_info',
'body' => [
'query' => [
'match' => [
(there were an array here) ['category_id' => 21]
]
]
]
]);
So the final code is :
$client->deleteByQuery([
'index' => 'test_index',
'body' => [
'query' => [
'match' => [
'category_id' => 21
]
]
]

ElasticSearch - Range query not working

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

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