2

I am trying to extend the SearchableText index for my content type. I have succeeded in getting multiple fields to be included by marking them as indexer:searchable="true" in the model file. However I can't extend the SearchableText from my type's py as follows:

class IMyBehavior(form.Schema):

    dexteritytextindexer.searchable('description')
    description = schema.Text(title=u'Precis')

alsoProvides(IMyBehavior, IFormFieldProvider)


class MySearchableTextExtender(object):
    adapts(IMyBehavior)
    implements(dexteritytextindexer.IDynamicTextIndexExtender)

    def __init__(self, context):
        self.context = context

    def __call__(self):
        """Extend the searchable text with a custom string"""
        return 'some more searchable words'

I have to admit, I don't really know how the first class works. Do I have to set the searchable fields in this class to be able to extend the SearchableText in the second? If I remove all the indexer:searchable="true" from the model, then the SearchableText is just empty.

Is the first class trying to register the schema at the same time? If so what should this look like if it's just extending the SearchableText?

Adrian Garner
  • 5,235
  • 2
  • 23
  • 29

1 Answers1

3

The collective.dexteritytextindexer provides two important features:

  1. As you already achieved, dexteritytextindexer gives you the ability to put values into Plone's SearchableText index. By adding dexteritytextindexer.searchable(FIELDNAME) to your form, the value of the field will appear in the SearchableText. In Archetypes you have the same feature, by adding searchable=True to the field definition.

  2. collective.dexteritytextindexer gives you also the ability to extend the searchableText manually by registering an IDynamicTextIndexExtender adapter. It extends the values from part 1 with the values from your adapter.

I guess the Problem in your case is, that you have missed to register the adapter: https://github.com/collective/collective.dexteritytextindexer#extending-indexed-data

Example:

<adapter
    factory=".yourbehavior.MySearchableTextExtender"
    provides="collective.dexteritytextindexer.IDynamicTextIndexExtender"
    name="IMyBehavior"
    />

Here's a working example: This code extends the SearchableText of a container with the searchableText of it's children.

IDynamicTextIndexExtender adapter: https://github.com/4teamwork/ftw.simplelayout/blob/a7d631de3984b8c1747506b9411045fdf83bc908/ftw/simplelayout/indexer.py

Register the adapter with zcml: https://github.com/4teamwork/ftw.simplelayout/blob/a7d631de3984b8c1747506b9411045fdf83bc908/ftw/simplelayout/behaviors.zcml#L21

And the most important part - test the implementation: https://github.com/4teamwork/ftw.simplelayout/blob/a7d631de3984b8c1747506b9411045fdf83bc908/ftw/simplelayout/tests/test_indexer.py#L31

Mathias
  • 6,777
  • 2
  • 20
  • 32
  • Following your ftw example, I have added an interfaces.py, with an empty interface for IMyBehavior (?), but I am not sure what "import _" means, and I can't find any docs on it. – Adrian Garner Jul 04 '14 at 00:55
  • In the ftw.simplelayout exmaple the ISimplelayout interface is a marker behavior (See http://docs.plone.org/external/plone.app.dexterity/docs/behaviors/providing-marker-interfaces.html) "_" the underscore is a message factory. It's common in plone to declare this as underscore. Did you register your adapter with zcml? Does it actualy go into the adapter (pdb?)? – Mathias Jul 04 '14 at 06:01
  • Why did you add a interfaces.py, you already have a `IMyBehavior`?? please post your full code, if it doesn't work. – Mathias Jul 04 '14 at 22:29
  • It does work now thanks. The last missing piece of the puzzle was to enable the behavior ttw – Adrian Garner Jul 07 '14 at 03:44
  • Is it possible to have the behavior enabled by default? – Adrian Garner Jul 07 '14 at 04:20
  • If the marker interface should be always active, you could simply provided it by your DX content `implements(INTERFACE)`, this way you don't need a bahvior to enable it. OR you can add it to your generic setup `mytype.xml` – Mathias Jul 07 '14 at 06:29
  • Do you know of an example or document that shows how to add this to generic setup mytype.xml (which I presume is .../profiles/types/mytype.xml)? – Adrian Garner Jul 07 '14 at 21:01
  • Yes it could look like this https://github.com/4teamwork/ftw.simplelayout/blob/master/ftw/simplelayout/profiles/default/types/ftw.simplelayout.ContentPage.xml#L30 `` – Mathias Jul 08 '14 at 06:09
  • The getFolderContents line appears to return a list with length 0, even though there are definitely child files. https://github.com/adrigen/hccrems.types/blob/indexing/hccrems/types/indexer.py I can't find any info on getFolderContents usage. (even greping the source) – Adrian Garner Jul 30 '14 at 04:24
  • To ignore the catalog. Try `print self.context.objectValues()` to make sure the objects are really there. – Mathias Jul 30 '14 at 07:43
  • They seem to be there, it lists something for each file e.g. The loop doesn't seem to run though, and doesn't throw errors. – Adrian Garner Jul 30 '14 at 13:50
  • This means your content (files) are not in the catalog. How did you create those files? manually? script? perhaps you miss to do a `catalog_object` somewhere. – Mathias Jul 30 '14 at 14:01
  • They appear to have a catalog entry when I search the path in the zmi... that is, I can see a page which lists the pdf's SearchableText (and it's a huge entry). By the way, to test my changes to indexer.py I restart and "clear and rebuild". – Adrian Garner Jul 30 '14 at 14:15
  • Those files were created through the frontend of the site. – Adrian Garner Jul 30 '14 at 14:22
  • OK, so it could be a workflow issue (security configuration). If you query the catalog thru `getFolderContents` the catalog only returns item with `View` permission for the current user. `objectValues` does no respect security. Go to `...licy-development.pdf/manage_access` (./manage_access on the file) and enter the username, check if the user has the View permission. – Mathias Jul 30 '14 at 15:01
  • Mmm... the current user is admin. View is set to "acquire" on the pdf and all it's parents up to the root and Site Admin has the "view" permission for that. – Adrian Garner Jul 30 '14 at 15:10
  • Could it be possible that the method I am using to test this is doing things in the wrong order? I.e. am clearing and rebuilding the catalog, so maybe there is no catalog entry for the files at the point at which the parent type is being indexed... – Adrian Garner Feb 19 '15 at 02:49
  • Yeah that was a problem too. Thanks again ;) – Adrian Garner Feb 19 '15 at 03:52