简体   繁体   English

ElasticSearch PHP API按字段搜索

[英]ElasticSearch PHP API Search by Field

Resolved 解决

I am trying to get a results back using the official elasticsearch 5.0 php package. 我正在尝试使用官方的Elasticsearch 5.0 php包来获得结果。 I can get the desired results via URL and cURL. 我可以通过URL和cURL获得所需的结果。

http://localhost:9200/index/doctype/_search?q=status:available

I get 8 hits and 8 hit results, as expected. 正如预期的那样,我得到了8次点击和8次点击结果。

 curl -XGET 'localhost:9200/index/doctype/_search?pretty' -d' { "query" : { "term" : { "status" : "available" } } }' 

I get 8 hits and 8 hit results, as expected. 正如预期的那样,我得到了8次点击和8次点击结果。

Using the following various bits of code, I get 8 results but 0 hits. 使用以下各种代码,我得到8个结果,但0次命中。

$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. 我在$ params以及其他代码段中正确设置了索引和doctype。 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 您的fromsize参数很可能计算不正确,并且from可能大于8,因此为什么您看到results.hits.total: 8但在results.hits.hits什么都没有

Make sure that from is 0 and size is at least 8 (default is 10) and you'll see your hits. 确保from为0, size至少为8(默认值为10),您将看到匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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