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
]
]
]
Related
I try to sort my documents per name using elastic search & the official php client , how i can proceed ?
$params = [
'index' => $this->index ,
'type' => 'videos',
'from' => $this->uri->segment(2),
'size' => 12,
'body' => [
'query' => [
'filtered' => [
'filter' => [
'term' => [ 'name' => $query ] ,
'term' => [ 'tags' => $query ]
]
]
]
]
];
$data['results'] = $this->client->search($params);
I know this question is over a year old, but the answer is not easy to find on the internet, so I'll answer it anyway.
To specify the field to sort on and the order to sort in, use the following syntax:
$params['sort'] = array('updated_at:desc');
To sort on multiple fields:
$params['sort'] = array('updated_at:desc', 'user_id:asc', ...);
I just saw this post while searching for an answer to the same question, in my case the solution was much simpler and different to the ones I saw here.
$params['body']['sort'] = [ 'id' => 'desc']
this worked fine for me using "elasticsearch/elasticsearch": "^6.1"
Try this
$params = [
'index' => $this->index ,
'type' => 'videos',
'from' => $this->uri->segment(2),
'size' => 12,
'body' => [
'query' => [
'filtered' => [
'filter' => [
'term' => [ 'name' => $query ] ,
'term' => [ 'tags' => $query ]
]
]
],
'sort' => [
'name' => [
'order' => 'asc'
]
]
]
];
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html
If you have unanalyzed keyword you must use it without enabling fielddata:
$params = [
'index' => $this->index ,
'type' => 'videos',
'from' => $this->uri->segment(2),
'size' => 12,
'body' => [
'query' => [
'filtered' => [
'filter' => [
'term' => [ 'name' => $query ] ,
'term' => [ 'tags' => $query ]
]
]
],
'sort' => [
'name' => [
'order.keyword' => 'asc'
]
]
]
];
But if you haven't the unanalyzed keyword you should reset your index mapping to enable fielddata to name field:
curl -X PUT "localhost:9200/my_index/_mapping" -H 'Content-Type: application/json' -d'
{
"properties": {
"name": {
"type": "text",
"fielddata": true
}
}
}
'
see : https://www.elastic.co/guide/en/elasticsearch/reference/current/fielddata.html
I am Using Elasticsearch 2.3 along with the official php driver. The updateByQuery is giving me troubles to use in php. A little help on how to use it will be appreciated.
$client = \Elasticsearch\ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
# Request
$updateRequest = [
'index' => 'gorocket',
'type' => 'logs',
'body' => [
'query' => [
'filtered' => [
'filter' => [
'bool' => [
'must' =>
[
[
'match' => [ 'enabled' => 1 ],
],
]
]
]
]
]
]
]
];
# Update
$results = $client->updateByQuery($updateRequest);
Basically i want to update a couple of document fields(name,price) that matches a certain query
Thank you.
So, with the help of how the CURL api works i managed to come up with a way.
First you need to edit your elasticsearch.yml to allow Scripting. Append the following lines at the end.
script.engine.groovy.inline.search: on
script.engine.groovy.inline.aggs: on
script.engine.groovy.inline.update: on
There after you can start doing batch updates using queries as the example bellow.
$client = \Elasticsearch\ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
# Request
$updateRequest = [
'index' => 'testindex',
'type' => 'logs',
'conflicts' => 'proceed',
'body' => [
'query' => [
'filtered' => [
'filter' => [
'bool' => [
'must' =>
[
[
'match' => [ 'enabled' => 1 ],
],
]
]
]
]
],
'script' => [
'inline' => 'ctx._source.enabled = value',
'params' => [
'value' => 0
]
]
]
]
];
# Update
$results = $client->updateByQuery($updateRequest);
That's it. It is not easily and well documented as of now so.
I want to add a small addition to the previous answer
You may not add the following params in elasticsearch.yml
script.engine.groovy.inline.search: on
script.engine.groovy.inline.aggs: on
script.engine.groovy.inline.update: on
And your query will be:
$client = \Elasticsearch\ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
# Request
$updateRequest = [
'index' => 'testindex',
'type' => 'logs',
'conflicts' => 'proceed',
'body' => [
'query' => [
'filtered' => [
'filter' => [
'bool' => [
'must' => [
[
'match' => [ 'enabled' => 1 ],
],
]
]
]
]
],
'script' => [
'lang' => 'painless',
'source' => 'ctx._source.enabled = params.value',
'params' => [
'value' => 0
]
]
]
];
# Update
$results = $client->updateByQuery($updateRequest);
I'm having a little trouble translating some of the queries I use for elasticsearch into PHP readable queries.
For example this simple query works:
$query = $elastic->search([
body' => [
'query' => [
'match' => [
'myfield' => 'mymatchingresult'
]
]
]
]);
But what I'm trying to get to work follow below. There isn't an error, it just doesn't run. I must not be understanding the structure. The same query if placed in something like google extension sense seems to work. (With the php '=>' converted to ':' etc.)
$query = $elastic->search([
'body' => [
'query' => [
'filtered' => [
'query' => [
'query_string' => [
'query' => '*',
'analyze_wildcard' => 'true'
]
],
'filter' => [
'bool' => [
'must' => [
'query' => [
'query_string' => [
'analyze_wildcard' => 'true',
'query' => 'cn:name'
]
],
'range' => [
'#timestamp' => [
'from' => '2012-05-01',
'to' => '2016-05-01'
]
]
]
]
]
]
]
]
]);
Thank you for the help!
-John
As far as I can tell, the constraints in your bool/must filter must be enclosed in square brackets, i.e. bool/must should be a pure array, not an associative array.
Like this:
$query = $elastic->search([
'body' => [
'query' => [
'filtered' => [
'query' => [
'query_string' => [
'query' => '*',
'analyze_wildcard' => 'true'
]
],
'filter' => [
'bool' => [
'must' => [
[
'query' => [
'query_string' => [
'analyze_wildcard' => 'true',
'query' => 'cn:name'
]
]
],
[
'range' => [
'#timestamp' => [
'from' => '2012-05-01',
'to' => '2016-05-01'
]
]
]
]
]
]
]
]
]
]);
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.
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,
]
]);