Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support search indexing and searching for multi-instance fields #13991

Open
wants to merge 4 commits into
base: 5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
'id' => 'CraftCMS',
'name' => 'Craft CMS',
'version' => '5.0.0-alpha.3',
'schemaVersion' => '5.0.0.12',
'schemaVersion' => '5.0.0.13',
'minVersionRequired' => '4.4.0',
'basePath' => dirname(__DIR__), // Defines the @app alias
'runtimePath' => '@storage/runtime', // Defines the @runtime alias
Expand Down
6 changes: 4 additions & 2 deletions src/migrations/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -925,9 +925,10 @@ public function createIndexes(): void
'elementId' => $this->integer()->notNull(),
'attribute' => $this->string(25)->notNull(),
'fieldId' => $this->integer()->notNull(),
'layoutElementUid' => $this->uid(),
'siteId' => $this->integer()->notNull(),
'keywords' => $this->text()->notNull(),
'PRIMARY KEY([[elementId]], [[attribute]], [[fieldId]], [[siteId]])',
'PRIMARY KEY([[elementId]], [[attribute]], [[fieldId]], [[layoutElementUid]], [[siteId]])',
]);

$sql = 'CREATE FULLTEXT INDEX ' .
Expand All @@ -946,10 +947,11 @@ public function createIndexes(): void
'elementId' => $this->integer()->notNull(),
'attribute' => $this->string(25)->notNull(),
'fieldId' => $this->integer()->notNull(),
'layoutElementUid' => $this->uid(),
'siteId' => $this->integer()->notNull(),
'keywords' => $this->text()->notNull(),
'keywords_vector' => $this->db->getSchema()->createColumnSchemaBuilder('tsvector')->notNull(),
'PRIMARY KEY([[elementId]], [[attribute]], [[fieldId]], [[siteId]])',
'PRIMARY KEY([[elementId]], [[attribute]], [[fieldId]], [[layoutElementUid]], [[siteId]])',
]);

$sql = 'CREATE INDEX ' . $this->db->quoteTableName($this->db->getIndexName()) . ' ON ' . Table::SEARCHINDEX . ' USING GIN([[keywords_vector]] [[pg_catalog]].[[tsvector_ops]]) WITH (FASTUPDATE=YES)';
Expand Down
41 changes: 41 additions & 0 deletions src/migrations/m231205_081215_searchindex_amend_pk.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace craft\migrations;

use craft\db\Migration;
use craft\db\Table;

