【问题标题】:add filters with foselasticabundle request使用 foselasticabundle 请求添加过滤器
【发布时间】:2015-11-15 00:54:30
【问题描述】:

我有一个 sf2 服务,可以在带有 foselasticabundle 的 elasticasearch 服务器上发出搜索请求。最初我用过滤器提出了一个请求:

public function findAll()
{
    $query = new \Elastica\Query\MatchAll();
    $response = new \Elastica\Query\Filtered($query, self::setFilter());
    return $response;
}

public function setFilter()
{
    $filter = new \Elastica\Filter\Bool();
    $filter->addMust(new \Elastica\Filter\Term(array('active' => true)));
    return $filter;
}

在我的控制器中,我得到了这个请求的计数:

    $search = $this->get("mb.search");
    $query = $search->findAll();
    $nbOnline = $this->get("fos_elastica.index.xxxx.yy")->count($query);

此代码工作正常。我想添加一个基于数组的过滤器:

$languages = array(1,3,5)

我没有找到在我的查询中添加这个新过滤器的好方法。我希望你能帮助我处理这个案子。提前致谢

【问题讨论】:

    标签: symfony foselasticabundle


    【解决方案1】:

    您可以为此任务使用“Terms”过滤器,here is PHP 实现。

    你的代码应该是这样的:

    new \Elastica\Filter\Terms(array('MY_FIELD' => array(1,3,5)))
    

    【讨论】:

    • 谢谢,我在我的方法中添加了这个:$filter->addMust(new \Elastica\Filter\Terms(array('language' => $this->languages)));但现在我有一个错误警告:非法偏移类型 500 内部服务器错误 - ContextErrorException
    • @Olivier 我没有你的索引结构,我不知道你为什么会收到这个错误。尝试使用 Sense 插件(谷歌浏览器)创建对索引的请求,然后转换它到 PHP。
    • 感谢您的帮助,我找到了解决方案,好的语法是 new \Elastica\Filter\Terms('MY_FIELD', $array)
    • @Olivier 啊,是的,我看到了here,看来我需要用 ES 刷新我的技能:) 。很高兴您找到了解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-18
    • 2013-02-22
    • 2018-06-22
    • 2023-04-04
    • 2016-01-19
    • 2021-06-09
    • 1970-01-01
    相关资源
    最近更新 更多