find and update a document in elasticsearch2 using php - php

I am trying to find a document using ElasticSearch and update a field in it. The code is:
require_once 'vendor/autoload.php';
$client = Elasticsearch\ClientBuilder::create()->build();
$params = [
'index' => 'myIndex',
'type' => 'myCollection',
'body' => [
'query'=> [
'match'=> [
'accessToken' => $accessToken
]
] ,
'script' => 'ctx._source.deviceId= deviceId',
'params' => [
'deviceId' => $deviceId
],
'upsert' => [
'counter' => 1
]
]
];
The code is giving 500 Internal Error. Any idea what am I missing?

If you want to increase the value of deviceid by 1 (mean if value of deviceid is 4 then u can increase it to 5 by adding 1 to it ),then follow below code
'script' => 'ctx._source.deviceId= deviceId',
replace with
'script' => 'ctx._source.counter += deviceId',
Example:
$params = [
'index' => 'my_index',
'type' => 'my_type',
'id' => 'my_id',
'body' => [
'script' => 'ctx._source.counter += count',
'params' => [
'count' => 4
],
'upsert' => [
'counter' => 1
]
]
];
$response = $client->update($params);
Check i Elasticsearch php

Related

Is it possible to use specific index in Mongodb using PHP?

Can somebody tell me please if is possible to force Mongodb to use specific index from PHP script? I have something like this:
$mongoPipeline = [
[
'$match' => [
'global_campaign_id' => ['$in' => $campaigns_ids]
]
],
[
'$group' => [
'_id' => [
'global_campaign_id' => '$global_campaign_id',
'device_id' => '$device_id',
'partner_id' => '$partner_id',
],
'date_last' => ['$max' => '$date_created'],
'partner_id' => ['$first' => '$server'],
'campaign_id' => ['$first' => '$global_campaign_id'],
'device_id' => ['$first' => '$device_id']
]
]
];
$options = [
'allowDiskUse' => true,
'maxTimeMS' => 1000 * 60 * 1,
'explain' => true,
];
$cursor = $this->mongoDb->{$collection}->aggregate($pipeline, $options);
Is it possible to add index to options object? If I use explain it seems it does not use an index prepared for this query. It is compoud index:
global_campaign_id: 1, device_id: 1, partner_id: 1, date_created: 1

elasticsearch php client search query returns all documents

I've been trying to search using 'match' and 'fuzzy', but I keep getting all of the documents, whether they match or not.
I've tried these 3 incarnations of my search:
1.
$params = [
'index' => 'my_engine',
'type' => 'my_type',
'body' => [
'query' => [
'fuzzy' => [
'_all' => $keyword
]
]
]
];
$params = [
'index' => 'my_engine',
'type' => 'my_type',
'body' => [
'query' => [
'match' => [
'title' => 'members'
]
]
]
];
$params['index'] = 'my_engine';
$params['type'] = 'my_type';
$params['body']['query']['match']['title'] = 'members';
Did I miss something during indexing?
I didn't provide any mapping.

Elasticsearch and PHP. Combine match and multi_match in one query

I have three fields - one of integer type (field1) and two of decimal type (field2, field3). I want to be able to query by all fields. These separate queries work nice in my situation:
$params = [
'index' => 'test_index',
'type' => 'text_index_type',
'body' => [
'query' => [
'match' => [
'field1' => '12'
]
]
]
];
and this query works well:
$params = [
'index' => 'test_index',
'type' => 'text_index_type',
'body' => [
'query' => [
'multi_match' => [
'query' => '345',
'fields' => ['field2', 'field3']
]
]
]
];
If, however, I combine them:
$params = [
'index' => 'test_index',
'type' => 'text_index_type',
'body' => [
'query' => [
'match' => [
'field1' => '12'
],
'multi_match' => [
'query' => '345',
'fields' => ['field2', 'field3']
]
]
]
];
I get an error:
Uncaught exception 'Elasticsearch\Common\Exceptions\BadRequest400Exception' ... [match] malformed query, unexpected [FIELD_NAME] found [multi_match]
So, what is wrong with that and how can I fix it?
PS. In terms of SQL, this is what I want to achive:
SELECT * FROM mytable where field1 = 12 or field2 = 345 or field3 = 345
You can combine them with bool queries
$params = [
'index' => 'test_index',
'type' => 'test_index_type',
'body' => [
'query' => [
'bool' => [
'should' => [
[ 'match' => [ 'field1' => '12' ] ],
[ 'multi_match' => [ 'query' => '345',
'fields' => ['field2', 'field3']] ],
]
]
]
]
];
should equates to "OR" while must equates to "AND"

Update by query (updateByQuery) Elasticsearch-PHP

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

ElasticSearch Index a Document fails on existing index

I am using ES php library. Here is what i have tried...
$params = [
'index' => 'tasks',
'body' => [
'settings' => [
'number_of_shards' => 3,
'number_of_replicas' => 2
],
'mappings' => [
'all' => [
'_source' => [
'enabled' => true
],
'properties' => [
'task' => [
'type' => 'string',
'analyzer' => 'standard'
],
'to' => [
'type' => 'string',
'analyzer' => 'standard'
],
'category' => [
'type' => 'integer',
'analyzer' => 'keyword'
]
]
]
]
]
];
// Create the index with mappings and settings now
$response = $client->indices()->create($params);
It returns success.
Now when i try to index a document...
$params = [
'index' => 'tasks',
'type' => 'all',
'id' => 'some_id',
'body' => [ 'task' => 'some test', 'to' => 'name', 'category' => 1]
];
$response = $client->index($params);
This throws error and does not work, however it works If i try this without creating index and mapping first.
Please suggest. Thanks
It's wrong to define analyzer in a field of type 'integer'.
Trying to create this mapping through Elasticsearch-PHP gives me a bad request:
... "reason":"Mapping definition for [category] has unsupported parameters: [analyzer : keyword]"}},"status":400}
Trying to create this mapping directly via PUT to ES gives me same error
I'm using ES version 2.2.0 and Elasticsearch-PHP 2.0

Categories