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

Matrix blocks not excludable from search #4291

Closed
peteralewis opened this issue May 21, 2019 · 2 comments
Closed

Matrix blocks not excludable from search #4291

peteralewis opened this issue May 21, 2019 · 2 comments

Comments

@peteralewis
Copy link

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

  1. 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.
  2. Create a search form for this section.
  3. Create 2 entries with differing keywords in the title and relevant content text and linking each entry to the other.
  4. Search on a keyword within the title of one of the entries, both entries will be presented in the results.

Solution (partial)

Within Matrix.php getSearchKeywords 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: -
@brandonkelly
Copy link
Member

We just released Craft 3.1.28 with that setting for Matrix sub-fields.

@peteralewis
Copy link
Author

You're awesome - thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants