2015-06-14 93 views
0

如何使用Elastica查詢WHERE categoryId = 1 OR categoryId = 2?我這樣做,但我得到了0 resultElastica PHP查詢哪裏或

$query = new \Elastica\Query(); 
    $boolOr = new \Elastica\Filter\BoolOr(); 
    $boolOr->addFilter(new \Elastica\Filter\Term(array('categoryId' => '1'))); 
    $boolOr->addFilter(new \Elastica\Filter\Term(array('categoryId' => '2'))); 
    $filtered = new \Elastica\Query\Filtered(new \Elastica\Query\MatchAll(), $boolOr); 
    $query->setQuery($filtered); 
    $products = $type->search($query)->getResults(); 

回答

1

這裏是一個工作示例:

$index = $this->_createIndex(); 
$type = $index->getType('test'); 

$doc1 = new Document('', array('categoryId' => 1)); 
$doc2 = new Document('', array('categoryId' => 2)); 
$doc3 = new Document('', array('categoryId' => 3)); 

$type->addDocument($doc1); 
$type->addDocument($doc2); 
$type->addDocument($doc3); 

$index->refresh(); 

$boolOr = new \Elastica\Filter\BoolOr(); 
$boolOr->addFilter(new \Elastica\Filter\Term(array('categoryId' => '1'))); 
$boolOr->addFilter(new \Elastica\Filter\Term(array('categoryId' => '2'))); 

$resultSet = $type->search($boolOr); 

你不需要使用過濾和matchall查詢。這個工作的例子可以在這裏找到:https://github.com/ruflin/Elastica/pull/887/files

+0

這個答案已被棄用在更高版本,我找不到\ Elastica \ Query \?相當於它。 @ruflin有什麼想法? – pcm