Querying Elasticsearch with PHP - php

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

Related

How to create an elasticsearch bool query with php client?

The query returns this error.
Elasticsearch\Common\Exceptions\BadRequest400Exception: {"error":{"root_cause":[{"type":"parsing_exception","reason":"unknown query [query]","line":1,"col":62}],"type":"x_content_parse_exception","reason":"[1:62] [bool] failed to parse field [should]","caused_by":{"type":"parsing_exception","reason":"unknown query [query]","line":1,"col":62,"caused_by":{"type":"named_object_not_found_exception","reason":"[1:62] unknown field [query]"}}},"status":400} in
How can i write this query ? . In array first one is (manufacturer_code,ean) parent, other objects are nested.
$query = [
'bool' => [
'should' => [
['query' => [ //parent root
'query_string' => [
'fields' => ['manufacturer_code', 'ean'],
'query' => $search,
],
],
],
['nested' => [ //nested parent.lang
'path' => 'lang',
'query' => [
'query_string' => [
'fields' => 'lang.name',
'query' => $search ,
],
],
]],
['nested' => [ //nested parent.product_base.lang
'path' => 'product_base.lang',
'query' => [
'query_string' => [
'fields' => 'product_base.lang.meta_keywords',
'query' => $search ,
],
],
]],
],
'minimum_should_match' => 1,
],
];
You're almost there, in the first should clause you need to remove the first query, i.e. query_string should be at top level
$query = [
'bool' => [
'should' => [
['query_string' => [ //parent root <-- modify this clause
'fields' => ['manufacturer_code', 'ean'],
'query' => $search,
],
],
['nested' => [ //nested parent.lang
'path' => 'lang',
'query' => [
'query_string' => [
'fields' => 'lang.name',
'query' => $search ,
],
],
]],
['nested' => [ //nested parent.product_base.lang
'path' => 'product_base.lang',
'query' => [
'query_string' => [
'fields' => 'product_base.lang.meta_keywords',
'query' => $search ,
],
],
]],
],
'minimum_should_match' => 1,
],
];

If else statement in elasticsearch query construction

I'm using elasticsearch php and trying to optimize my query contraction in one place. Typical Elasticsearch query like:
$params = [
'index' => 'my_index',
'type' => 'my_type',
'body' => [
'query' => [
'bool' => [
'must' => [
[ 'match' => [ 'testField' => 'abc' ] ],
[ 'match' => [ 'testField2' => 'xyz' ] ],
]
]
]
]
];
So the question is, does it possible to put conditional query in $params before 'match' string maybe like:
<?php if (isset($_GET['query'])) [ 'match' => [ 'testField' => 'abc' ] ]; ?>
Thank you in any advice
You can use this:
<?php
$must = [[ 'match' => [ 'testField2' => 'xyz' ] ] ];
if (isset($_GET['query']))
$must[] = [ 'match' => [ 'testField' => 'abc' ] ];
$params = [
'index' => 'my_index',
'type' => 'my_type',
'body' => [
'query' => [
'bool' => [
'must' => $must
]
]
]
];
or this;
<?php
$params = [
'index' => 'my_index',
'type' => 'my_type',
'body' => [
'query' => [
'bool' => [
'must' => [
[ 'match' => [ 'testField2' => 'xyz' ] ],
],
]
]
]
];
if (isset($_GET['query']))
$params['body']['query']['bool']['must'][] = [ 'match' => [ 'testField' => 'abc' ] ];

Elasticsearch search results filtering

I am new to Elasticsearch and I am using REST API for PHP to play around with data returned. I am using following code to retrieve data.
$params = [
'index' => 'my_search',
'type' => 'mytype',
'from' => 0,
'size' => 10,
'body' => [
'query' => [
'bool' => [
'must' => [
[ 'match' => [ 'validated' => true ] ],
[ 'match' => [ 'image' => true ] ]
]
]
],
'sort' => [
'created_at' => [ 'order' => 'asc']
]
]
];
Above code returns data perfectly matching "validated=>true" and "image=>true".
Further I want to add open text search like we use /_search/?q=Apple macbook. I have tried to use match, multi_match, query_string options, but couldn't get success.
So, I want to retrieve results from ES that have "validated=>true", "image=>true" and matches with text "Apple macbook".
Thanks in advance.
You can try with query_string or simple_query_string
$params = [
'index' => 'my_search',
'type' => 'mytype',
'from' => 0,
'size' => 10,
'body' => [
'query' => [
'bool' => [
'must' => [
[ 'match' => [ 'validated' => true ] ],
[ 'match' => [ 'image' => true ] ],
[ 'query_string' => [ 'query' => 'Apple macbook' ] ]
]
]
],
'sort' => [
'created_at' => [ 'order' => 'asc']
]
]
];
$params = [
'index' => 'my_search',
'type' => 'mytype',
'from' => 0,
'size' => 10,
'body' => [
'query' => [
'bool' => [
'must' => [
[ 'match' => [ 'validated' => true ] ],
[ 'match' => [ 'image' => true ] ],
[ 'simple_query_string' => [ 'query' => 'Apple macbook' ] ]
]
]
],
'sort' => [
'created_at' => [ 'order' => 'asc']
]
]
];
you can also do this by
enabling all_field mapping for your index, you can do that by following the below URL
https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-all-field.html
and then use the below ES query:
$params = [
'index' => 'my_search',
'type' => 'mytype',
'from' => 0,
'size' => 10,
'body' => [
'query' => [
'bool' => [
'must' => [
[ 'match' => [ '_all' => 'Apple macbook' ] ],
[ 'match' => [ 'validated' => true ] ],
[ 'match' => [ 'image' => true ] ]
]
]
],
'sort' => [
'created_at' => [ 'order' => 'asc']
]
]
];

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

Categories