elasticsearch-php: Search method returns NULL

I’m having a puzzling problem with a website that uses Elasticsearch—Elasticsearch is returning results to elasticsearch-php, which is then mysteriously failing to output them. This is with Elasticsearch v0.90.9, elasticsearch-php v0.4.5, and PHP v5.3.10. (Just in case, I’ve also tried elasticsearch-php v1.2.0, despite its incompatibility with pre-v1.0 Elasticsearch, which presented precisely the same problem.)

When I directly query my Elasticsearch index, Elasticsearch happily returns the results. That’s if I make the request via cURL. If I make the request via elasticsearch-php, Elasticsearch still returns the results, but they don’t make it past elasticsearch-php.

For example, here I search for a term that I know will return a small number of results (Jaquith, my last name) from my index (business) of a couple of million records:

require 'vendor/autoload.php';
$config['logging'] = TRUE;
$client = new Elasticsearch\Client($config);
$params['index'] = 'business';
$params['body']['query']['match']['_all'] = 'Jaquith';
$result = $client->search($params);
var_dump($result);

The output of var_dump($result) is NULL. Not false, but NULL. If I I deliberately specify invalid options, such as an invalid index (e.g., $params['index'] = 'fake_index'), then it returns false, so it’s not like it can only return NULL.

Where things get weird is when looking at elasticsearch.log. Here is what’s logged from the execution of the above:

