-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
63 lines (54 loc) · 1.61 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
declare(strict_types=1);
require_once 'vendor/autoload.php';
use Elastic\Elasticsearch\ClientBuilder;
$client = ClientBuilder::create()
->setHosts(['elasticsearch:9200'])
->build();
if (!isset($_POST['phrase'])) {
header('Content-type: application/json');
echo json_encode([]);
exit;
}
$searchPhrase = $_POST['phrase'];
$searchParams = [
'index' => 'airports_php',
'body' => [
'query' => [
'bool' => [
'should' => [
[
'term' => [
'code' => strtoupper($searchPhrase)
],
],
[
'match_phrase_prefix' => [
'name' => $searchPhrase
],
],
[
'match_phrase_prefix' => [
'city' => $searchPhrase
],
],
[
'match_phrase_prefix' => [
'country' => $searchPhrase
],
],
[
'multi_match' => [
'fields' => ['name', 'city', 'country'],
'query' => $searchPhrase,
'fuzziness' => 1
]
]
]
]
]
]
];
$response = $client->search($searchParams);
header('Content-type: application/json');
echo json_encode($response->asArray());