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 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100