我在网络主机服务器上安装了一个 PHP 脚本。它具有搜索栏和按钮。在搜索栏中输入信息并单击按钮后,将向 Elasticsearch 发送查询,该查询应返回结果。
这在我使用 WAMP 的本地服务器设置上运行良好。不过,这里的一个区别是 Elasticsearch 本身运行在另一台服务器(Debian 9)上。
脚本(已编辑外部 IP):
<?php
require_once 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$client = ClientBuilder::create()->setHosts(['externalIP:9200'])->build();
$es = $client;
echo "<div class='search'>
<form action='index.php' method='get' autocomplete='off' class='search_form'>
<label>
<input type='text' name='q' placeholder='Søk her'>
</label>
<label><input type='submit' value='Søk' name='s'></label>
</form>
</div>";
if (isset($_GET['q'])) {
$q = $_GET['q'];
$query = $es->search([
'index' => 'aksjeregisteret2017',
'size' => 20,
'body' => [
'query' => [
'bool' => [
'must' => [
'multi_match' => [
'query' => $q,
'fields' => ['message', 'Navn Aksjoner', 'Orgnr', 'Selskap',
'Aksjeklasse', 'Navn Aksjoner', 'Fodselar/Orgnr', 'Postnr',
'Antall Aksjer', 'Antall Aksjer Selskap'],
"minimum_should_match" => "50%"
]
]
]
]
]
]);
}
if($query['hits']['total'] >=1 ) {
$results = $query['hits']['hits'];
}
//var_dump($results);
?>
<!doctype html>
<html>
<head>
<title>Søk i Aksjoneregisteret</title>
<link rel="stylesheet" href="css/main.css">
</head>
<body>
<?php if (isset($_GET['q'])) { ?>
<table>
<tr>
<th>Orgnr</th>
<th>Selskap</th>
<th>Aksjeklasse</th>
<th>Navn Aksjoner</th>
<th>Fodselar/Orgnr</th>
<th>Postnr</th>
<th>Poststed</th>
<th>Antall Aksjer</th>
<th>Antall Aksjer Selskap</th>
</tr>
<?php foreach ($results as $r) { ?>
<tr>
<td><?php echo $r['_source']['Orgnr'] ?></td>
<td><?php echo $r['_source']['Selskap'] ?></td>
<td><?php echo $r['_source']['Aksjeklasse'] ?></td>
<td><?php echo $r['_source']['Navn Aksjoner'] ?></td>
<td><?php echo $r['_source']['Fodselar/Orgnr'] ?></td>
<td><?php echo $r['_source']['Postnr'] ?></td>
<td><?php echo $r['_source']['Poststed'] ?></td>
<td><?php echo $r['_source']['Antall Aksjer'] ?></td>
<td><?php echo $r['_source']['Antall Aksjer Selskap'] ?></td>
</tr>
<?php } ?>
</table>
<?php } else { echo "no result"; } ?>
</body>
</html>
问题是它没有返回带有数据的表,它什么也不返回。加载一段时间后只是一个空白页面。没有错误。
可能出错的一件事是我们如何安装 PHP 脚本。我们无法在我们的 Web 主机上使用终端,因此我们使用 composer 在 linux 服务器(安装了 Elasticsearch 的那个)上创建了 elasticsearch PHP vendor 文件夹,然后将它与脚本一起直接移动到我们的 Web 主机上的文件夹中。这应该有效还是这样做有任何问题?还有另一种方法吗?
通过在浏览器中使用外部 IP + 端口(9200),我们可以看到这个页面:
如果我没有错,那应该意味着与 Elasticsearch 的连接是打开的?那么在脚本中使用相同的 externalIP + Port 应该能让我们得到一个结果吗?
关于我做错了什么的任何想法?
我们发现上述内容在 Web 主机服务器上不起作用,因为该服务器缺少 Elasticsearch PHP 所需的某些功能。
我们将脚本移到了 VPS 并在那里进行了设置,几乎没有修改,并且它工作正常。
如果您打算在无法让您安装新库的 Web 主机服务器上使用 Elasticsearch 脚本,请确保它们具有首先运行它的要求。Elasticsearch PHP 的要求可以在这个页面上找到:https : //www.elastic.co/guide/en/elasticsearch/client/php-api/current/_installation_2.html
他们是:
PHP 7.0.0 or higher
Composer
ext-curl: the Libcurl extension for PHP (see note below)
Native JSON Extensions (ext-json) 1.3.7 or higher
Web 主机服务器没有 Libcurl 扩展名,因此脚本无法运行。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句