Y
Y
Yuriy2020-12-17 11:19:39
Sphinx
Yuriy, 2020-12-17 11:19:39

Bitrix search sphinx how to set weight for certain fields?

how to set search weight?
for example, if a match is found in the title of the document, then put it in the first place in the results, if it finds it, put it lower in the description, even lower in the properties, etc.?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
Puma Thailand, 2020-12-17
@opium

In the options, you can set your own weight calculation formula, specify a larger one for the title

V
Vyacheslav Shevchenko, 2020-12-17
@WebDev2030

Most likely out of the box.
The matter is that in a sphinx two fields title, body in which the text information is stored. Therefore, you can raise either title or body. An example of a request from the network to set weights for fields:

SELECT id FROM tx3nh_users WHERE MATCH('keyword1') 
             OPTION field_weights=(p.fullname=3, s.staff_title=2, s.bio=1)

To rank by the position of the search query in the text, you should use the sph04 ranker. I inherited the Bitrix class from Sphinx and added in the Search and SearchTitle methodsOPTION ranker=sph04

Y
Yuriy, 2020-12-18
@yous

and there is already OPTION, just add a comma? and also sph04 itself arranges the weight? how does he understand that the title is above all the rest?

}

      $ts = time()-CTimeZone::GetOffset();
      if ($bTagsCloud)
      {
        $sql = "
          select groupby() tag_id
          ,count(*) cnt
          ,max(date_change) dc_tmp
          ,if(date_to, date_to, ".$ts.") date_to_nvl
          ,if(date_from, date_from, ".$ts.") date_from_nvl
          ".($cond1 != ""? ",$cond1 as cond1": "")."
          from ".$this->indexName."
          where ".implode("\nand\t", $arWhere)."
          group by tags
          order by cnt desc
          limit 0, ".$limit."
          option max_matches = ".$limit."
        ";

        $DB = CDatabase::GetModuleConnection('search');
        $startTime = microtime(true);

        $r =  $this->query($sql);

        if($DB->ShowSqlStat)
          $DB->addDebugQuery($sql, microtime(true)-$startTime);

        if (!$r)
        {
          throw new \Bitrix\Main\Db\SqlQueryException('Sphinx select error', $this->getError(), $sql);
        }
        else
        {
          while($res = $this->fetch($r))
            $result[] = $res;
        }
      }
      else
      {
        $sql = "
          select id
          ,item
          ,param1
          ,param2
          ,module_id
          ,param2_id
          ,date_change
          ,custom_rank
          ,weight() as rank
          ".($cond1 != ""? ",$cond1 as cond1": "")."
          ,if(date_to, date_to, ".$ts.") date_to_nvl
          ,if(date_from, date_from, ".$ts.") date_from_nvl
          from ".$this->indexName."
          where ".implode("\nand\t", $arWhere)."
          ".$this->__PrepareSort($aSort)."
          limit ".$offset.", ".$limit."
          option max_matches = ".($offset + $limit)."
        ";

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question