更改Magento索引全文搜索


Alter Magento Index Fulltext Search?

我有一个独特的任务,我已经完成了,但这个子任务非常困难!所以你有背景:我们运行一个Magento网站,并使用自定义的SOLR搜索页面。我使用phpSolrClient来解析Solr XML并返回可用的结果,然后从中构建搜索结果页面。

我得到的任务是在Magento的后端有一个"属性",让我们称之为"search_tags"。目标是能够插入一个标签,它的权重由逗号分隔:

sight^2,hearing^1,smell^3

我想编辑Magento的全文重新索引中的代码,以拆分字符串,并在fulltext1_en字段中插入X次标记。因此,它将添加两次"视觉"、一次"听觉"和三次"嗅觉"。这将允许我们说,当有人搜索榨汁机时,在页面上放一个搅拌器,即使"榨汁机"或"果汁"一词没有出现在fulltext1_en字符串中。我已经开发了拉、拆分和迭代的代码。。。然而,我仍然停滞不前,因为我不知道在重新索引过程中要编辑什么代码才能将其包含在我的fulltext1_en中。如果有人有任何编辑Magento全文索引的经验,我们将非常感谢您的输入!我查看了Indexer.php,但该文件中的所有内容充其量都是模糊的,所以这毫无帮助!一定要爱马根托!

对于那些希望使用SOLR在Magento中更改并为自定义搜索提供"加权标签"的人来说,我彻夜未眠,但它确实有效。。。

首先,在Magento中创建一个过滤器,并将其应用于所有产品。我把我的名字命名为"search_tags"。

接下来,在测试项目的过滤器中使用以下公式:

dude^25,crazyman^25,wierdsearch^25

每个单词后面跟着一克拉,然后是你想给它的重量。(这是这个单词重复多少次,然后添加到fulltext1_en中。)

完成后,打开以下文件:

/app/code/core/Mage/CatalogSearch/Model/Mysql4/Fulltext.php

我知道上面写着MySQL4,不用注意,SOLR使用这个索引。

关于500行,您将看到以下区块:

    if ($selects) {
        $select = '('.join(')UNION(', $selects).')';
        $query = $this->_getWriteAdapter()->query($select);
        while ($row = $query->fetch()) {

在该块的正下方插入以下内容:注意:不要使用我在这里列出的属性ID,这是我的设置独有的。您必须搜索数据库才能找到此ID。我使用JOIN将eav_attributescatalog_product_entity_varchar连接起来,并使用SELECT查找attribut_idvalue WHERE entity_ID=(在此处插入您的产品ID)。这很痛苦,但这是唯一的办法。这将返回该产品的所有属性。查找具有我们之前输入的标签的那个,并获取它的ID。将其插入下面的代码中。

      $attr_val = $row['value'];  // Set attr_val so that it can be manipulated in following IF
      if ($row['attribute_id'] == 457) {    // 457 is the ID of MY search_tags filter, yours WILL be different!  It can be found by joining eav_attributes table and catalog_product_entity_varchar and searching for the attribute value and ID where entity_id is X
            $input = $row['value'];             // Set $input to value of filter
            $attr_val = "";                         // Create Emtpy string
            $pieces = explode( ',', $input ); // Explode filter by comma
                foreach ($pieces as $val){
                $i=1;
                $val = explode( '^', $val);    // Explode each "tag" by carat
                    while ($i <= $val[1]) {     // Loop while $i is less than or equal to the number on the right side of the carat
                    $i++;
                    $attr_val = $attr_val . " " . $val[0];  // Append $attr_val with the word to the right side of the carat
                    }                   
                }                       
         }
    $result[$row['entity_id']][$row['attribute_id']] = $attr_val;  //  Modified from Original

插入后。。。然后注释掉下面的块。

$result[$row['entity_id']][$row['attribute_id']] = $row['value']; // ORIGINAL BLOCK  --  UNCOMMENT -- DO NOT DELETE

现在运行一个全文重新索引,你的全文1_en应该显示你已经添加了25次"dude"、"crazyman"answers"怪异搜索"!索引完成后,在网站搜索中搜索任何标签:添加标签的项目应该显示在顶部附近,如果不是顶部的话。享受