【问题标题】:Elasticsearch Unable to print array in PHPElasticsearch无法在PHP中打印数组
【发布时间】:2017-10-08 08:40:51
【问题描述】:

我按照Video 作为指南来使用 PHP 启动和运行 elasticsearch,但我无法打印或查看结果。我可以打印原始数据,但无法更深入。

  • 操作系统:Debian (jessie)
  • PHP 版本:5.6.30
  • Java 版本:1.7.0
  • Elasticsearch 版本:5.4.0

我使用此代码请求数据:

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

这个 print_r($raw);返回:

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

我知道通过 print_r,它应该显示彼此内部的所有数组。所以我不明白我在这里做错了什么?我知道它应该显示,因为我在名为 POSTMAN 的程序中做了同样的事情,并得到了 40 个结果。我还尝试了另一个 PHP 版本。 PHP7,但它返回了同样的东西。

我在这里做错了什么?

【问题讨论】:

  • 因为 hits=>hits 是空的?没有任何深度可以探测吗?看起来好像您正在搜索“a”...您是否尝试过不太可能出现在停用词文件/db 中的较长术语?
  • 我尝试删除整个查询,query_string,query,它应该给我所有的结果,但仍然得到相同的结果
  • 为什么要删除呢?你的指数是怎样的?尝试搜索“without”、“long”、“cookie”、“house”或“donut”。不要尝试“a”、“an”、“the”……这些是大多数搜索系统中的停用词。删除查询不应导致您获得结果...除非您正在阅读文档并且它是这样说的...这似乎违反直觉。
  • 搜索“房子”输出相同的结果。是的,该数据库包含超过 600 000 个文档。但是,使用 POSTMAN 搜索“房子”时,我得到了 2436 个结果。
  • 我可以尝试用 curl 代替 composer,看看能不能再靠近一点。

标签: php arrays elasticsearch composer-php


【解决方案1】:

似乎不喜欢我将 * 作为索引和类型。因此,要删除它们,或者将它们更改为工作索引,type 将解决问题。 对于其他遇到此问题的人,这是基本修复:

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

$query = [
    'index' => 'your valid index',
    'type' => 'your valid type',
    'body' => [
        'query' => [
            'match' => [
                'title' => 'Your search'
            ]
        ]
    ]
];

【讨论】:

    猜你喜欢
    • 2017-09-04
    • 1970-01-01
    • 1970-01-01
    • 2017-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多