The Search API Saved Searches modules gives you the ability to save searches executed by Search API and be notified of new results via email.
Install module
Enable module
- Go to "Modules" (admin/modules) and enable:
- Search API saved searches
- (Save Configuration)
After installing and enabling the Search API Saved Searches module, we can configure saved searches in the node index.
Enable Saved Searches
- Go to "Configuration => Search and Metadata => Search API" (admin/config/search/search_api)
- Node index
- Click the dropdown
- Select "Saved searches"
- [x] Enable (This will enable saved searches for the node index.)
- Settings
- Miscellaneous
- Method for determining new results: Determine by Date created
- [ ] Let logged-in users also enter a different mail address (If checked, this would allow logged-in users to enter any email address they'd like to receive the notifications. You can require notifications to be activated (in the E-Mails section below) before additional notifications are sent. So if they enter an incorrect email, the owner of that address won't get spammed.)
- [x] Customizable search name (This will allow the users to edit the name of the saved searched.)
- [x] Create a delete link for registered user (This will allow the users to to delete their saved searches.)
- [x] Show empty list of saved searches (This will show the "Saved searches" tab even if the user has not yet created a saved search.
- Notification Interval (Here you can set the available notification intervals that users can choose from.)
- E-Mails (In this section you can create the emails that will be sent to activate notifications, and the notification emails themselves.)
- Manual Saved Search Creation
- [x] Allow manual creation of saved searches (This will create a menu item (by default, in the navigation menu) that lets users create saved searches without performing a search first.)
- [ ] Provide textfield for fulltext keywords (This will add a field that lets the user search keywords fields other than those listed below, like the "Body" field.)
- Fields for which to provide filter options (Each option you select here will add a field that the user can utilize to narrow their search.)
- [x] Title
- Search page
- Base path: saved-search-results
- Miscellaneous
- (Save settings)
Next, we need to enable the saved search block to allow users to save a search.
Display the Save search block
- Go to "Structure => Blocks" (admin/structure/block)
- Scroll down to Disabled
- Block: Node index: Save search (This block will only appear on search results pages which have been performed by Search API.)
- Region: Choose "Sidebar second"
- Re-arrange the blocks by simply dragging the "Node index: Save search" above the "Current search: Standard" block.
- (Save blocks)
Configure permissions
In order for users to have access to saved searches, we need to grant them the appropriate permissions.
- Go to "People => Permissions"
- Scroll down to Search API saved searches
- Grant "Use saved searches" to:
- [x] Anonymous user
- [x] Authenticated user
- (Save permissions)
Saving searches as an authenticated user
Let's take a look at how this works for authenticated users.
- Go to the home page and enter "Item" as the keyword and click the "Search" button. (You'll notice that we now the the save search block where we can save searches and be notified of new results.)
- I'll select a notification interval.
- Then click "Save search"
Now, I'll create another item with "Item" in the title. Once that's done, I'll run Cron, and check my email. Here's the email that lists the new results for the search term!
You'll notice that the email uses the template that we created earlier. It includes:
- My username
- Text explaining where the email came from
- Which term has new results
- The new result, with a link to view the result
- A link to the "Saved searches" tab of our profile, where we can view all of our saved searches.
Saving searches as an anonymous user
Now, I'll demonstrate the user experience for anonymous users.
- With the site open and viewing it as an anonymous user, I'll perform a search.
- When I do, I'll see the "Save search" block in the sidebar.
- I'll enter an email address and select a notification interval.
- Then click "Save search"
Now, I'll check my email, and see that I'm required to click the link to activate the search. If I do nothing, the search will be discarded. I'll go ahead and click the link. Now that it's active, we can create new content to see it in action.
I'll create an article with "cui" in the title. Once that's done, I'll run Cron, and check my email. Here's the email that lists the new results for the search term! You'll notice that a couple of the tokens don't work properly, and that there isn't any way to delete this search. That's bad for usability and for your email spam ranking. Because when the person no longer wants to receive them, without any way to stop them, their only option is to mark them as spam until they stop receiving them. So, let's add a delete link to these emails.
- Go to "Configuration => Search and Metadata => Search API" (admin/config/search/search_api)
- Node index
- Click the dropdown
- Select "Saved searches"
- Expand the "Emails" section
- Expand the "Notification Mails" section
- In the "Results replacement" field, add this to the bottom: Delete this search here: [search-api-saved-search:delete-url]
- (Save settings)
Now, I'll create another article and run cron.
This time, when I check my email, the notification includes a link to delete the saved search. When I click the link, I'm taken to a page to confirm the deletion, and once I do that, I'll no longer receive emails for this saved search.
This still isn't perfect, though. So, if you'd like to jump in and help out with saved searches for anonymous users, checkout the issue at https://drupal.org/node/1306622.