dzenlife
8/22/2019 - 9:36 AM

bitrix search with indexing

<?
AddEventHandler("search", "BeforeIndex", "BeforeIndexHandler");
function BeforeIndexHandler($arFields) {
    //BODY
    $arrIblock = array(11);
    if ($arFields["MODULE_ID"] == 'iblock' && in_array($arFields["PARAM2"], $arrIblock) && intval($arFields["ITEM_ID"]) > 0){
      $dbElement = CIblockElement::GetByID($arFields["ITEM_ID"]) ;
      if ($arElement = $dbElement->Fetch()) {
        $arFields["BODY"] = $arElement['DETAIL_TEXT'];
        // echo $arFields["BODY"]; exit;
      }
    }
    //TITLE
    $words = [];
    $arFields['TITLE'] = trim($arFields['TITLE']);
    if (preg_grep('/-/', explode("\n", $arFields['TITLE']))) {
        $words[] = preg_replace('/-/', '', $arFields['TITLE']);
    }
    $masks = [
        '/.*?([0-9]+).*?/i',
        '/.*?([0-9a-zA-Z.,]+).*?/i',
    ];
    //preg_match('/([x*х])/i', $input_line, $output_array);
    foreach ($masks as $mask) {
        preg_match_all($mask, $arFields['TITLE'], $tags);
        if (isset($tags[1])) {
            foreach ($tags[1] as $tag) {
              if (!in_array($tag, $words) && strlen($tag) > 1) {
                  $words[] = trim($tag);
              }
            }
        }
    }

    preg_match_all('/(?<sizes>[0-9,.]+[x|*|х][0-9,.]+[x|*|х][0-9,.]+)/iu', $arFields['TITLE'], $matches);
    $synonyms = ['', 'x', '*', 'х'];
    $searchSizes = [];
    if ($matches['sizes']) {
        $searchSizes = [];
        foreach ($matches['sizes'] as $size) {
            preg_match_all('/(?<num1>[0-9,.]+)(?<op1>[x|*|х])(?<num2>[0-9,.]+)(?<op2>[x|*|х])(?<num3>[0-9,.]+)/iu', $size, $elements);
            $arrNum[] = $elements['num1'][0];
            $arrNum[] = $elements['num2'][0];
            $arrNum[] = $elements['num3'][0];
            $isHasSeparator = preg_grep('/[.,]/iu', explode("\n", $size));

            foreach ($synonyms as $operator) {
                $newSize = implode($operator, $arrNum);
                if (!in_array($newSize, $searchSizes)) {
                    $searchSizes[] = $newSize;
                }

                if ($isHasSeparator) {
                    $temp = preg_replace('/[.,]/iu', '', $newSize);
                    if (!in_array($temp, $searchSizes)) {
                        $searchSizes[] = $temp;
                    }
                    $temp = preg_replace('/[.]/iu', ',', $newSize);
                    if (!in_array($temp, $searchSizes)) {
                        $searchSizes[] = $temp;
                    }
                    $temp = preg_replace('/[,]/iu', '.', $newSize);
                    if (!in_array($temp, $searchSizes)) {
                        $searchSizes[] = $temp;
                    }
                }
            }
        }
    }

    if ($searchSizes) {
      $arFields['TITLE'] = $arFields['TITLE'].'  '.implode(' ', $searchSizes);
    }
    if ($words) {
      $arFields['TITLE'] = $arFields['TITLE'].'  '.implode(' ', $words);
    }


    return $arFields;
}