My entity has a term field that allows 1 and only 1 term.
It also has another term field allowing multiple terms.

Search API DB created a DB index table for the first field using an "item_id" primary key.
Instead the second field got a DB index table using item_id+value as primary key.

When I enable the "index hierarchy" "data alteration" option for the first term it fails with a lot of duplicate key exceptions on item insertion.

It works fine (after applying http://drupal.org/node/1400882) , instead, if I enable it for a "multivalued" term field.

CommentFileSizeAuthor
#3 patch-search_api_db-hierarchicalterm.diff1.32 KBbago

Comments

bago’s picture

I've just managed to get indexing of parents working for that fields by manually altering the primary key of the tables just after configuring the data alteration.
---
alter table search_api_db_nodes_field_myterm drop primary key;
alter table search_api_db_nodes_field_myterm add primary key (item_id, value);
---

I saw that submitting the data alteration form rebuilds my field tables, so I guess the search_api_db module have some way to know that it will receive multiple values and simply isn't "checking" it.

I guess this has something to do with service.inc:createFieldTable method where it currently does
"if ($type != $field['type']) {" to decide if the field is a list or not.

Maybe it should do "if (search_api_is_list_type($field['type'])) {" but I think this is not the issue, as if search_api_is_list_type just checks for "list<" so the current check should work too.

Maybe this is a bug in search_api SearchApiAlterAddHierarchy class and its propertyInfo method that should change the field type to list if the hierarchy indexing is enabled? (it seems the code try to do this, not sure it succeed or it contains bugs).

Unfortunately I'm a newbie to search_api/search_api_db/facet_api so I need some help/pointer before I can try to produce a patch.

bago’s picture

I've investigated this a while but I'm totally lost. It seems that the tables are regenerated when search_api_admin_index_workflow_submit is called but I debugged the service.inc:fieldsUpdated (search_api_db) called during that submit and "$fields" and "$new_fields" contains inverted options. So it seems that $fields contains the list type while $new_fields contains type integer type for my term with hierarchical option enabled.
So, it seems it updates the DB using "outdated" (or inverted) data.
$this->index is not corrent in that instant. When the form has been submitted and redisplayed the debug for $this->index instead gives the new updated values (but the search_api_db fields have already been created with bad values).

bago’s picture

StatusFileSize
new1.32 KB

Here is my attempt to fix the issue. Unfortunately I don't feel confident this is the right solution, but at least this fixes my own indexing issues, so maybe it will be good to test for anyone else with the same issues.

I have to add that I am on latest dev snapshot for entity and search_api (2012/03/21).

The weird thing is that I get the "old fields" record for the getFields() but then I read the "old type" from the index->original->options structure because the one in getFields() is already updated in that place. Still I need to loop through the getFields() result because it is the only place where I can read the "table" names.

drunken monkey’s picture

Thanks for reporting, and debugging so thoroughly!

The problem sounds like the one fixed with the patch in #1414138-10: Notice: Undefined index: search_api_access_node for the Search API. Please test the patch to see whether it helps. (Activating the data alteration with the patched version should correctly update the tables.)

bago’s picture

Status: Active » Closed (fixed)

It never happened since that patch. Thank you.