我正在尝试使用PHP客户端在索引之前检查用户的输入是否已存在。
我已阅读过一些类似的帖子(例如Check Elasticsearch document similarity before indexing或Check if the index exists or not Elasticsearch),但它们并没有涵盖如何实现这一目标。
我尝试使用$client->search
和$client->index
,但我在$client->search
行上收到以下错误:
HP致命错误:未捕获Elasticsearch \ Common \ Exceptions \ ServerErrorResponseException:{“error”:{“root_cause”:[{“type”:“illegal_state_exception”,“reason”:“无法在START_ARRAY上获取文字1:129" }] ...
这是我尝试过的代码:
<?php
require_once 'app/init.php';
if(!empty($_POST)) {
//Determines if the variables are set
if (isset($_POST['title'], $_POST['description'], $_POST['thema'], $_POST['type'])) {
$title = $_POST['title'];
$description = $_POST['description'];
$thema = explode(',', $_POST['thema']);
$type = $_POST['type'];
$searched = $client->search([
'index' => 'my_index',
'type' => 'default',
'body' => [
'query' => [
'bool' => [
'should' => [
[ 'match_phrase' => [ 'Title' => $title ] ],
[ 'match_phrase' => [ 'Description' => $description ] ],
[ 'match_phrase' => [ 'Thema' => $thema ] ],
[ 'match_phrase' => [ 'Type' => $type ] ],
]
]
]
]
]);
if($searched['hits']['total'] >= 1) {
print_r("Your idea already exists in the database");
}
else {
$indexed = $client->index([
'index' => 'january-2018',
'type' => 'default',
'body' => [
'Titel' => $titel,
'Beschreibung' => $beschreibung,
'Thema' => $thema,
'Beitragstyp' => $beitragstyp
]
]);
print_r("Your idea has been successfully submitted! The ID for your submission is: " . $indexed['_id']);
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Add | ES</title>
<link rel="stylesheet" href="css/main.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<div class="text2">
<h1>Matching</h1>
<h2>Add your idea:</h2>
</div>
<div class="">
<form action="add.php" method="post" autocomplete="off">
<input type="text" class="addTerm" placeholder="Titel" name="title" required>
<select id="themas" name="thema" required>
<option value="thema1">thema1</option>
<option value="thema2">thema2</option>
</select>
<select id="themas" name="type" required>
<option value="type1">type1</option>
<option value="type2">type2</option>
</select>
<input type="submit" class="addButton" value="Add " required>
<textarea class="addBeschreibung" name="description" placeholder="Beschreibung" maxlength="2000" required></textarea>
<button type="button" class="homeButton" onclick="window.location.href='http://localhost/server/elasticsearch-php/'">
Home
<i class="fa fa-home"></i>
</button>
</form>
</div>
</body>
</html>
<?php