I have an elasticsearch that is working fine and so is my php client used to query the index. However, my client works only with one index. When I try adding multiple indices in a string as shown here my code breaks.
Here is a snippet of my code:
if ($q2 == '') {
$query = $client->search([
'index' => 'trial2',
'body' => [
'from' => $from,
'size' => $size,
'query' => [
'multi_match' => [
'query' => $q,
'operator' => $op,
'fields' => ['content','file','file.extension','meta', 'path']
]
]
]
]);
$data['q'] = $q;
}
elseif ($q2 == "docx") {
$params = [
'index' => 'trial2',
'type' => '_doc',
'body' => [
'query' => [
'bool' => [
'must' => [
[ 'terms' => [ 'file.extension' => ["docx", "doc", "txt", "rtf"] ] ],
[ 'match' => [ 'content' => $q ] ],
]
]
]
]
];
$query = $client->search($params);
$data['q'] = $q;
}
I would like to query several indexes (trial2, trial3 and trial4). How do I do it?
Related
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' ] ];
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.
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"
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 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
]
]
]
]
]
];