Searching non-field data with Low Search
One of Low Search‘s strengths is its use of an index that consists of channel fields you define. It allows for efficient, fast and customisable keyword searches. However, a channel entry also has non-field data, like categories. This data cannot be indexed by Low Search directly, but with a bit of creative thinking and Solspace’s hidden (and free) gem Preparse, we can make it work.
Say, you’ve got a channel called “Services” and you want its categories to be searchable. First, create a new field for that channel, and name it something like “Service Categories”. Make sure the field is searchable in its settings.
Then create a template, something like services/category_data
. In that template, put the code to generate the searchable data. If you just want the category name to be added to the index, then simply use something like this:
{exp:channel:entries entry_id="{preparse_entry_id}"}
{categories}
{category_name}
{/categories}
{/exp:channel:entries}
But you can use anything you like, including custom category fields.
Finally, tie the whole thing together. Go to the Preparse extension settings, select the Services channel, and select any custom field below it (any but the Service Categories field). Then, for the Outgoing Field, select Service Categories and select services/category_data
as the Template. Save and you’re done!
Now, each time an entry is saved, the parsed template that you created will be put in the Service Categories field, which in turn will be added to the index, making categories searchable. You can create any amount of fields like this, depending on how specific you want them to be. And it’s not just limited to categories: you can also use the same principle for Author data. Sweet.
Comments
Dustin Hansen 8 May 2013 at 23:18
This is great… but any tips on how we can then batch/bulk save our existing entries so that everything gets updated/indexed? We added this to a site where we have 1000+ entries.
Lodewijk Schutte 9 May 2013 at 14:38
There isn’t a ready-made solution for that, I’m afraid. You’d have to write a script or add-on for it.
Guy Roberts 16 May 2013 at 17:13
Can Low Search search member fields (or member custom fields)?
Lodewijk Schutte 17 May 2013 at 12:02
Guy: you could, using the same approach as explained in the blog post. But be aware that Low Search only returns entries, not members. It might be worth taking a look at Zoo Visitor, which extends the members using channel entries, making members play nice with Low Search.