Skip to content

Commit

Permalink
Fixed #13987
Browse files Browse the repository at this point in the history
Still may want to do #13991 though
  • Loading branch information
brandonkelly committed Dec 27, 2023
1 parent 9de58d9 commit 4e6b9d2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Added `craft\events\SetEagerLoadedElementsEvent::$plan`.
- `craft\base\ElementInterface::setEagerLoadedElements()` now has a `$plan` argument, which will be set to the eager-loading plan.
- Fixed an error that could occur if eager-loading aliases conflicted with native eager-loading handles, such as `author`. ([#14057](https://github.com/craftcms/cms/issues/14057))
- Fixed an error that occurred when indexing search keywords for an element with multiple instances of the same custom field. ([#13987](https://github.com/craftcms/cms/issues/13987))

## 5.0.0-alpha.3 - 2023-12-21

Expand Down
35 changes: 22 additions & 13 deletions src/services/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,23 +146,22 @@ public function indexElementAttributes(ElementInterface $element, ?array $fieldH
}

// Figure out which fields to update, and which to ignore
/** @var FieldInterface[] $updateFields */
$updateFields = [];
/** @var string[] $ignoreFieldIds */
$customFields = $element->getFieldLayout()?->getCustomFields() ?? [];
$updateFieldIds = [];
$ignoreFieldIds = [];
$fieldLayout = $element->getFieldLayout();
if ($fieldLayout !== null) {

if (!empty($customFields)) {
if ($fieldHandles !== null) {
$fieldHandles = array_flip($fieldHandles);
}
foreach ($fieldLayout->getCustomFields() as $field) {
foreach ($customFields as $field) {
if ($field->searchable) {
// Are we updating this field's keywords?
if ($fieldHandles === null || isset($fieldHandles[$field->handle])) {
$updateFields[] = $field;
$updateFieldIds[$field->id] = true;
} else {
// Leave its existing keywords alone
$ignoreFieldIds[] = (string)$field->id;
$ignoreFieldIds[$field->id] = true;
}
}
}
Expand All @@ -174,7 +173,12 @@ public function indexElementAttributes(ElementInterface $element, ?array $fieldH
'siteId' => $element->siteId,
];
if (!empty($ignoreFieldIds)) {
$deleteCondition = ['and', $deleteCondition, ['not', ['fieldId' => $ignoreFieldIds]]];
$ignoreFieldIds = array_map(fn(int $fieldId) => (string)$fieldId, array_keys($ignoreFieldIds));
$deleteCondition = [
'and',
$deleteCondition,
['not', ['fieldId' => $ignoreFieldIds]],
];
}
Db::delete(Table::SEARCHINDEX, $deleteCondition);

Expand All @@ -185,10 +189,15 @@ public function indexElementAttributes(ElementInterface $element, ?array $fieldH
}

// Update the custom fields' keywords
foreach ($updateFields as $field) {
$fieldValue = $element->getFieldValue($field->handle);
$keywords = $field->getSearchKeywords($fieldValue, $element);
$this->_indexKeywords($element, $keywords, fieldId: $field->id);
$keywords = [];
foreach ($customFields as $field) {
if (isset($updateFieldIds[$field->id])) {
$fieldValue = $element->getFieldValue($field->handle);
$keywords[$field->id][] = $field->getSearchKeywords($fieldValue, $element);
}
}
foreach ($keywords as $fieldId => $instanceKeywords) {
$this->_indexKeywords($element, implode(' ', $instanceKeywords), fieldId: $fieldId);
}

// Release the lock
Expand Down

0 comments on commit 4e6b9d2

Please sign in to comment.