简体   繁体   English

更改Magento索引全文搜索?

[英]Alter Magento Index Fulltext Search?

I have a unique task that I have been given, and I am in the last leg of it, but this sub-task is proving to be extremely difficult! 我已经完成了一项独特的任务,并且处于最后一步,但是事实证明,此子任务非常困难! So you have background: We run a Magento site, and use a custom built SOLR search page. 所以您有背景:我们运行一个Magento网站,并使用一个自定义的SOLR搜索页面。 I am using phpSolrClient to parse the Solr XML and return usable result which I then build the search results page from. 我正在使用phpSolrClient解析Solr XML并返回可用的结果,然后从中构建搜索结果页面。

The task I have been given is to have an "attribute" in the back end of Magento, lets call that "search_tags". 我得到的任务是在Magento的后端有一个“属性”,让我们称之为“ search_tags”。 The goal is to be able to insert a tag, and it's weight delimitered by commas: 目标是能够插入标签,并且其权重由逗号分隔:

ie sight^2,hearing^1,smell^3 sight^2,hearing^1,smell^3

I would like to edit the code in Magento's fulltext reindex to break apart the string, and insert that tag X of times into the fulltext1_en field. 我想在Magento的全文索引中编辑代码以拆分字符串,然后将X倍的标签插入fulltext1_en字段中。 So it would add "sight" twice, "hearing" once, and "smell" three times. 因此,它将两次添加“视力”,“听力”一次,“气味”三次。 This is going to allow us to say, put a blender on the page when someone searches for juicers, even though the term "juicer" or "juice" does not appear in the fulltext1_en string. 这将使我们可以说,即使有人在fulltext1_en字符串中没有出现“ juicer”或“ juice”一词,也可以在有人搜索榨汁机时在页面上放置一个搅拌器。 I have developed the code to pull, split and iterate ... However I am at an stand-still since I don't know what code to edit to include this in my fulltext1_en during the reindex process. 我已经开发了拉,拆分和迭代的代码。但是,由于我不知道在重新索引过程中不知道要编辑什么代码以将其包含在我的fulltext1_en中,因此我处于停滞状态。 If anyone has any experience with editing Magento's Fulltext Reindex, your input would be greatly appreciated! 如果任何人有编辑Magento的全文索引的经验,将非常感谢您的输入! I looked in Indexer.php, but everything in that file is ambiguous at best, so that was no help! 我查看了Indexer.php,但是该文件中的所有内容充其量都是模棱两可的,所以这没有帮助! Gotta love Magento! 一定要爱Magento!

OK for those looking to alter and give "weighted tags" to custom search in Magento using SOLR, I was up all night getting this right, but it works... 对于那些希望使用SOLR在Magento中进行自定义搜索并希望对其进行更改并提供“加权标签”的用户,我整夜都忙于正确执行此操作,但是它可以正常工作...

First, create a filter in Magento and apply it to all products. 首先,在Magento中创建一个过滤器,并将其应用于所有产品。 I named mine "search_tags". 我将其命名为“ search_tags”。

Next, use the following formula in that filter for a test item: 接下来,在该过滤器中为测试项目使用以下公式:

dude^25,crazyman^25,wierdsearch^25

Each word followed by a carat, and then the weight you want to give it. 每个字后跟一个克拉,然后是您要赋予的重量。 (This is how many times the word will be repeated and then added to fulltext1_en.) (这是单词将重复多少次,然后添加到fulltext1_en的次数。)

After that is done, open the following file: 完成之后,打开以下文件:

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

I know it says MySQL4, pay no attention, SOLR uses this index. 我知道它说MySQL4,不用理会,SOLR使用此索引。

About line 500, you will see the following block: 关于第500行,您将看到以下方框:

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

JUST BELOW this block insert the following: NOTE: Do not use the attribute ID I have listed here, that is unique to my setup. 仅在此块下方插入以下内容:注意:请勿使用我在此处列出的属性ID,这对于我的设置是唯一的。 You are going to have to search your database to find this ID. 您将必须搜索数据库以找到此ID。 I used JOIN to join eav_attributes with catalog_product_entity_varchar and used SELECT to find attribut_id and value WHERE entity_id = (Insert your product ID here). 我使用JOIN将eav_attributescatalog_product_entity_varchar eav_attributes在一起,并使用SELECT查找attribut_idvalue WHERE entity_id =(在此处插入您的产品ID)。 It's a pain, but it's the only way. 这很痛苦,但这是唯一的方法。 This will return all the attributes for that product. 这将返回该产品的所有属性。 Look for the one that has the tags we entered in earlier, and get it's ID. 查找具有我们之前输入的标签的标签,并获取其ID。 Insert that into the code below. 将其插入下面的代码中。

      $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

After you insert that ... Then comment out the following block. 插入后...然后注释掉以下块。

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

Now run a fulltext reindex, and your fulltext1_en should show that you've added "dude", "crazyman", and "weirdsearch" all 25 times! 现在运行全文索引,您的fulltext1_en应该显示您已经全部添加了25次“ dude”,“ crazyman”和“ weirdsearch”! When the index is completed, search for any of the tags in your site search: That item you added the tags to should show up close to, if not the top. 索引完成后,在站点搜索中搜索任何标签:添加标签的项目应显示在顶部附近(如果不是)。 Enjoy! 请享用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM