I run the following PHP:
function curl_delete($url)
{
$ch = curl_init();
curl_setopt_array($ch,
[
CURLOPT_CUSTOMREQUEST => 'DELETE',
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true
]);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
function curl_put($url, $data)
{
$ch = curl_init();
curl_setopt_array($ch,
[
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER =>
[
'Content-Type: application/json',
'Content-Length: ' . strlen($data),
],
CURLOPT_POSTFIELDS => $data,
CURLOPT_RETURNTRANSFER => true
]);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
function curl_post($url, $data)
{
$ch = curl_init();
curl_setopt_array($ch,
[
CURLOPT_POST => true,
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_POSTFIELDS => $data,
CURLOPT_RETURNTRANSFER => true
]);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
echo curl_delete('localhost:9200/my_index?pretty');
echo curl_put('localhost:9200/my_index?pretty', '{"settings": {"number_of_shards": 1}}');
echo curl_post('localhost:9200/my_index/my_type/_bulk?pretty', '
{ "index": { "_id": 1 }}
{ "title": "The quick brown fox" }
{ "index": { "_id": 2 }}
{ "title": "The quick brown fox jumps over the lazy dog" }
{ "index": { "_id": 3 }}
{ "title": "The quick brown fox jumps over the quick dog" }
{ "index": { "_id": 4 }}
{ "title": "Brown fox brown dog" }
');
echo curl_post('localhost:9200/my_index/my_type/_refresh?pretty', '{}');
echo curl_post('localhost:9200/my_index/my_type/_search?pretty', '{}');
and I get the following output with no hits:
{
"acknowledged" : true
}
{
"acknowledged" : true,
"shards_acknowledged" : true
}
{
"took" : 92,
"errors" : false,
"items" : [
{
"index" : {
"_index" : "my_index",
"_type" : "my_type",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"created" : true,
"status" : 201
}
},
{
"index" : {
"_index" : "my_index",
"_type" : "my_type",
"_id" : "2",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"created" : true,
"status" : 201
}
},
{
"index" : {
"_index" : "my_index",
"_type" : "my_type",
"_id" : "3",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"created" : true,
"status" : 201
}
},
{
"index" : {
"_index" : "my_index",
"_type" : "my_type",
"_id" : "4",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"created" : true,
"status" : 201
}
}
]
}
{
"_index" : "my_index",
"_type" : "my_type",
"_id" : "_refresh",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"created" : true
}
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : null,
"hits" : [ ]
}
}
But if afterwards I run the same empty query from the command line:
"d:\Program Files\curl\bin\curl.exe" -XPOST localhost:9200/my_index/my_type/_search?pretty" -H "Content-Type: application/json" -d"{}"
I get all the hits as I'm supposed to:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"failed" : 0
},
"hits" : {
"total" : 4,
"max_score" : 1.0,
"hits" : [
{
"_index" : "my_index",
"_type" : "my_type",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"title" : "The quick brown fox"
}
},
{
"_index" : "my_index",
"_type" : "my_type",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"title" : "The quick brown fox jumps over the lazy dog"
}
},
{
"_index" : "my_index",
"_type" : "my_type",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"title" : "The quick brown fox jumps over the quick dog"
}
},
{
"_index" : "my_index",
"_type" : "my_type",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"title" : "Brown fox brown dog"
}
}
]
}
}
Can someone please tell me what's wrong with my last line of PHP? Why is it getting no hits?
When running the bulk query, you can see that all documents were created. However, the index is not yet refreshed, hence why you don't get any hits when running a search immediately.
What you can do is to call refresh just before the search, like this:
echo curl_post('localhost:9200/my_index/_refresh', '{}');
Or simply add a refresh
parameter to your bulk call like this:
echo curl_post('localhost:9200/my_index/my_type/_bulk?pretty=true&refresh=true', ...
And then you can issue your search query normally:
echo curl_post('localhost:9200/my_index/my_type/_search?pretty', '{}');