I've said it for a long time when people asked: while not working completely smoothly in D7, indexing multiple types of entities in one index (to simplify things like site search, etc.) would be perfectly possible with a custom datasource and a bit of configuration for creating new item types using it.
Thanks to #1184610: Limit indexes to specific entity bundles, the second part isn't even necessary anymore, the datasource itself can just have a configuration form for setting the entity types to index. And since this is a feature that surely a lot of sites will want (anyone who's ever tried to use Search API Multi-Index Searches will probably agree it doesn't do a great job), I think it makes sense adding it to this module, if possible.
| Comment | File | Size | Author |
|---|---|---|---|
| #6 | 2520934-6--multi_entity_datasource--interdiff.txt | 2.07 KB | drunken monkey |
| #6 | 2520934-6--multi_entity_datasource.patch | 26.25 KB | drunken monkey |
| #3 | 2520934-3--multi_entity_datasource.patch | 24.46 KB | drunken monkey |
Comments
Comment #1
drunken monkeyHere's a patch that would add such an item type, called "Multiple types". A few remarks:
Therefore, I decided to go with the "crazy hacks" alternative this time: the datasource now looks at the stack trace to figure out from which index it was called. Sounds insane (and probably is, to a point), but seems to work fine, as far as I can tell.
However, this makes it all the more important to get this thoroughly tested before committing.
Comment #2
drunken monkeyUpdated patch – forgot to adjust the index IDs in the new
search_api_item_string_idtable when they change (due to a feature revert, most likely).Comment #3
drunken monkeyAnd another revision: the "Bundle filter" data alteration now also supports multi-type indexes (to make up for the bundle setting not being present in the datasource config).
Comment #4
drunken monkeyHaving the
TYPE_SEPARATORconstant might be cleaner, and more analogous to how we do it in D8, but it causes fatal errors when updating completely unnecessarily.As much as I am a purist, I don't think that's worth it for a module, whose code is in not too good a shape style-wise anyways.
Also: No testers? Would be great to get some feedback from others here, so we make sure to end up with a patch that works for as many people as possible (and doesn't break anyone's site).
Comment #5
drunken monkeyAdded "Bundle" and "Entity ID" properties and renamed the property machine names to
item_id(etc.) instead of_id(etc.).The former could be handy in some cases, the latter just looks better and also doesn't mess with the Javascript on the "Fields" tab. I initially just used properties starting with an underscore to avoid potential clashes with entity types, but I guess there won't be any entity type with machine name
item_ideither.Comment #6
drunken monkeyAdded an
item_labelproperty to the items to get a single field for the entity label (for convenience – would have been easily possible with an "Aggregated fields" data alteration otherwise, except in edge cases).Comment #7
drunken monkeyCommitted.
Comment #9
benjy commented+1, sorry was going to comment yesterday but I tested this and it works well. I tested with the views integration, not sure if a rendered entity row plugin exists that supports multiple entity types?
Comment #10
benjy commentedTook a closer look at HEAD today and realised that there is already support for rendering multiple entity types. Thanks.