Current stemming modules transform words in the input file to their stem using preprocessing (hook search_preprocess). As a result all entries in word.search_index and the complete text in data.search_dataset is transformed to the stem equivalents.
When preparing the snippet to be shown in search results, search_files uses the code

'snippet' => search_excerpt($keywords, $dataset->data),

what results in a sequence of stemmed word shown to the user.

In the opposite module "node" avoids this by handing over the original (unstemmed) text

'snippet' => search_excerpt($keys, $node->body),
CommentFileSizeAuthor
#3 search_files-column_body.patch1.87 KBschildi

Comments

schildi’s picture

Please check if the following changes can be applied to your module:

  • alter table search_files_files add column body longtext;
  • in search_files.module, function search_files_update_index
    after line
    search_index($file->id, 'search_files', $file_text);
    append the following two lines:
    $set_body = "UPDATE {search_files_files SET `body`='%s' where `id`='%s'";
    $set_body_result = db_query($set_body, $file_text, $file->id);
  • in function search_files_search
    exchange
    'snippet' => search_excerpt($keywords, $dataset->data),
    by
    'snippet' => search_excerpt($keywords, $result->body),
jrglasgow’s picture

now that you have brought this to my attention I agree that it needs to be fixed. if you would create a patch it would be most useful, otherwise it will take me more time to get this change made.

schildi’s picture

StatusFileSize
new1.87 KB

Please check the appended patch

schildi’s picture

Status: Active » Needs review

Status changed: see #3

ezraw’s picture

Version: 6.x-1.6 » 6.x-2.0-beta4
Status: Needs review » Active

I couldn't get this patch to work as expected. I applied it against 1.6 and the snippet disappeared (which is admittedly better than the trimmed words). I expected it to show the full snippet but without the trimmed stem. I uninstalled, reinstalled and did not get any results from search files at all, but probably not related the patch? Not sure, and since there is a 2.0 dev version I decided to try that.

This problem exists in the 2.0 beta4 version as well. With stemming the words are trimmed to their stems.

Since this issue is pretty old and it looks like the 2.0 version is the way to go now, I am going to update this issue and set it to the new version and make the status active. Hope that's OK.

schildi’s picture

Hello Ezra

I'm not sure to understand what you did. The "snippet patch" accompanies the german stemmer. My site is still running D5, but I expect it to run seamlessly on D6 either.
But when you are using a different stemmer (e.g. the english porter stemmer) it will not work out of the box. The german version "de_stemmer" provides a function "de_stemmer_stem_list" which returns some kind of a lookup table. This function is probably not provided by other stemmers (but should be quite easily implemented).
But you are talking about "search files". As you can see in the patch I provides there was some issue with this module (5.x version):
* When using a stemmer the original text is/was overwritten with the stemmed version.
* Therefore the database table should be altered to keep the original text
* And one or two source lines should be altered too to use the new column
* If a file is attached to a node the corresponding node permissions should be checked
* and in the case the file is attached to a node it would be nice to have a link pointing to this node

And please don't forget to re-index your site!

Does this help?

To have a look at possible results, please check e.g.
http://archiv.bgv-rhein-berg.de/de/search/search_files/häuser

"Häuser" is the german plural form of "Haus". You can see that singular and plural forms are highlighted.

Regards

ezraw’s picture

Ah, OK. That makes more sense now. I was using the English porter stemmer. I wasn't actually using this module on a site but just taking some time to review patches (patch bingo). This one popped up and the functionality seems useful so I thought I'd give it a try.

I'd review it against German but ich spreche kein Deutsch!

schildi’s picture

when just applying the patch I provided for "search files", then the original text will be kept correctly in a new column "body". But when searching the stemmed word will be looked at in the original text and probably now found. Therefore I provided a lookup function (included in de_stemmer, patch against search.module) which will map the stemmed words to their corresponding words in the text.

henkiejan’s picture

Hello,

The problem with a snippet of stemmed words also exists for search attachments. The patch doesn't work there; there's no table for attachments to save the original text. Is it necessary to create a new table for attachments and changing the code in the same way as for files?
A new table and a lot of code, just for a correct search-snippet, or is there another way?

Erik Greve
www.kennisakker.nl

schildi’s picture

As far as I know "search files" and "search attachments" both rely on the core search functionality. And this does not contain functions to handle a stemmed text.

henkiejan’s picture

Ok then, for search attachments I've added an extra table:

function search_files_attachments_update_6202() {
  $schema['search_files_attachments'] = array(
    'description' => t('unstemmed text of attachments that we need for snippet'),
    'fields' => array(
      'id' => array('type' => 'int', 'not null' => TRUE, 'disp-width' => '11'),
      'content' => array('type' => 'text', 'size' => 'big', 'not null' => FALSE, 'default' => ''),
    ),
    'primary key' => array('id'),
  );
  $ret = array();
  if (!db_table_exists('search_files_attachments')) {
    db_create_table($ret, 'search_files_attachments', $schema['search_files_attachments']);
  }
  return $ret;
}

Then a few extra lines in this function:

function search_files_attachments_index_file($file) {
  $contents = search_files_attachments_get_file_contents($file->filepath);
  if ($contents) $contents = search_files_convert_to_utf8("{$file->filename} ". $contents);
  search_index($file->fid, 'search_files_att', $contents);
-->  $set_text = "UPDATE {search_files_attachments} SET content = '%s' WHERE id = %d";
-->  db_query($set_text, $contents, $file->fid);
-->  if (!db_affected_rows()) {
-->    $set_text = "INSERT INTO {search_files_attachments} (id, content) VALUES (%d, '%s')";
-->    @db_query($set_text, $file->fid, $contents);
-->  }
  return $contents;
}

And last but not least, added/changed two lines in the hook_search-function:

$text = db_result(db_query("SELECT content FROM {search_files_attachments} WHERE id = %d", $item->sid));
'snippet' => search_excerpt($keywords, $text),

That did the trick!

Erik Greve
(sorry, I can't make patches)