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

DataObjects can declare their own indexing behaviour #6

Open
3 of 4 tasks
unclecheese opened this issue Apr 30, 2020 · 5 comments
Open
3 of 4 tasks

DataObjects can declare their own indexing behaviour #6

unclecheese opened this issue Apr 30, 2020 · 5 comments

Comments

@unclecheese
Copy link

unclecheese commented Apr 30, 2020

As a Silverstripe CMS developer, I want to leverage the power of the ORM to create a highly inclusive set of index fields on my dataobjects so that searches return more accurate results.

Acceptance Criteria

  • Can whitelist both native and relationship fields for indexing
  • Can list getters (computed fields) for indexing
  • Include all fields on subclasses Note: Decided not to do this to avoid inferring field names
  • Individual records can opt out of indexing (e.g. ShowInSearch)
@unclecheese unclecheese changed the title DataObject can declare indexed fields DataObjects can declare their own indexing behaviour Apr 30, 2020
@chillu
Copy link
Member

chillu commented May 8, 2020

Include all fields on subclasses

I think we should allow for selecting fields on subclasses, but not a * rule where it includes everything - because of #18.

Page:
  fields:
    Content
    MyCustomFieldOnSubclass

@dhensby
Copy link
Contributor

dhensby commented May 17, 2020

It would be nice if you could index any object regardless of if it is from the ORM, for example by making them comply with an interface or trait.

The default behaviour could be supplied as a trait that is easily added to DataObjects.

Should be fairly simple by implementing a SearchServiceIndexable interface and a DataObjectIndexable trait (naming could be anything)

@unclecheese
Copy link
Author

@unclecheese
Copy link
Author

Removing "index all fields on subclasses" as Ingo and I have discussed the potential security hazards of implicit exposure of fields.

@chillu
Copy link
Member

chillu commented Jun 12, 2020

Can whitelist both native and relationship fields for indexing

This doesn't work with subclasses at the moment:

SilverStripe\SearchService\Service\IndexConfiguration:
  indexes:
    my_index:
      includeClasses:
        Page:
          fields:
            title:
              property: Title
            relation_only_on_subclass:
              property: MyRelation.Title

DataObject->relObject() throws when it can't find the relationship. I'm not keen to create subclass-specific index definitions, since we should use this layer to communicate the "flattening" which happens in App Search (doesn't know about subclasses). We could catch the LogicException but that's messy. As a workaround, we could check if the first segment of the dot notation name exists on the object?

diff --git a/src/DataObject/DataObjectDocument.php b/src/DataObject/DataObjectDocument.php
index 38090d8..e3dd790 100644
--- a/src/DataObject/DataObjectDocument.php
+++ b/src/DataObject/DataObjectDocument.php
@@ -343,8 +343,15 @@ class DataObjectDocument implements
      */
     public function toDBField(Field $field)
     {
+        $obj = $this->getDataObject();
         if ($field->getProperty()) {
-            return $this->getDataObject()->relObject($field->getProperty());
+            $firstField = explode('.', $field->getProperty())[0];
+            if ($obj->hasField($firstField) || $obj->hasMethod($firstField) || $obj->getRelationClass($firstField)) {
+                return $obj->relObject($field->getProperty());
+            } else {
+                // Guard against missing fields which only exist in some subclasses
+                return null;
+            }
         }
 
         return $this->resolveField($field->getSearchFieldName());

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

3 participants