[2014-08-04 21:34:13] log.DEBUG: Request Body ["{\"query\":{\"match\":{\"_all\":\"Jaquith\"}}}"] {"file":"/vol/www/business.openva.com/htdocs/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/AbstractConnection.php","line":113,"class":"Elasticsearch\\Connections\\AbstractConnection","function":"logRequestSuccess"}
[2014-08-04 21:34:13] log.INFO: Request Success: {"method":"POST","uri":"http://localhost:9200/business/_search","headers":{"host":["localhost:9200"],"user-agent":["Guzzle/3.9.1 curl/7.22.0 PHP/5.3.10-1ubuntu3.13"],"content-length":[38]},"HTTP code":200,"duration":0.009757} {"file":"/vol/www/business.openva.com/htdocs/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/AbstractConnection.php","line":123,"class":"Elasticsearch\\Connections\\AbstractConnection","function":"logRequestSuccess"}
[2014-08-04 21:34:13] log.DEBUG: Response ["{\"took\":7,\"timed_out\":false,\"_shards\":{\"total\":5,\"successful\":5,\"failed\":0},\"hits\":{\"total\":4,\"max_score\":6.5482264,\"hits\":[{\"_index\":\"business\",\"_type\":\"5\",\"_id\":\"ZTU0BFYcSIWQWZYdCaNS1g\",\"_score\":6.5482264, \"_source\" : {\"first_name\": \"MARY\", \"last_name\": \"JAQUITH\", \"middle_name\": \"\", \"id\": \"0562703\", \"title\": \"PRESIDENT\"},},{\"_index\":\"business\",\"_type\":\"5\",\"_id\":\"L9PNiEdUSGmbZAq1bgPjQQ\",\"_score\":4.9117994, \"_source\" : {\"first_name\": \"MARY\", \"last_name\": \"JAQUITH\", \"middle_name\": \"T\", \"id\": \"0562703\", \"title\": \"S/T\"},},{\"_index\":\"business\",\"_type\":\"9\",\"_id\":\"C7Web8w6T0S1zhyUtbjaAw\",\"_score\":2.0459418, \"_source\" : {\"expiration-date\": null, \"agent-state\": \"VA\", \"agent_status\": \"1\", \"agent_court_locality\": \"LOUDOUN COUNTY\", \"state-formed\": \"VIRGINIA\", \"id\": \"S496907\", \"agent-name\": \"NATALIE ANDERSON JAQUITH\", \"city\": \"Sterling\", \"zip\": \"20164\", \"state\": \"VA\", \"agent-city\": \"STERLING\", \"status\": \"ACTIVE\", \"agent_zip\": \"20164\", \"street-2\": \"\", \"street-1\": \"206 Laura Anne Drive\", \"date\": \"2014-03-17\", \"agent-street-1\": \"206 LAURA ANNE DRIVE\", \"agent_date\": \"2014-03-17\", \"name\": \"Happy Little Paintings, LLC\", \"agent-street-2\": \"\", \"industry\": \"00\", \"status-date\": \"2014-03-17\", \"address-date\": null},},{\"_index\":\"business\",\"_type\":\"2\",\"_id\":\"0LYakZfaTaKj4lGxaF0hGw\",\"_score\":1.6373172, \"_source\" : {\"agent_name\": \"MARY G JAQUITH\", \"address_date\": null, \"stock_ind\": \"S\", \"agent_status\": \"OFFICER\", \"agent_court_locality\": \"VIRGINIA BEACH CITY\", \"total_shares\": \"00000005000\", \"incorporation_date\": \"2001-08-02\", \"stock_class\": \"00000000\", \"id\": \"0562703\", \"city\": \"VA BEACH\", \"zip\": \"23457\", \"state\": \"VA\", \"assessment\": \"NORMAL ASSESSMENT\", \"merged\": \"\", \"state_formed\": \"VIRGINIA\", \"status\": \"ACTIVE\", \"agent_zip\": \"23457\", \"agent_city\": \"VIRGINIA BEACH\", \"status_date\": \"2009-03-10\", \"street_1\": \"4166 CHARITY NECK ROAD\", \"street_2\": \"\", \"agent_date\": \"2013-04-29\", \"name\": \"CORNERSTONE FARMS, LTD.\", \"expiration_date\": null, \"industry\": \"GENERAL\", \"agent_street_1\": \"4166 CHARITY NECK RD.\", \"agent_street_2\": \"\", \"number_shares\": \"00000000000\", \"agent_state\": \"VA\"},}]}}"] {"file":"/vol/www/business.openva.com/htdocs/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/AbstractConnection.php","line":124,"class":"Elasticsearch\\Connections\\AbstractConnection","function":"logRequestSuccess"}
[2014-08-04 21:34:13] trace.INFO: curl -XPOST 'http://localhost:9200/business/_search?pretty=true' -d '{"query":{"match":{"_all":"Jaquith"}}}' [] []
[2014-08-04 21:34:13] trace.DEBUG: Response: {"response":"{\"took\":7,\"timed_out\":false,\"_shards\":{\"total\":5,\"successful\":5,\"failed\":0},\"hits\":{\"total\":4,\"max_score\":6.5482264,\"hits\":[{\"_index\":\"business\",\"_type\":\"5\",\"_id\":\"ZTU0BFYcSIWQWZYdCaNS1g\",\"_score\":6.5482264, \"_source\" : {\"first_name\": \"MARY\", \"last_name\": \"JAQUITH\", \"middle_name\": \"\", \"id\": \"0562703\", \"title\": \"PRESIDENT\"},},{\"_index\":\"business\",\"_type\":\"5\",\"_id\":\"L9PNiEdUSGmbZAq1bgPjQQ\",\"_score\":4.9117994, \"_source\" : {\"first_name\": \"MARY\", \"last_name\": \"JAQUITH\", \"middle_name\": \"T\", \"id\": \"0562703\", \"title\": \"S/T\"},},{\"_index\":\"business\",\"_type\":\"9\",\"_id\":\"C7Web8w6T0S1zhyUtbjaAw\",\"_score\":2.0459418, \"_source\" : {\"expiration-date\": null, \"agent-state\": \"VA\", \"agent_status\": \"1\", \"agent_court_locality\": \"LOUDOUN COUNTY\", \"state-formed\": \"VIRGINIA\", \"id\": \"S496907\", \"agent-name\": \"NATALIE ANDERSON JAQUITH\", \"city\": \"Sterling\", \"zip\": \"20164\", \"state\": \"VA\", \"agent-city\": \"STERLING\", \"status\": \"ACTIVE\", \"agent_zip\": \"20164\", \"street-2\": \"\", \"street-1\": \"206 Laura Anne Drive\", \"date\": \"2014-03-17\", \"agent-street-1\": \"206 LAURA ANNE DRIVE\", \"agent_date\": \"2014-03-17\", \"name\": \"Happy Little Paintings, LLC\", \"agent-street-2\": \"\", \"industry\": \"00\", \"status-date\": \"2014-03-17\", \"address-date\": null},},{\"_index\":\"business\",\"_type\":\"2\",\"_id\":\"0LYakZfaTaKj4lGxaF0hGw\",\"_score\":1.6373172, \"_source\" : {\"agent_name\": \"MARY G JAQUITH\", \"address_date\": null, \"stock_ind\": \"S\", \"agent_status\": \"OFFICER\", \"agent_court_locality\": \"VIRGINIA BEACH CITY\", \"total_shares\": \"00000005000\", \"incorporation_date\": \"2001-08-02\", \"stock_class\": \"00000000\", \"id\": \"0562703\", \"city\": \"VA BEACH\", \"zip\": \"23457\", \"state\": \"VA\", \"assessment\": \"NORMAL ASSESSMENT\", \"merged\": \"\", \"state_formed\": \"VIRGINIA\", \"status\": \"ACTIVE\", \"agent_zip\": \"23457\", \"agent_city\": \"VIRGINIA BEACH\", \"status_date\": \"2009-03-10\", \"street_1\": \"4166 CHARITY NECK ROAD\", \"street_2\": \"\", \"agent_date\": \"2013-04-29\", \"name\": \"CORNERSTONE FARMS, LTD.\", \"expiration_date\": null, \"industry\": \"GENERAL\", \"agent_street_1\": \"4166 CHARITY NECK RD.\", \"agent_street_2\": \"\", \"number_shares\": \"00000000000\", \"agent_state\": \"VA\"},}]}}","method":"POST","uri":"http://localhost:9200/business/_search","HTTP code":200,"duration":0.009757} []

