-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypesense.php
139 lines (122 loc) · 2.83 KB
/
typesense.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
include 'vendor/autoload.php';
use Symfony\Component\HttpClient\HttplugClient;
use Typesense\Client;
class Typesense extends CI_Controller {
function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('typesense/list');
}
public function sync()
{
try {
$client = new Client(
[
'api_key' => 'XXXXXXXXXXXXXXXXXXX',
'nodes' => [
[
'host' => 'XXX.XXX.XXX.XXX',
'port' => '8108',
'protocol' => 'http',
],
],
'client' => new HttplugClient(),
]
);
} catch (Exception $e) {
echo $e->getMessage();
}
$client->collections['parts']->delete(); //first delete all parts
try {
$client->collections->create(
[
'name' => 'parts',
'fields' => [
[
'name' => 'group_id',
'type' => 'int32',
'facet' => true,
],
[
'name' => 'group_name',
'type' => 'string',
],
[
'name' => 'part_id',
'type' => 'int32',
],
[
'name' => 'part_name',
'type' => 'string',
],
[
'name' => 'is_active',
'type' => 'bool',
]
],
'default_sorting_field' => 'group_id',
]
);
$parts = $this->db->query("get parts query")->result();
foreach($parts as $part)
{
$client->collections['parts']->documents->upsert(
[
'group_id' => (int)$part->group_id,
'group_name' => $part->group_name,
'part_id' => (int)$part->part_id,
'part_name' => $part->part_name,
'is_active' => $part->is_active == 't' ? true : false,
]
);
}
} catch (Exception $e) {
echo $e->getMessage();
// Don't error out if the collection was not found
}
}
public function get()
{
$term = $this->input->get('term');
try {
$client = new Client(
[
'api_key' => 'XXXXXXXXXXXXXXXXXXX',
'nodes' => [
[
'host' => 'XXX.XXX.XXX.XXX',
'port' => '8108',
'protocol' => 'http',
],
],
'client' => new HttplugClient(),
]
);
} catch (Exception $e) {
echo $e->getMessage();
}
try {
header('Content-Type: application/json; charset=utf-8');
$response = $client->collections['parts']->documents->search(
[
'q' => $term,
'query_by' => 'part_name',
//'sort_by' => '_text_match:desc',
'group_by' => 'group_id',
'per_page' => 250,
'group_limit' => 99,
//'num_typos' => 0 // 0 hata, 1 hata, 2 hata
]
);
echo json_encode($response);
} catch (Exception $e) {
echo $e->getMessage();
// Don't error out if the collection was not found
}
}
}
?>