My project has some data that needs to be in the index so it can be filtered on (and also returned), but that data is computed and not necessarily a field on the entity or traceable through relationships.

The apachesolr project has a hook called HOOK_apachesolr_update_index that gives you access to alter the document just before it is posted to the server. It does not appear that this module will allow for that currently.

1) Am I missing it somewhere and it does actually exist?

2) If not, I will roll a patch to add that if you are willing to accept that functionality.

Thanks!

Comments

febbraro’s picture

Status: Active » Needs review
StatusFileSize
new1.34 KB

Here is a patch that I have working in my development environment. I also added docs to the api.php file to document the new hook.

drunken monkey’s picture

My project has some data that needs to be in the index so it can be filtered on (and also returned), but that data is computed and not necessarily a field on the entity or traceable through relationships.

You could still add it as an entity property via hook_entity_property_info_alter(). Just use a function for the new property's "getter callback" which computes the wanted value.
You could also do that via a data alteration, if you want.

Both of these have the additional advantages that the field will be accessible by the Search API – if you just alter the Solr document, the data will be in the Solr index but there won't be many ways in which you can use it via the Search API (except by again using Solr-specific alter hooks).

If you still think a custom Solr hook is the way to go, the patch still has to be improved. First off, the doc comment for the hook contains several C&P errors. Also, we should add an (empty) alterSolrDocument() helper method to the service class, like we already have for hook_search_api_solr_query_alter() and hook_search_api_solr_search_results_alter(), so subclasses can also easily alter documents.
But without a clear use case, I'd hesitate to commit this anyways, so you would also have to explain why adding the field in the Search API, as usual, isn't an (desirable) option.

davidseth’s picture

I can think of a case. Please take a look at: http://drupal.org/node/1117464#comment-4877058. Someone is wanting to store a field within Solr in an exact LatLonType type. This hook would make that very easy to do (once I know what format they need it in).

drunken monkey’s picture

Good point, this wouldn't be easy to do at the moment. Then the only remaining issues are the ones mentioned in the last paragraph of #2:

First off, the doc comment for the hook contains several C&P errors. Also, we should add an (empty) alterSolrDocument() helper method to the service class, like we already have for hook_search_api_solr_query_alter() and hook_search_api_solr_search_results_alter(), so subclasses can also easily alter documents.

drunken monkey’s picture

Status: Needs review » Needs work
sebas5384’s picture

Status: Needs review » Needs work

I have a case where we need to duplicate items of one entity, because the rendered content is different for each location, so I'm using the hook_search_api_index_items_alter() to clone the item and indexing.
But!! we know whats the problem here(at least the more critical), is the duplicate entry of the "id" field, which is the same for each entity, so, with this patch I can resolve that altering the "id" field before the import.

If someone knows another way around without changing the module's code, please let me know!! :D

[Edited]
@drunken monkey thanks for the comment #2 ! was the way to the final solution for the duplicating thing.

Cheers!,
Sebas.

maciej.zgadzaj’s picture

Status: Needs work » Needs review
StatusFileSize
new1.35 KB

Thomas, as a follow-up to your comment #5 in #2060305: Add hooks on data import and extract, I've changed slightly febbraro's patch from #1, made it work on all documents in the batch (as per your suggestion), re-rolled for the current dev version and updated api doc accordingly.

I guess your comment re adding alterSolrDocument() no longer applies?

drunken monkey’s picture

StatusFileSize
new2.35 KB

I guess your comment re adding alterSolrDocument() no longer applies?

Yes, it does, why shouldn't it?

Anyways, thanks a lot for refining and re-rolling this patch! I now also added the alterSolrDocuments() method, patch attached. Please test/review, then we can commit this.

maciej.zgadzaj’s picture

Status: Needs work » Reviewed & tested by the community

Yes, it does, why shouldn't it?

Um, because I haven't noticed any other alter method in SearchApiSolrService class, including nothing for previously mentioned hook_search_api_solr_query_alter() - and thought it was just a thing of the past? Or am I just simply blind?

Please test/review, then we can commit this.

Looks good!

drunken monkey’s picture

Status: Reviewed & tested by the community » Fixed

Um, because I haven't noticed any other alter method in SearchApiSolrService class, including nothing for previously mentioned hook_search_api_solr_query_alter() - and thought it was just a thing of the past? Or am I just simply blind?

See the preQuery() and postQuery() methods.
Admittedly, they don't follow the same naming scheme, but they serve exactly that purpose.

Looks good!

Excellent, thanks for reviewing!
Committed.

Status: Fixed » Closed (fixed)

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