doushansu9012 2017-05-09 21:10
浏览 36
已采纳

Elasticsearch无法在PHP中打印数组

I followed this Video as a guide to get elasticsearch up and running with PHP, but I am unable to print or view the results. I can print the raw data, but i'm unable to go any deeper.

  • Operating system: Debian (jessie)
  • PHP version: 5.6.30
  • Java version: 1.7.0
  • Elasticsearch version: 5.4.0

I use this code to request the data:

// Creating the request.
if(!defined('JSON_PRESERVE_ZERO_FRACTION'))
{
    define('JSON_PRESERVE_ZERO_FRACTION', 1024);
}

require_once('vendor/autoload.php'); // autoload which makes it "easier"
                                     // to get values from elasticsearch
$hosts = [
    "192.168.1.120:9200"
];
$client = Elasticsearch\ClientBuilder::create()
    ->setHosts($hosts)
    ->build();

// Define the search
$query = ([
    'index' => '*',
    'type' => '*',
    'body' => [
            'from' => 0,
        'size' => 40,
        'query' => [
            'query_string' => [
                    'query' => 'a'
            ]
        ]
    ]
]);
$raw = $client->search($query);

print_r($raw);

This print_r($raw); returns this:

Array ( [took] => 2 [timed_out] => [_shards] => Array ( [total] => 30 [successful] => 30 [failed] => 0 ) [hits] => Array ( [total] => 0 [max_score] => [hits] => Array ( ) ) )

I know that by doing print_r, it should show all arrays inside eachother. So I don't understand what I'm doing wrong here? I know it should be displaying because I did the same thing in program called POSTMAN, and got back 40 results. I also tried another PHP version. PHP7, but it returned the same thing.

What am I doing wrong here?

  • 写回答

1条回答 默认 最新

  • duangan6133 2017-05-10 22:04
    关注

    It seems like it didn't like that I put * as index and type. So to either remove them, or change them to a working index,type will fix the problem. For anyone else having this problem, this is the basic fix:

    $query = [
        'body' => [
            'query' => [
                'match' => [
                    'title' => 'Your search'
                ]
            ]
        ]
    ];
    

    or

    $query = [
        'index' => 'your valid index',
        'type' => 'your valid type',
        'body' => [
            'query' => [
                'match' => [
                    'title' => 'Your search'
                ]
            ]
        ]
    ];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch