Problem/Motivation

We have a quite complex site where a lot of the output is cached.
And so are search pages. Unfortunately it's quite hard to figure out when to clear the cache of the search pages. When using "Index items immediately" we can act on entity CRUD operation hooks.
But as soon as "Index items immediately" is off it's becoming hard to figure out when to act.
And to create a datasource just for the purpose of this looks like an overkill.

Proposed resolution

Add a hook to search_api_track_item_indexed() to notify modules that items were indexed.

Remaining tasks

Review.

User interface changes

none

API changes

Added hook hook_search_api_track_item_indexed

Comments

drunken monkey’s picture

Title: Add hook_search_api_track_item_indexed » Add hook_search_api_items_indexed()
StatusFileSize
new1.19 KB

What about hook_search_api_index_items_alter()? Wouldn't that be enough for your use case?
I mean, if it's a custom module, you'll know what items (if any) will be rejected by data alterations, so you could just register a shutdown function (to really make sure that the items have already been indexed when the cache is cleared) and clear the cache there.

But I'd also not really be against adding this new hook. Shouldn't be a performance problem or anything, and more hooks are a good thing in most cases. ;)

It's not very helpful to use a method in the example code that doesn't exist, though. ;) The hook documentation also fits better right after the corresponding alter hook, I think. You had it even outside of the @addtogroup hooks block.
Lastly, I think it should be called hook_search_api_items_indexed(). The singular is a bit confusing (it's the same for the function, I know, that's my fault) and the "track" doesn't really fit there, either (and makes things more confusing when it's almost the function name, but not exactly).

So, if we really add this hook, then with the patch attached (or a further improved variant, of course). Please review!

das-peter’s picture

Status: Needs review » Reviewed & tested by the community

What about hook_search_api_index_items_alter()? Wouldn't that be enough for your use case?
I mean, if it's a custom module, you'll know what items (if any) will be rejected by data alterations, so you could just register a shutdown function (to really make sure that the items have already been indexed when the cache is cleared) and clear the cache there.

Hmm, that's an interesting idea and I think that could be an viable approach. However, somehow I personally don't like to register shutdown functions and try to avoid it whenever possible.
So if you don't mind to add the hook I'd prefer to add it :)

It's not very helpful to use a method in the example code that doesn't exist, though. ;)

I guess you mean $index->getEntityType() - that's just because I'm coming from the future - the code will become valid as soon as #1760706: Add a flexible way for determining whether an index contains entities is in :P
Patch looks good to me. Only nit-picky thing I could find is An array of the indexed item ids. instead An array of the indexed items.
But I'm bold enough to set this to RTBC.

drunken monkey’s picture

Status: Reviewed & tested by the community » Fixed

Slightly rephrased hook documentation and committed.
Thanks again!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.