Skip to content

Commit 402ebf4

Browse files
committed
feat(custom-pages): corrected indexing (#3015)
1 parent 2567054 commit 402ebf4

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

phpmyfaq/src/phpMyFAQ/Instance/Search/Elasticsearch.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ public function index(array $faq): ?object
224224
'answer' => strip_tags($faq['answer']),
225225
'keywords' => $faq['keywords'],
226226
'category_id' => $faq['category_id'],
227+
'content_type' => 'faq',
227228
],
228229
];
229230

@@ -265,6 +266,7 @@ public function bulkIndex(array $faqs): array
265266
'answer' => strip_tags((string) $faq['content']),
266267
'keywords' => $faq['keywords'],
267268
'category_id' => $faq['category_id'],
269+
'content_type' => 'faq',
268270
];
269271

270272
if (($i % 1000) === 0) {
@@ -314,6 +316,7 @@ public function update(array $faq): array
314316
'answer' => strip_tags($faq['answer']),
315317
'keywords' => $faq['keywords'],
316318
'category_id' => $faq['category_id'],
319+
'content_type' => 'faq',
317320
],
318321
],
319322
];
@@ -364,6 +367,13 @@ public function isAvailable(): bool
364367
*/
365368
public function indexCustomPage(array $page): ?object
366369
{
370+
// Only index active pages
371+
if (isset($page['active']) && $page['active'] === 'n') {
372+
// Delete from index if it exists (in case it was previously active)
373+
$this->deleteCustomPage((int) $page['id'], $page['lang']);
374+
return null;
375+
}
376+
367377
$params = [
368378
'index' => $this->elasticsearchConfiguration->getIndex(),
369379
'id' => 'page_' . $page['id'] . '_' . $page['lang'],
@@ -395,6 +405,11 @@ public function indexCustomPage(array $page): ?object
395405
*/
396406
public function updateCustomPage(array $page): array
397407
{
408+
// Only index active pages - delete from index if inactive
409+
if (isset($page['active']) && $page['active'] === 'n') {
410+
return $this->deleteCustomPage((int) $page['id'], $page['lang']);
411+
}
412+
398413
$params = [
399414
'index' => $this->elasticsearchConfiguration->getIndex(),
400415
'id' => 'page_' . $page['id'] . '_' . $page['lang'],
@@ -415,6 +430,12 @@ public function updateCustomPage(array $page): array
415430
try {
416431
return $this->client->update($params)->asArray();
417432
} catch (ClientResponseException|MissingParameterException|ServerResponseException $e) {
433+
// If document doesn't exist, try to create it
434+
if (str_contains($e->getMessage(), 'document_missing_exception')) {
435+
$result = $this->indexCustomPage($page);
436+
return $result ? ['success' => true] : ['error' => 'Failed to create document'];
437+
}
438+
$this->configuration->getLogger()->error('Update custom page error.', [$e->getMessage()]);
418439
return ['error' => $e->getMessage()];
419440
}
420441
}

phpmyfaq/src/phpMyFAQ/Instance/Search/OpenSearch.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ public function index(array $faq): array
202202
'answer' => strip_tags($faq['answer']),
203203
'keywords' => $faq['keywords'],
204204
'category_id' => $faq['category_id'],
205+
'content_type' => 'faq',
205206
],
206207
];
207208

@@ -238,6 +239,7 @@ public function bulkIndex(array $faqs): array
238239
'answer' => strip_tags((string) $faq['content']),
239240
'keywords' => $faq['keywords'],
240241
'category_id' => $faq['category_id'],
242+
'content_type' => 'faq',
241243
];
242244

243245
if (($i % 1000) === 0) {
@@ -279,6 +281,7 @@ public function update(array $faq): array
279281
'answer' => strip_tags($faq['answer']),
280282
'keywords' => $faq['keywords'],
281283
'category_id' => $faq['category_id'],
284+
'content_type' => 'faq',
282285
],
283286
],
284287
];
@@ -321,6 +324,12 @@ public function isAvailable(): bool
321324
*/
322325
public function indexCustomPage(array $page): array
323326
{
327+
// Only index active pages
328+
if (isset($page['active']) && $page['active'] === 'n') {
329+
// Delete from index if it exists (in case it was previously active)
330+
return $this->deleteCustomPage((int) $page['id'], $page['lang']);
331+
}
332+
324333
$params = [
325334
'index' => $this->openSearchConfiguration->getIndex(),
326335
'id' => 'page_' . $page['id'] . '_' . $page['lang'],
@@ -352,6 +361,11 @@ public function indexCustomPage(array $page): array
352361
*/
353362
public function updateCustomPage(array $page): array
354363
{
364+
// Only index active pages - delete from index if inactive
365+
if (isset($page['active']) && $page['active'] === 'n') {
366+
return $this->deleteCustomPage((int) $page['id'], $page['lang']);
367+
}
368+
355369
$params = [
356370
'index' => $this->openSearchConfiguration->getIndex(),
357371
'id' => 'page_' . $page['id'] . '_' . $page['lang'],
@@ -372,6 +386,11 @@ public function updateCustomPage(array $page): array
372386
try {
373387
return $this->client->update($params);
374388
} catch (Exception $e) {
389+
// If document doesn't exist, try to create it
390+
if (str_contains($e->getMessage(), 'document_missing_exception')) {
391+
return $this->indexCustomPage($page);
392+
}
393+
$this->configuration->getLogger()->error('Update custom page error.', [$e->getMessage()]);
375394
return ['error' => $e->getMessage()];
376395
}
377396
}

phpmyfaq/src/phpMyFAQ/Search/Search/Elasticsearch.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ public function search(string $searchTerm): array
8888
// FAQs: must match category filter
8989
[
9090
'bool' => [
91-
'must_not' => [
92-
'term' => ['content_type' => 'page'],
91+
'must' => [
92+
'term' => ['content_type' => 'faq'],
9393
],
9494
'filter' => [
9595
'terms' => ['category_id' => $this->getCategoryIds()],

phpmyfaq/src/phpMyFAQ/Search/Search/OpenSearch.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ public function search(string $searchTerm): array
8686
// FAQs: must match category filter
8787
[
8888
'bool' => [
89-
'must_not' => [
90-
'term' => ['content_type' => 'page'],
89+
'must' => [
90+
'term' => ['content_type' => 'faq'],
9191
],
9292
'filter' => [
9393
'terms' => ['category_id' => $this->getCategoryIds()],

0 commit comments

Comments
 (0)