Php Sphinx,在 setFilter() 中使用 Or
Php Sphinx, Using Or in setFilter()
下面是我的 sphinx 获取代码。我们有一个名为 typeId 的过滤器,用于比较局类型。现在我又添加了一个名为 altTypeId 的过滤器。现在我必须比较 typeId 和 altTypeId 的相同值,例如 typeId = 6 或 altTypeId = 6.
我尝试了多种解决方案,但没有一个有效。我遵循了这个 link 的解决方案: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']);
}
}
坦率地说,将两个值放在同一个 sphinx 属性中似乎最简单。 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 ...
下面是我的 sphinx 获取代码。我们有一个名为 typeId 的过滤器,用于比较局类型。现在我又添加了一个名为 altTypeId 的过滤器。现在我必须比较 typeId 和 altTypeId 的相同值,例如 typeId = 6 或 altTypeId = 6.
我尝试了多种解决方案,但没有一个有效。我遵循了这个 link 的解决方案: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']);
}
}
坦率地说,将两个值放在同一个 sphinx 属性中似乎最简单。 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 ...