PHP Sphinx,在SetFilter()中使用or

时间:2019-03-12 16:00:44

标签: php sphinx

以下是我获取的狮身人面像代码。我们有一个名为typeId的过滤器,用于比较局类型。现在,我又添加了一个名为altTypeId的过滤器。现在,我必须将typeId和altTypeId都比较为相同的值,例如typeId = 6或altTypeId =6。我尝试了几种解决方案,但没有人在工作。我在以下链接上遵循了解决方案:https://sphx.org/forum/view.html?id=13474,但其效果也不佳。任何帮助将不胜感激。

protected function fetch() {
        $this->_searchCriteria->orders = $this->sort;
        $this->pagination->validateCurrentPage = false;
        $this->_searchCriteria->paginator = $this->pagination;

        $this->_searchCriteria->query.=" profileTypePlaceholderTexxxxxt";

        Yii::app()->search->SetLimits($this->pagination->offset, $this->pagination->limit);
        Yii::app()->search->SetFieldWeights($this->_fieldWeights);
        if ($this->_searchCriteria->query)
            Yii::app()->search->setMatchMode(SPH_MATCH_ALL);
        else
            Yii::app()->search->setMatchMode(SPH_MATCH_FULLSCAN);

        Yii::app()->search->setFieldWeights(array(
            'primary_comp' => 100,
            'premium' => 80,
            'standard' => 60,
            'free' => 40,
            'comp' => 20,
            'title' => 5,
            'description' => 5,
        ));

        //Yii::app()->search->SetSortMode(SPH_SORT_EXTENDED, '@weight DESC');
        Yii::app()->search->SetSortMode(SPH_SORT_EXTENDED,'@weight DESC, random DESC');



        foreach ($this->_searchCriteria->filters as $filter) {
            if ($filter[0] == 'range')
                Yii::app()->search->SetFilterFloatRange($filter[1], (float) $filter[2] + 0.01, (float) $filter[3]);
            else
                Yii::app()->search->SetFilter($filter[0], array($filter[1]));
        }
        $results = Yii::app()->search->Query($this->_searchCriteria->query, $this->_searchCriteria->from);
        $this->_data = array();
        $this->_keys = array();
        $this->_itemCount = $results['total'];
        $this->pagination->itemCount = $this->_itemCount;
        if ($this->_itemCount) {
            $this->_populateItems($results['matches']);
        }
    }

1 个答案:

答案 0 :(得分:0)

坦率地说,将两个值放在相同的狮身人面像属性中似乎最简单。 MVA非常适合!

可以采取多种方式,但是...

sql_query = SELECT id,title, CONCAT_WS(',',typeId,altTypeId) AS typeids FROM ... 
sql_attr_multi = uint typeids from field

然后就

->SetFilter('typeids', array(6));

从EITHER列中查找结果。


否则,如果真的只想在查询时执行此操作,则类似

if ($filter[0] == 'typeid') {
    Yii::app()->search->SetSelect("*, (typeid = {$filter[1]} OR altTypeId = {$filter[1]}) AS myfilter");
    Yii::app()->search->SetFilter('myfilter', array(1));
} else ...