Resolved
I am trying to get a results back using the official elasticsearch 5.0 php package. I can get the desired results via URL and cURL.
http://localhost:9200/index/doctype/_search?q=status:available
I get 8 hits and 8 hit results, as expected.
curl -XGET 'localhost:9200/index/doctype/_search?pretty' -d'
{
"query" : {
"term" : { "status" : "available" }
}
}'
I get 8 hits and 8 hit results, as expected.
Using the following various bits of code, I get 8 results but 0 hits.
$params['body']['query']['term']['status'] = 'available';
$params['q'] = 'status:available';
$params['body'] = [
'query' => [
'bool' => [
'must' => [
['match' => ['status' => 'available']],
]
]
]
];
Called via:
$params = [
'index' => 'index',
'type' => 'doctype',
];
$skip = !empty($_GET['page']) ? (int)$_GET['page'] - 1 : 0;
$listings_per_page = 25;
$params['from'] = $skip * $per_page;
$params['size'] = $per_page;
// show only available
$params['body'] = [
'query' => [
'bool' => [
'must' => [
['match' => ['status' => 'available']],
]
]
]
];
$hosts = [env('ES_HOST', 'elasticsearch') . ':' . env('ES_PORT', '9200')];
$client = ClientBuilder::create()->setHosts($hosts)->build();
$es_results = $client->search($params);
I have the index and doctype set properly in the $params as well in other bits of code. What could I be doing wrong here?
It is very likely that your from and size parameters are not computed correctly and from might be higher than 8, hence why you see results.hits.total: 8 but nothing in results.hits.hits
Make sure that from is 0 and size is at least 8 (default is 10) and you'll see your hits.
Related
i use below code and get below error
for connect php to elasticsearch i use elasticsearch php client
$params = [
'index'=>'index1,index2',
'from'=>$startfrom,
'size'=>$maxperpage,
'sort' => array('id:desc'),
'body' => [
'query' => [
'query_string' => [
'query' => "\"$search_text\""
]
]
],
];
if($limit_time['start']){
$params['body']['query']['range']['date_insert']['gte']=$limit_time['start'];
}
if($limit_time['end']){
$params['body']['query']['range']['date_insert']['lte']=$limit_time['end'];
}
error :
{"error":{"root_cause":[{"type":"parsing_exception","reason":"[query_string] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":104}],"type":"parsing_exception","reason":"[query_string] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":104},"status":400}
You should try something like this instead. The query_string and the range queries need to be combined into a bool/filter query:
$params = [
'index'=>'index1,index2',
'from'=>$startfrom,
'size'=>$maxperpage,
'sort' => array('id:desc'),
'body' => [
'query' => [
'bool' => [
'filter' => [
[
'query_string' => [
'query' => "\"$search_text\""
]
]
]
]
]
],
];
$range = ['range' => ['date_insert' => []]]
if($limit_time['start']){
$range['range']['date_insert']['gte'] = $limit_time['start'];
}
if($limit_time['end']){
$range['range']['date_insert']['lte'] = $limit_time['end'];
}
$params['body']['query']['bool']['filter'][] = $range;
i solved problem
but thanks a lot "Val" for your help
i use below code :
$params = [
'index'=>'index1,index2',
'from'=>$startfrom,
'size'=>$maxperpage,
'sort' => array('id:desc')
];
if($search_text){
$params['body']['query']['bool']['must'][0]['match_phrase']['src']=$search_text;
}
if($use_time_status){
if($limit_time['start']){
$params['body']['query']['bool']['must'][1]['range']['date_insert']['gte']=$limit_time['start'];
}
if($limit_time['end']){
$params['body']['query']['bool']['must'][1]['range']['date_insert']['lte']=$limit_time['end'];
}
}
I have a json file which I looped through it and index it in elastic. and after that I want to be able to search through my data.
this is my json file :
https://github.com/mhndev/iran-geography/blob/master/tehran_intersection.json
which looks like :
{
"RECORDS":[
{
"first":{
"name":"ابن بابویه",
"slug":"Ibn Babawayh"
},
"second":{
"name":"میرعابدینی",
"slug":"Myrabdyny"
},
"latitude":"35.601605",
"longitude":"51.444208",
"type":"intersection",
"id":1,
"search":"ابن بابویه میرعابدینی,Ibn Babawayh Myrabdyny,ابن بابویه تقاطع میرعابدینی,Ibn Babawayh taghato Myrabdyny",
"name":"ابن بابویه میرعابدینی",
"slug":"Ibn Babawayh Myrabdyny"
},
...
]
}
when my query is : "mir", I expect my result to be records which has "mirabedini", "mirdamad", "samir", and every other word which contains this string.
but I just get words which are exactly "mir"
and this is my php code for search :
$fields = ['search','slug'];
$params = [
'index' => 'digipeyk',
'type' => 'location',
'body' => [
'query' => [
'match' => [
'fields' => $fields,
'query' => $_GET['query']
]
],
'from' => 0,
'size' => 10
]
];
$client = ClientBuilder::create()->build();
$response = $client->search($params);
and also this my php code for indexing documents.
$client = ClientBuilder::create()->build();
$deleteParams = ['index' => 'digipeyk'];
$response = $client->indices()->delete($deleteParams);
$intersections = json_decode(file_get_contents('data/tehran_intersection.json'), true)['RECORDS'];
$i = 1;
foreach($intersections as $intersection){
echo $i."\n";
$params['index'] = 'digipeyk';
$params['id'] = $intersection['id'];
$params['type'] = 'location';
$params['body'] = $intersection;
$response = $client->index($params);
$i++;
}
I'm using php 7 and elasticsearch 2.3
match query doesn't support wildcard query by default, so you have to use wildcard instead of that.
$fields = ['search','slug'];
$params = [
'index' => 'digipeyk',
'type' => 'location',
'body' => [
'query' => [
'wildcard' => [
'query' => '*'.$_GET['query'].'*'
]
],
'from' => 0,
'size' => 10
]
];
$client = ClientBuilder::create()->build();
$response = $client->search($params);
For more information about wildcard in elastic visit following link : https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html
For that:
'query' => $_GET['query']
You will be burn in hell :D
I recently tried to use ES. So I set it up in a cloud 9 environnement. I inserted data using a curl request file and I can see them with
http://mydomain/ingredients/aliments/_search?size=350&pretty=true
I then tried to set up elastic SDK (v.2.0) with Silex but I can't get the same output...
Here is my code :
$client = $app['elasticsearch'];
$params = array(
'size' => 350,
'index' => 'ingredients',
'type'=>'aliment',
'body' => array(
'query'=>array(
'match_all' => new \stdClass()
)
)
);
$ingredients = $client->search($params);
The output is NULL but when I do the following :
$params = array(
'index' => 'ingredients',
'type' => 'aliment'
);
$count = $client->count($params);
The output is as expected : {"count":240,"_shards":{"total":5,"successful":5,"failed":0}}
I've already spent a few hours trying to figure what's going on, I tried to replace the 'query' args with a json string, I tried empty array instead of the new stdClass but nothing seems to work.
Edit : I read the documentation again and tried the official example :
$client = $app['elasticsearch'];
$params = [
"search_type" => "scan", // use search_type=scan
"scroll" => "30s", // how long between scroll requests. should be small!
"size" => 50, // how many results *per shard* you want back
"index" => "ingredients",
"body" => [
"query" => [
"match_all" => []
]
]
];
$output = $client->search($params);
$scroll_id = $output['_scroll_id']; /*<<<This works****/
while (\true) {
// Execute a Scroll request
$response = $client->scroll([
"scroll_id" => $scroll_id, //...using our previously obtained _scroll_id
"scroll" => "30s" // and the same timeout window
]
);
var_dump($response); /*<<<THIS IS NULL****/
...
}
And unfortunately got same null result...
What am I doing wrong ?
Thanks for reading.
In my case it works this way:
$json = '{
"query": {
"match_all": {}
}
}';
$params = [
'type' => 'my_type',
'body'=> $json
];
I found out that the inserted data was malformed.
Accessing some malformed data via browser URL seems to be OK but not with a curl command line or the SDK.
Instead of {name:"Yaourt",type:"",description:""} , I wrote {"name":"Yaourt","description":""} in my requests file and now everything work as expected !
#ivanesi 's answer works. You may also try this one:
$params["index"] = $indexName;
$params["body"]["query"]["match_all"] = new \stdClass();
I was not able to find a working example on how to query Elasticsearch using the completion suggester in PHP (elasticsearch-php).
Querying via CURL, e.g.
curl -X POST 'localhost:9200/tstidx/_suggest?pretty' -d '{
"try" : {
"text" : "a",
"completion" : {
"field" : "suggest"
}
}
}'
works, so the only problem is the query part in PHP.
How do I use the API to query Elasticsearch using the completion suggester?
The PHP ES client has a method called suggest that you can use for that purpose:
$params = [
'index' => 'tstidx',
'body' => [
'try' => [
'text' => 'a',
'completion' => [ 'field' => 'suggest' ]
]
]
];
$client = ClientBuilder::create()->build();
$response = $client->suggest($params);
Using the PHP elasticsearch API
<?Php
include 'autoload.php';
$elastic_search = new ElasticApiService();
$search_params = array(
'index' => 'my_index',
'search_type' => 'match',
'search_key' => 'citynamefield',
'search_value' => 'orlando'
);
$search_results = $elastic_search->searchElastic($search_params);
echo '<pre>';
print_r($search_results);
I am trying to create a spatial database with neo4j 3.0.2 and neo4j-spatial for 3.0.2. I have installed the plugin and I have checked that the plugin is running with cURL curl -v http://neo4j:neo4j#localhost:7474/db/data/ which outputs following:
{
"extensions" : {
"SpatialPlugin" : {
"addSimplePointLayer" : "http://localhost:7474/db/data/ext/SpatialPlugin/graphdb/addSimplePointLayer",
"addNodesToLayer" : "http://localhost:7474/db/data/ext/SpatialPlugin/graphdb/addNodesToLayer",
"findClosestGeometries" : "http://localhost:7474/db/data/ext/SpatialPlugin/graphdb/findClosestGeometries",
"addGeometryWKTToLayer" : "http://localhost:7474/db/data/ext/SpatialPlugin/graphdb/addGeometryWKTToLayer",
"findGeometriesWithinDistance" : "http://localhost:7474/db/data/ext/SpatialPlugin/graphdb/findGeometriesWithinDistance",
"addEditableLayer" : "http://localhost:7474/db/data/ext/SpatialPlugin/graphdb/addEditableLayer",
"addCQLDynamicLayer" : "http://localhost:7474/db/data/ext/SpatialPlugin/graphdb/addCQLDynamicLayer",
"addNodeToLayer" : "http://localhost:7474/db/data/ext/SpatialPlugin/graphdb/addNodeToLayer",
"getLayer" : "http://localhost:7474/db/data/ext/SpatialPlugin/graphdb/getLayer",
"findGeometriesInBBox" : "http://localhost:7474/db/data/ext/SpatialPlugin/graphdb/findGeometriesInBBox",
"updateGeometryFromWKT" : "http://localhost:7474/db/data/ext/SpatialPlugin/graphdb/updateGeometryFromWKT",
"findGeometriesIntersectingBBox" : "http://localhost:7474/db/data/ext/SpatialPlugin/graphdb/findGeometriesIntersectingBBox"
}
},
"node" : "http://localhost:7474/db/data/node",
"relationship" : "http://localhost:7474/db/data/relationship",
"node_index" : "http://localhost:7474/db/data/index/node",
"relationship_index" : "http://localhost:7474/db/data/index/relationship",
"extensions_info" : "http://localhost:7474/db/data/ext",
"relationship_types" : "http://localhost:7474/db/data/relationship/types",
"batch" : "http://localhost:7474/db/data/batch",
"cypher" : "http://localhost:7474/db/data/cypher",
"indexes" : "http://localhost:7474/db/data/schema/index",
"constraints" : "http://localhost:7474/db/data/schema/constraint",
"transaction" : "http://localhost:7474/db/data/transaction",
"node_labels" : "http://localhost:7474/db/data/labels",
"neo4j_version" : "3.0.2"
* Connection #0 to host localhost left intact
}* Closing connection #0
Now I can create a new simplePointLayer:
// define entity manager
$client = $this->get('neo4j.spatial_manager')->getClient();
// 1. Create a pointlayer
$request = $client->request('POST',
'/db/data/ext/SpatialPlugin/graphdb/addSimplePointLayer',
[
'json' => [
'layer' => 'geom',
'lat' => 'lat',
'lon' => 'lon',
],
]
);
var_dump($request);
This creates the spatial root node with the rTree. But when I now want to create a spatial index with following:
// 2. Create a spatial index
$request = $client->request('POST',
'/db/data/index/node/',
[
'json' => [
'name' => 'geom',
'config' => [
'provider' => 'spatial',
'geometry_type' => 'point',
'lat' => 'lat',
'lon' => 'lon',
],
],
]
);
var_dump($request);
I am confronted with the error-message:
"message" : "No index provider 'spatial' found.
What am I doing wrong? I have checked a lot of forums and so on, but the answer always seems to be to install the spatial plugin, which I have and it seems to be working according to the first output.
EDIT 15.06.2016
Whats weird is that I can add nodes to the rTree:
// Create a node with spatial data
$json = [
'team' => 'REDBLUE',
'name' => 'TEST',
'lat' => 25.121075,
'lon' => 89.990630,
];
$response = $client->request('POST',
'/db/data/node',
[
'json' => $json
]
);
$node = json_decode($response->getBody(), true)['self'];
// Add the node to the layer
$response = $client->request('POST',
'/db/data/ext/SpatialPlugin/graphdb/addNodeToLayer',
[
'json' => [
'layer' => 'geom',
'node' => $node,
],
]
);
$data = json_decode($response->getBody(), true);
var_dump($data);
And I can query the nodes through REST:
$request = $client->request('POST',
'/db/data/ext/SpatialPlugin/graphdb/findGeometriesWithinDistance',
[
'json' => [
'layer' => 'geom',
'pointX' => 89.99506,
'pointY' => 25.121260,
'distanceInKm' => 10,
],
]
);
$data = json_decode($request->getBody(), true);
var_dump($data);
But why is it not letting me create an index? Or does it do the indexing automatically? And if so, how can I query using CYPHER (in the web console for example)?
Any help would be appreciated! Cheers
The index provider was removed (back then it was the only means to provide integration with Cypher), in favor of user defined procedures for Spatial.
see: https://github.com/neo4j-contrib/spatial/blob/master/src/test/java/org/neo4j/gis/spatial/procedures/SpatialProceduresTest.java#L113
As this is a new major release (for 3.0) we found it sensible to remove the index provider.