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 有没有整苹果智能分拣线上图像数据
  • ¥20 有没有人会这个东西的
  • ¥15 cfx考虑调整“enforce system memory limit”参数的设置
  • ¥30 航迹分离,航迹增强,误差分析
  • ¥15 Chrome Manifest扩展引用Ajax-hook库拦截请求失败
  • ¥15 用Ros中的Topic通讯方式控制小乌龟的速度,走矩形;编写订阅器代码
  • ¥15 LLM accuracy检测
  • ¥15 pycharm添加远程解释器报错
  • ¥15 如何让子窗口鼠标滚动独立,不要传递消息给主窗口
  • ¥15 如何能达到用ping0.cc检测成这样?如图