You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Search is only preventable/allowable for an entire Matrix field, including all it's blocks. The individual blocks/fields within a Matrix don't have the fine control of being included or not included in search. An issue occurs when entries fields are within the Matrix, therefore polluting search with data from linked entries instead of the individual entry.
Create a Matrix field with text field (content you want in search) and an entries field linked to entries within the same section (which you don't want included in the search results). Ensure Matrix field is searchable.
Create a search form for this section.
Create 2 entries with differing keywords in the title and relevant content text and linking each entry to the other.
Search on a keyword within the title of one of the entries, both entries will be presented in the results.
Solution (partial)
Within Matrix.phpgetSearchKeywords function, the foreach field loop needs a conditional to check that the field is searchable:
foreach (Craft::$app->getFields()->getAllFields() as $field) {
/** @var Field $field */
if ($field->searchable) {
$fieldValue = $block->getFieldValue($field->handle);
$keywords[] = $field->getSearchKeywords($fieldValue, $element);
}
}
Along with the above conditional, when manually editing the DB fields table and setting the relevant searchable column for the Matrix block (sub-field) to false (0) resolves the issue (allows for individual fields within the MAtrix to not be used in search). Obviously reindexing search is required (or saving test entries).
Ideally the CMS UI would be updated to include a checkbox for each field within the matrix allowing for finer control over which fields within the Matrix are searchable, as the underlying DB and code does support this functionality (with the above tweak).
Additional info
Craft version: 3.1.22
PHP version: 7.2
Database driver & version: -
Plugins & versions: -
The text was updated successfully, but these errors were encountered:
Description
Search is only preventable/allowable for an entire Matrix field, including all it's blocks. The individual blocks/fields within a Matrix don't have the fine control of being included or not included in search. An issue occurs when entries fields are within the Matrix, therefore polluting search with data from linked entries instead of the individual entry.
Full description and scenario here: https://craftcms.stackexchange.com/questions/30466/how-to-exclude-a-matrix-block-from-search
Steps to reproduce
Solution (partial)
Within Matrix.php
getSearchKeywords
function, the foreach field loop needs a conditional to check that the field is searchable:Along with the above conditional, when manually editing the DB
fields
table and setting the relevantsearchable
column for the Matrix block (sub-field) to false (0) resolves the issue (allows for individual fields within the MAtrix to not be used in search). Obviously reindexing search is required (or saving test entries).Ideally the CMS UI would be updated to include a checkbox for each field within the matrix allowing for finer control over which fields within the Matrix are searchable, as the underlying DB and code does support this functionality (with the above tweak).
Additional info
The text was updated successfully, but these errors were encountered: