php - ElasticSearch PHP API按字段搜索

标签 php elasticsearch

已解决

我正在尝试使用官方的Elasticsearch 5.0 php包返回结果。我可以通过URL和cURL获得所需的结果。

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

正如预期的那样,我得到了8次点击和8次点击结果。

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


正如预期的那样,我得到了8次点击和8次点击结果。

使用以下各种代码,我得到8个结果,但0次命中。
$params['body']['query']['term']['status'] = 'available';
$params['q'] = 'status:available';
$params['body'] = [
        'query' => [
            'bool' => [
                'must' => [
                    ['match' => ['status' => 'available']],
                ]
            ]
        ]
    ];

通过以下方式调用:

$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);

我在$ params以及其他代码段中正确设置了索引和doctype。我在这里可能做错了什么?

最佳答案

您的fromsize参数很可能计算不正确,并且from可能高于8,因此为什么您看到results.hits.total: 8但在results.hits.hits中什么都看不到

确保from为0,且size至少为8(默认值为10),您会看到匹配。

关于php - ElasticSearch PHP API按字段搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41379944/

相关文章:

php - 网站如何在不重新加载页面的情况下检查登录凭据?

php - 在使用 WordPress Submit_button() 提交之前运行 PHP 函数

performance - 如何在Elasticsearch和Python中初始化标签数组列表

php - Wordpress get_users() 通过比较查询返回所有用户

PHP 文件托管

php - 在 Laravel 中扩展 TCPDF 类

elasticsearch - 嵌套计数查询

json - Logstash不解析JSON

spring - JAVA spring (JPA)中的PostgreSQL + Elasticsearch同步

elasticsearch - 如何在Elasticsearch中使用match_phrase查询使用正则表达式搜索子对象的值字段