Skip to content

Commit 8232b62

Browse files
committed
Allow using multisearch
1 parent ceebe93 commit 8232b62

2 files changed

Lines changed: 33 additions & 3 deletions

File tree

config/statamic-typesense.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22

33
return [
44

5-
// if you make this higher it will use more memory
6-
// but be quicker to update large numbers of documents
5+
// If you make this higher it will use more memory
6+
// but be quicker to update large numbers of documents.
77
'insert_chunk_size' => 100,
88

9+
// Use multiSearch via POST for search requests.
10+
// Typesense limits GET request parameter size for security
11+
// reasons, so if you have many or long search parameters,
12+
// you should enable this.
13+
'use_multi_search' => env('STATAMIC_TYPESENSE_MULTISEARCH', false)
14+
915
];

src/Typesense/Index.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ public function searchUsingApi($query, array $options = []): array
111111
->join(',') ?: '*';
112112
}
113113

114-
$searchResults = $this->getOrCreateIndex()->documents->search($options);
114+
$searchResults = config('statamic-typesense.use_multi_search', false)
115+
? $this->multiSearch($options)
116+
: $this->regularSearch($options);
115117

116118
$total = count($searchResults['hits']);
117119

@@ -182,4 +184,26 @@ public function client()
182184
{
183185
return $this->client;
184186
}
187+
188+
private function regularSearch(array $options)
189+
{
190+
return $this->getOrCreateIndex()->documents->search($options);
191+
}
192+
193+
private function multiSearch(array $options)
194+
{
195+
$this->getOrCreateIndex();
196+
197+
$searchRequest = [
198+
'searches' => [
199+
array_merge($options, [
200+
'collection' => $this->name
201+
])
202+
],
203+
];
204+
205+
$multiSearchResults = $this->client->multiSearch->perform($searchRequest, []);
206+
207+
return array_shift($multiSearchResults['results']);
208+
}
185209
}

0 commit comments

Comments
 (0)