Elastica PHP查询在哪里或

时间:2015-06-14 15:15:58

标签: php elastica foselasticabundle

如何使用WHERE categoryId = 1 OR categoryId = 2进行Elastica查询?我这样做但我得到了0 result

    $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 个答案:

答案 0 :(得分: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);

您不需要使用filtered和matchall查询。可以在此处找到工作示例:https://github.com/ruflin/Elastica/pull/887/files