【问题标题】:Symfony2 Elastica bundle (elasticsearch) - possible to restrict only 'active' items?Symfony2 Elastica bundle (elasticsearch) - 可以只限制“活动”项目吗?
【发布时间】:2012-07-25 17:59:26
【问题描述】:

我目前正在使用FOQElasticaBundle 与我的弹性搜索服务器进行交互,但我似乎无法找到一种方法来根据每个实体上的“活动”标志来限制搜索结果。是否可以以某种方式在配置中设置它?这是我当前的配置:

foq_elastica:
    clients:
        default: { host: localhost, port: 9200 }

    indexes:
        website:
            client: default
            types:
                story:
                    mappings:
                        title: { boost: 8 }
                        summary: { boost: 5 }
                        text: { boost: 3 }
                        author:
                    persistence:
                        driver: orm # orm, mongodb, propel are available
                        model: Joe\Bundle\StoryBundle\Entity\Story
                        provider:
                        listener:
                        finder:

我不能只在控制器/视图级别排除不活动的故事,因为这会打乱分页 - 我需要每页有一致的项目数量,因此搜索需要过滤掉所有 @ 987654324@ 为假(或 0)。我确信这一定是可能的,但有人知道怎么做吗?

谢谢。

编辑

在为提供者指定自定义query_builder_method 失败后,我尝试指定自定义存储库并构建我的查询。但我还不能让它工作。这是我的搜索方法:

public function findByQueryString($queryString)
{

    $builder = new \Elastica_Query_Builder();
    $builder
        ->queryString()
            ->field('query', $queryString)
        ->queryStringClose()
        ->filter()
            ->term()
                ->field('active', 1)
            ->termClose()
        ->filterClose()
    ;
    // TODO check published date...

    return $this->findPaginated($builder);
}

生成以下 JSON:

{
    "query": {
        "query_string": {
            "query": "story"
        },
        "filter": {
            "term": {
                "active": "1"
            }
        }
    },
    "from": 0,
    "size": 10
}

但由于某种原因,它似乎不喜欢过滤器的“术语”部分。我只是想模仿elasticsearch documentation 中的内容,但我承认我真的不知道我应该做什么!

【问题讨论】:

    标签: php symfony elasticsearch


    【解决方案1】:

    是的,可以使用Doctrine query builderRead here.

        persistence:
                driver: orm
                model: Application\UserBundle\Entity\User
                provider:
                    query_builder_method: createIsActiveQueryBuilder
    

    【讨论】:

    • 感谢您的回复,但这对我不起作用。该方法从未被调用 - 我花了一段时间彻底调试正在发生的事情,并且从未调用过使用此 query_builder_method 配置值的 AbstractProvider。看起来 findPaginated() 没有利用这个。
    • 忽略我最后的评论。这确实起到了作用——我只需要清除缓存并重新填充 elasticsearch。
    【解决方案2】:

    好吧,这是 3 年前的事了,但新读者可能想阅读这个答案。

    ==> 其实很简单,不需要工作。

    例如,我有一个带有“已发布”布尔字段的 Post 实体。 确保在您的实体类中有此字段的 getter。 在此示例中,getter 是 getPublished。 然后你可以简单地在你的配置文件中配置监听器。

     persistence:
         listener:
             is_indexable_callback: "getPublished"
    

    Here is the doc

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-19
      • 2022-10-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多