This is similar to #1085072, but am starting a new issue as that one is a couple years old and relates to D6.
The issue I'm having is that short of manually deleting attachments that have been uploaded and later replaced on the site, I've been unable to find a way to keep old versions of attachments from appearing in the search index. I'm getting duplicates if the attachments are included in the index as separate entities and false hits if bundled with their parent entity.
I'm using workbench moderation to create new versions and track changes to nodes and want to continue to support the ability for content editors on the site to review and restore previous versions (attachments included) if possible. I've seen some suggestions related to programmatically deleting files after a user clicks remove and saves a node, but would prefer not to go that route if possible.
Looking at the module code, it looks like anything that is in the file_managed table that is a valid file gets included in the index. Is it possible to limit indexed attachments to only those that go with the most recent version of their parent node?
Comments
Comment #1
nick_vhCould you write up a scenario how someone can test this bug?
It feels like there is indeed something wrong and I'd love to give it a try but I'm hoping for a clear tutorial to show me how I can see things are wrong.
Thanks!
Comment #2
moultonk commentedI'm experiencing the same issue. Files used in previous revisions of a node are coming up in the search results. If you have enabled Revisions in your content-type, and have a field of type "File", you can run this test:
1. Find or create a node that has no file associated with it.
2. Upload a PDF that contains a unique word and save the node.
3. Index queued content at admin/config/search/apachesolr.
4. Search on the unique word to make sure the PDF is being indexed.
5. Remove the file from the node and Save.
6. Re-index queued content (and clear any necessary caches) to make sure search index is fresh.
7. Search on the unique word. It will show up and point to the PDF that was saved with the revision.
It seems to me this is also a problem of Revisioning, which retains old files (re issue 1816584) at their original paths, but really, Apache Solr Attachments should remove the contents of files that have been revisioned from the index.
Comment #3
hdotnet commentedWe are seeing the exact same issue here...
Comment #4
andrew-drupal commentedI have the issue as well, is any solution now?
Comment #5
bbcStill getting questions about this from one of my clients. Seems like this is something that could be solved if there were a way to figure out whether a file from the file_managed table was part of a current node revision. Any suggestions about how to get that info?
So far, I've been experimenting with things like file_usage_list and the various field_attach_ functions, but am not having much luck. Using file_usage_list, I can get a list of the nodes where the file is used, but the nids returned by file_usage_list are the same regardless of which revision of the node that the file was attached to.
The other issue I'm running into is that some of the attachments I'd like indexed are set to not display, which means that they won't necessarily appear in the field_attachments array on node_load.
Suggestions welcome. Would be happy to work on a patch if someone can point me in the right direction.
Comment #6
moultonk commentedI was able to resolve this on my site but your mileage may vary. When you look at the Configuration settings on the Apache Solr Default Index tab (/admin/config/search/apachesolr), you might try unchecking anything under FILE. Then go over to the Attachments tab and open the Bundle configuration page. There you can specify that your file attachments are searched as part of the parent entity of the content-type you wish to have searchable. That is enough to get the contents of your files indexed, but only if they are currently attached to the node. At least, when I ran the test I described above, the PDF disappeared from my search results after I removed it from the node.
The consequence of this, however, is that if you have any files that are not specifically attached to a node through a dedicated file field, they will not be indexed in search. But this may be a good thing depending on your workflow.
Comment #7
bbcThanks moultonk. Your suggested config is exactly what I settled on after discovering the bug a few months back. Unfortunately, my client is attached to the idea that the files themselves (not their parent entities) should appear in the search results.
The other problem with this approach based on my testing is that it can result in false positives in the case that a keyword that was searched for exists in the attachment of an older revision of the file, but not in the current one.
In any case, I appreciate the help.
Comment #8
vistree commentedI have the same problem and I need to keep attachments being indexed as standalone objects. Has anybody an additional idea on how to solve this problem?
Comment #9
vistree commentedAnybody??
Comment #10
mike-michal commentedHi Drupal Community,
I'd like to share with you my solution for this headache. The idea is to add new field to Solr index for `file` entity_type `bs_latest_revision` that defines if the file comes from latest revision. Then it's just a matter of query alter based on this field in order to retrieve files only from latest revision. Here is ready solution (improvements are nicely welcome):
1. Implement hook_entity_info_alter.
2. Implement helper function to extend `file` entity type in Solr index.
3. Drupal search query alteration
4. Clear Cache
5. Reindex.
Hope it helps,
Peace! Michał K.
Comment #11
vistree commented@michalk: nice solution!! Just one feedback to your code: seems that $filedocument->setMultiValue (in _YOURcustomMODULE_solr_document) is deprecated. Can we use $filedocument->addField instead?
And, how are your index settings? Did you enable "File->Document" on "Default index" page?
And, what are your bundle settings on the "Attachments" page? I guess you set "Attachments as separate entities" for each content type? What about the "Document" bundle? Did you enable indexing here or did you choose "Don't index attachments" instead?
Comment #12
vistree commented@michalk: tried your code - but now nothing is in the search results anymore. No nodes, no attachments, no users ...
Problem seems to be with the YOURcustomMODULE_apachesolr_query_prepare function - if I comment this function, search results are shown again.
Comment #13
mike-michal commentedHi @vistree,
I just tested the code against my website and it works fine. Have you cleared the cache and reindexed?
And yes "File->Document" enabled.
And yes "Attachments as separate entities" for each content type.
With the current condition you wont get the users in search results, I believe that if you add this it will give you users as well:
$sub_q->addFilter('entity_type', 'user');
Maybe your Solr engine version is the problem. I use 5.5.5
Check if "bs_latest_revision" field has been indexed by Solr.
Please let me know if you got any progress.
Kind regards,
M.K.
Comment #14
vistree commentedHi Michal,
still investigating to get your workaround running. Seems, that the bs_latest_revision field is indexed by SOLR (bs_latest_revision boolean 2) - but I need to get access to the solr itself to check engine version and to check the index itself.
Will provide feedback as soon as possible ;-)
Comment #15
vistree commentedHi Michal,
should the field bs_latest_revision exist on file entity only or also on the node entity? Installed solr_devel module an can get information for node like this:
Comment #16
ñull commentedInitial test gives me the suspicion that the code in #10 sort of requires revisions. The parent node without revisions should produce file SOLR index records that always have "bs_latest_revision": true, but I see here that for one of the two attached files "bs_latest_revision": false. With SOLR queries I could find more attachments indexed with bs_latest_revision false of parent nodes that had no revisions . I will try to resync my data from production re-do indexing to see if I can reproduce the same behavior. Depending on my client's priorities I might also provide alternative code.
Can someone who better understands the code confirm my suspicion?