简体   繁体   中英

ElasticSearch PHP API Search by Field

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM