E
E
Evgeny Osipov2020-06-03 19:39:13
1C-Bitrix
Evgeny Osipov, 2020-06-03 19:39:13

How to make Bitrix search for linked elements?

Previously, I already asked the question when the product had downloaded files and it was necessary to search for them.
How can Bitrix search by the property of an infoblock of the File/Multiple type?

I added it to init.php and everything works great.

spoiler
$eventManager = \Bitrix\Main\EventManager::getInstance();
    $eventManager->addEventHandler('search', 'BeforeIndex', array('MySearch', 'BeforeIndex'));
    class MySearch {
      public static function BeforeIndex($arFields) {
        if(intval($arFields['PARAM2']) == 34 && intval($arFields['ITEM_ID']) > 0 && isset($arFields['BODY'])) {
          \Bitrix\Main\Loader::includeModule('iblock');
          $dbRes = CIBlockElement::GetList(array(), array('IBLOCK_ID' => $arFields['PARAM2'], 'ID' => $arFields['ITEM_ID']), false, false, array('PROPERTY_DOCUMENTS'));
          while($arRes = $dbRes->Fetch()) {
            $arFile = CFile::GetFileArray($arRes['PROPERTY_DOCUMENTS_VALUE']);
            $arFields['BODY'] .= PHP_EOL.$arFile['ORIGINAL_NAME'];
          }
          return $arFields;
        }
      }
    }


Some identical files in the product occur 30 times, so I created an infoblock and uploaded the files to the infoblock, then linked the infoblock elements to the product. Now the files are displayed in the product, but they have dropped out of the search.

What should be added to the handler so that the files associated with the product are added to the search?

For search engines: bitrix search linked elements

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
PetrPo, 2020-06-11
@Medik18

$eventManager = \Bitrix\Main\EventManager::getInstance();
    $eventManager->addEventHandler('search', 'BeforeIndex', array('MySearch', 'BeforeIndex'));

    class MySearch {
      private static $catalogIblockId = 2;
      private static $docsIblockId = 4;
      const PROPERTY_DOCUMENTS = 'PROPERTY_FILE';

      public static function BeforeIndex($arFields) {
        if(intval($arFields['PARAM2']) == self::$catalogIblockId && intval($arFields['ITEM_ID']) > 0 && isset($arFields['BODY'])) {
          \Bitrix\Main\Loader::includeModule('iblock');
          
          $arDocsId = array();

          $dbRes = CIBlockElement::GetList(array(), array('IBLOCK_ID' => $arFields['PARAM2'], 'ID' => $arFields['ITEM_ID']), false, false, array(self::PROPERTY_DOCUMENTS));
          while($arRes = $dbRes->Fetch()) {
              if($arRes[self::PROPERTY_DOCUMENTS.'_VALUE'])
                  $arDocsId[] = $arRes[self::PROPERTY_DOCUMENTS.'_VALUE'];
          }

          if($arDocsId) {
              $dbRes = CIBlockElement::GetList(array(), array('IBLOCK_ID' => self::$docsIblockId, 'ID' => $arDocsId), false, false, array('NAME'));
              while($arRes = $dbRes->Fetch()) {
                  $arFields['BODY'] .= PHP_EOL.$arRes['NAME'];
              }
          }
         
          return $arFields;
        }
      }
    }

- specify two variables and a constant in the class
- file name = element name in the infoblock with files

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question