我的问题非常简单。我想在Elastica中的一个查询中组合两个过滤器one filter_bool和一个filter_range。代码是
public function getAdvancedTweetsEs($keyword, $location, $datepicker, $datepicker1, $offset)
{
$elasticaClient = new Elastica_Client();
// Load index
$elasticaIndex = $elasticaClient->getIndex('mongoindex3');
// Define a Query. We want a string query.
$elasticaQueryString = new Elastica_Query_QueryString();
//$elasticaQueryString->setDefaultOperator('AND');
$elasticaQueryString->setQuery($keyword);
// Create the actual search object with some data.
$elasticaQuery = new Elastica_Query();
$elasticaQuery->setQuery($elasticaQueryString);
$elasticaQuery->setFrom(0);
$elasticaQuery->setLimit($offset);
//Set a filter in text
$elasticaFilterBool = new Elastica_Filter_Bool();
$filter1 = new Elastica_Filter_Term();
$filter2 = new Elastica_Filter_Term();
$filter1->setTerm('text', array(strtolower($keyword)));
$filter2->setTerm('location', array(strtolower($location)));
$elasticaFilterBool->addMust($filter1);
$elasticaFilterBool->addMust($filter2);
$elasticaQuery->setFilter($elasticaFilterBool);
$range = new Elastica_Query_Range();
$range->addField('date',
array( 'from' => $datepicker,
'to' => $datepicker1
)
);
//Search on the index.
$elasticaResultSet = $elasticaIndex->search($elasticaQuery);
$elasticaResults = $elasticaResultSet->getResults();
$results = array();
foreach ($elasticaResults as $elasticaResult)
{
$results[] = $elasticaResult->getData();
}
return $results;
}
我无法在$ elasticaQuery中将两个过滤器组合在一起。我将不胜感激任何帮助!
谢谢!
答案 0 :(得分:4)
为什么不在must
过滤器中将范围过滤器添加为另一个bool
字词:
$rangeFilter = new Elastica_Filter_Range();
$rangeFilter->addField('date',
array( 'from' => $datepicker,
'to' => $datepicker1
)
);
$elasticaFilterBool->addMust($rangeFilter);
$elasticaQuery->setFilter($elasticaFilterBool);
您需要在布尔过滤器中使用过滤器而不是查询。注意我使用了Elastica_Filter_Range
而不是您正在使用的范围查询。
另外,您知道您使用的是过时版本的Elastica,新版本使用名称空间!