Elasticsearch returns results just fine. Somewhere between elasticsearch-php receiving that JSON and returning the content as PHP it’s becoming, simply, NULL.

There’s a non-trivial chance that I’m doing something stupid here, but I can’t figure out what it is. Any ideas of what’s going on?

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 19 (7 by maintainers)

Most upvoted comments

Leave it to a Perl guy to find the problem 😃

Glad you got it working! @clintongormley had mentioned the illegal JSON syntax to me a while ago, but I didn’t think it was a problem because your dataset worked just fine on my end (and I didn’t see any invalid JSON). Still not sure why the problem didn’t reproduce itself, perhaps ES was polling a different set of documents on my computer so it never showed up. Oh well.

Funnily enough, I have a bug report open at ES about this very issue: https://github.com/elasticsearch/elasticsearch/issues/7299

Elasticsearch only parses the first JSON object that it encounters. If that first JSON object is valid, the document is considered a success. The “gotcha” is that ES will still index the entire request body into the _source field, which can include invalid JSON.

Technically this isn’t a problem for ES, since it only displays the _source in the response. All operations go against the indexed data structure. But for clients that have to parse JSON responses…it explodes.

I thought I was checking for invalid JSON and throwing an exception, but sure enough, it’s just returning the output of json_decode: https://github.com/elasticsearch/elasticsearch-php/blob/master/src/Elasticsearch/Serializers/EverythingToJSONSerializer.php#L49

…which happens to be NULL if the JSON was invalid. Sigh. Should have noticed this. I definitely count this as a bug, so we’ll get this fixed to throw a real exception and helpful text 😃

Thanks again for the report and sticking through this whole time!