Search code examples
phpsphinx

Php Sphinx, Using Or in setFilter()


Below is my fetch code for sphinx. We have a filter named typeId which we use to compare bureau type. Now I have added one more filter named as altTypeId. Now I have to compare typeId and altTypeId both for same value such typeId = 6 OR altTypeId = 6.

I have tried several solutions but no one is working. I followed the solution on this link: https://sphx.org/forum/view.html?id=13474 but its not working as well.

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']);
        }
    }

Solution

  • Frankly putting both values in the same sphinx attrbute, seems easiest. MVA is perfect for this!

    Couple of ways could be done, but just...

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

    then just

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

    finds results from EITHER column.


    Otherwise if really want to only do it at query time, its something like

    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 ...