Elastic Search PHP Client Date Range Query - php

I have this code what executes an query.
I am using the official php elastic search library.
https://github.com/elastic/elasticsearch-php
The field "Tijdsperiode" is in this format : ( 2016-01-30 00:00:00 ) ( YY-MM-DD HH:MM:SS )
$params = [
'index' => 'veenendaal2',
'type' => 'passanten2',
'body' => [
'query' => [
'match_all' => [],
'filter' => [
'range' => [
'Tijdsperiode' => [
'gte' => '2016-01-30 01:00:00',
'lte' => '2016-01-30 08:00:00'
]
]
]
],
],
];
$response = $client->search($params);
var_dump($response);
I just wonder what the format need to be to get results between the 2 dates.
When i do this :
$params = [
'index' => 'veenendaal2',
'type' => 'passanten2',
'body' => [
'query' => [
'match_all' => [],
],
],
];
It's working fine but i need the results between the 2 dates!
I also tried this but with no result :
$json = '{
"query": {
"bool": {
"must": [
{
"range": {
"Tijdsperiode": {
"gt": "2016-01-30 07:00:00",
"lt": "2016-01-30 09:00:00"
}
}
}
]
}
}
}';
$client = ckan_graphmapper_client();
$params = [
'index' => 'veenendaal2',
'type' => 'passanten2',
'body' => $json
];
$response = $client->search($params);
I also tried it with a mapping :
$params = [
'index' => 'veenendaal2',
'type' => 'passanten2',
'size' => $size,
'body' => [
'query' => [
"match_all" => [],
'filter' => [
'range' => [
'Tijdsperiode' => [
'gte' => '2016-01-30 01:00:00',
'lte' => '2016-01-30 08:00:00'
]
]
]
],
'mappings' => [
'_default_' => [
'properties' => [
'Tijdsperiode' => [
'type' => 'date',
'format' => 'yyyy-MM-dd HH:mm:ss'
]
]
]
]
]
];
How to do the right syntax for choosing between 2 dates?. Both string formats ( 2016-01-30 00:00:00 ) ( YY-MM-DD HH:MM:SS )
Thanks!

So it turns out my import of data was not mapping the correct way.
The date field was recognised as a string value.
When i finnaly had the correct mapping in elastic i could do this query :
$query = [
'query' => [
'filtered' => [
'query' => [
'match_all' => []
],
'filter' => [
'range' => [
'Tijdsperiode' => [
'gte' => $start,
'lte' => $end
]
]
]
]
]
];

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"
]
}
}
}
}
}

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 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
]
]
]

Sort by name elasticsearch

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

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

Categories