问题
my question is very simple. I want to combine two filters one filter_bool and one filter_range in one query in Elastica. The code is
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;
}
I can't put together two filters together in $elasticaQuery. Please I will appreciate any help!
thanks!
回答1:
Why don't you add the range filter as another must
term in the bool
filter:
$rangeFilter = new Elastica_Filter_Range();
$rangeFilter->addField('date',
array( 'from' => $datepicker,
'to' => $datepicker1
)
);
$elasticaFilterBool->addMust($rangeFilter);
$elasticaQuery->setFilter($elasticaFilterBool);
You need to use filters, not queries, in a boolean filter. Note I have used Elastica_Filter_Range
instead of the range query you were using.
Also, you know you are using an outdated version of Elastica, the new ones use namespaces!
来源:https://stackoverflow.com/questions/18671580/combination-of-two-different-filters-in-one-query-elastica