1

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?

9
  • What do you mean with "I get 8 results but 0 hits."? Commented Dec 29, 2016 at 12:45
  • ElasticSearch will return the number of documents as well as the hits to the query. The number of records is stored in $results['hits']['total'] and the records themselves are stored in $results['hits']['hits']; of which I get 8 total but empty hits. Commented Dec 29, 2016 at 12:51
  • ok I see, what if you add $params['size'] = 10 to your code? Commented Dec 29, 2016 at 12:52
  • 1
    In my opinion from has a value higher than 8 and that's the reason why you don't see any hits. Can you print out the value of from and size? Commented Dec 29, 2016 at 12:59
  • 1
    There you go, glad you figured it out! Commented Dec 29, 2016 at 13:06

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.