/**
* m231205_081215_searchindex_amend_pk migration.
*/
class m231205_081215_searchindex_amend_pk extends Migration
{
/**
* @inheritdoc
*/
public function safeUp(): bool
{
if ($this->db->getIsMysql()) {
$this->dropPrimaryKey('elementId', Table::SEARCHINDEX);
$this->addColumn(Table::SEARCHINDEX, 'layoutElementUid', $this->uid()->after('fieldId'));
$this->addPrimaryKey('layoutUid', Table::SEARCHINDEX, ['elementId', 'attribute', 'fieldId', 'layoutElementUid', 'siteId']);
} else {
$pkName = $this->db->getSchema()->getRawTableName(Table::SEARCHINDEX) . '_pkey';

$this->dropPrimaryKey($pkName, Table::SEARCHINDEX);
$this->addColumn(Table::SEARCHINDEX, 'layoutElementUid', $this->uid()->after('fieldId'));
$this->addPrimaryKey($pkName, Table::SEARCHINDEX, ['elementId', 'attribute', 'fieldId', 'layoutElementUid', 'siteId']);
}

return true;
}

/**
* @inheritdoc
*/
public function safeDown(): bool
{
echo "m231205_081215_searchindex_amend_pk cannot be reverted.\n";
return false;
}
}
54 changes: 40 additions & 14 deletions src/services/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ public function indexElementAttributes(ElementInterface $element, ?array $fieldH
// Figure out which fields to update, and which to ignore
/** @var FieldInterface[] $updateFields */
$updateFields = [];
/** @var string[] $ignoreFieldIds */
$ignoreFieldIds = [];
/** @var string[] $ignoreFields */
$ignoreFields = [];
$fieldLayout = $element->getFieldLayout();
if ($fieldLayout !== null) {
if ($fieldHandles !== null) {
Expand All @@ -162,7 +162,7 @@ public function indexElementAttributes(ElementInterface $element, ?array $fieldH
$updateFields[] = $field;
} else {
// Leave its existing keywords alone
$ignoreFieldIds[] = (string)$field->id;
$ignoreFields[] = $field->layoutElement->uid;
}
}
}
Expand All @@ -173,8 +173,8 @@ public function indexElementAttributes(ElementInterface $element, ?array $fieldH
'elementId' => $element->id,
'siteId' => $element->siteId,
];
if (!empty($ignoreFieldIds)) {
$deleteCondition = ['and', $deleteCondition, ['not', ['fieldId' => $ignoreFieldIds]]];
if (!empty($ignoreFields)) {
$deleteCondition = ['and', $deleteCondition, ['not', ['layoutElementUid' => $ignoreFields]]];
}
Db::delete(Table::SEARCHINDEX, $deleteCondition);

Expand All @@ -188,7 +188,7 @@ public function indexElementAttributes(ElementInterface $element, ?array $fieldH
foreach ($updateFields as $field) {
$fieldValue = $element->getFieldValue($field->handle);
$keywords = $field->getSearchKeywords($fieldValue, $element);
$this->_indexKeywords($element, $keywords, fieldId: $field->id);
$this->_indexKeywords($element, $keywords, fieldId: $field->id, layoutElementUid: $field->layoutElement->uid);
}

// Release the lock
Expand Down Expand Up @@ -406,8 +406,9 @@ public function deleteOrphanedIndexes(): void
* @param string $keywords
* @param string|null $attribute
* @param int|null $fieldId
* @param string|null $layoutElementUid
*/
private function _indexKeywords(ElementInterface $element, string $keywords, ?string $attribute = null, ?int $fieldId = null): void
private function _indexKeywords(ElementInterface $element, string $keywords, ?string $attribute = null, ?int $fieldId = null, ?string $layoutElementUid = null): void
{
if ($attribute !== null) {
$attribute = strtolower($attribute);
Expand All @@ -422,6 +423,7 @@ private function _indexKeywords(ElementInterface $element, string $keywords, ?st
'element' => $element,
'attribute' => $attribute,
'fieldId' => $fieldId,
'layoutElementUid' => $layoutElementUid,
'keywords' => $keywords,
]);
$this->trigger(self::EVENT_BEFORE_INDEX_KEYWORDS, $event);
Expand All @@ -438,6 +440,7 @@ private function _indexKeywords(ElementInterface $element, string $keywords, ?st
'elementId' => $element->id,
'attribute' => $attribute ?? 'field',
'fieldId' => $fieldId ? (string)$fieldId : '0',
'layoutElementUid' => $layoutElementUid ?? '0',
'siteId' => $site->id,
];

Expand Down Expand Up @@ -670,9 +673,12 @@ private function _getSqlFromTerm(SearchQueryTerm $term, array|int|null $siteId,
// Check for other attributes
if ($term->attribute !== null) {
// Is attribute a valid fieldId?
$fieldId = $this->_getFieldIdFromAttribute($term->attribute, $customFields);
[$fieldId, $layoutElementUid] = $this->_getFieldIdFromAttribute($term->attribute, $customFields);

if (!empty($fieldId)) {
if (!empty($layoutElementUid)) {
$attr = 'layoutElementUid';
$val = $layoutElementUid;
} elseif (!empty($fieldId)) {
$attr = 'fieldId';
$val = $fieldId;
} else {
Expand Down Expand Up @@ -801,16 +807,36 @@ private function _normalizeTerm(string $term, array|int|null $siteId = null): st
*
* @param string $attribute
* @param MemoizableArray<FieldInterface>|null $customFields
* @return int|int[]|null
* @return array
*/
private function _getFieldIdFromAttribute(string $attribute, ?MemoizableArray $customFields): array|int|null
private function _getFieldIdFromAttribute(string $attribute, ?MemoizableArray $customFields): array
{
if ($customFields !== null) {
return ArrayHelper::getColumn($customFields->where('handle', $attribute)->all(), 'id');
return [
ArrayHelper::getColumn($customFields->where('handle', $attribute)->all(), 'id'),
ArrayHelper::getColumn($customFields->where('handle', $attribute)->all(), 'layoutElement.uid'),
];
}

// we can no longer just get one field by handle; we need to find them all because we now support multiple instanced
$fields = [];
foreach (Craft::$app->getFields()->getAllLayouts() as $fieldLayout) {
array_push($fields, ...$fieldLayout->getCustomFields());
}

/** @var FieldInterface[][] $fieldsByHandle */
$fieldsByHandle = ArrayHelper::index($fields, null, [
fn(FieldInterface $field) => $field->handle,
]);

if (isset($fieldsByHandle[$attribute])) {
return [
ArrayHelper::getColumn($fieldsByHandle[$attribute], 'id'),
ArrayHelper::getColumn($fieldsByHandle[$attribute], 'layoutElement.uid'),
];
}

$field = Craft::$app->getFields()->getFieldByHandle($attribute);
return $field->id ?? null;
return [null, null];
}

/**
Expand Down