我正在尝试使用 PHP 和 AJAX 制作一个网络应用程序。它应该从 Elasticsearch 检索数据而不刷新页面(实时搜索)。是否可以在elasticsearch索引上实现这样的检索? 为此,我制作了这两个文件: 索引2.php
<?php
session_start();
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ajax-Trials</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div class="container">
<br />
<h2 align="center">Ajax Live Data Search using Jquery, PHP, Elasticsearch</h2><br />
<div class="form-group">
<div class="input-group">
<span class="input-group-addon">Search</span>
<input type="text" name="search_text" id="search_text" placeholder="Search by Customer Details" class="form-control" />
</div>
</div>
<br />
<div id="result"></div>
</div>
</body>
</html>
<script>
$(document).ready(function(){
load_data();
function load_data(query)
{
$.ajax({
url:"fetch2.php",
method:"POST",
data:{query:query},
success:function(data)
{
$('#result').html(data);
}
});
}
$('#search_text').keyup(function(){
var search = $(this).val();
if(search != '')
{
load_data(search);
}
else
{
load_data();
}
});
});
</script>
这是 fetch2.php
<?php
session_start();
require_once '../vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$client = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
$output = '';
if(isset($_POST["query"]))
{
$search = $_POST["query"];
$query = $client->search([
'body' => [
'query' => [
'bool' => [
'should' => [
'multi_match' => [
"fuzziness" => "AUTO",
'fields' => ['district','countrycode','name','population'],
'query' => $search
]
]
]
],
'from' => 0,
'size' => 100
]
]);
if($query['hits']['total']>=1){
$results = $query['hits']['hits'];
}
}
if(isset($results))
{
$output .= '
<div class="table-responsive">
<table class="table table bordered">
<tr>
<th>ID</th>
<th>Name</th>
<th>CountryCode</th>
<th>District</th>
<th>Population</th>
</tr>
';
foreach($results as $r)
{
$output .= '
<tr>
<td>'.$r['_id'].'</td>
<td>'.$r['_source']['name'].'</td>
<td>'.$r['_source']['countrycode'].'</td>
<td>'.$r['_source']['district'].'</td>
<td>'.$r['_source']['population'].'</td>
</tr>
';
}
echo $output;
}
else
{
echo 'Data Not Found';
}
?>
当我搜索任何字符串时,它成功打印表的标题,但不打印数据。这里有什么问题呢? 索引的JSON格式是这样的:
{
"_index": "cities",
"_type": "city",
"_id": "35",
"_version": 1,
"_score": 0,
"_source": {
"id": 35,
"@timestamp": "2020-03-10T18:24:46.963Z",
"@version": "1",
"population": 2168000,
"district": "Alger",
"countrycode": "DZA",
"name": "Alger"
},
"fields": {
"@timestamp": [
"2020-03-10T18:24:46.963Z"
]
}
}
您的 JSON 格式显示返回的数据是一个对象。 要访问对象值,请编写以下代码:
foreach($results as $r)
{
$output .= '
<tr>
<td>'.$r->_id.'</td>
<td>'.$r->_source->name.'</td>
<td>'.$r->_source->countrycode.'</td>
<td>'.$r->_source->district.'</td>
<td>'.$r->_source->population.'</td>
</tr>
';
}
如你所见,我使用了 -> (对象)而不是 [] (数组)
我刚刚从搜索字段中删除了“人口”字段,它就起作用了。 Elasticsearch 无法从数字字段中搜索数字输入。