Symfony2 Elastica束(弹性搜索) - 可能仅限制“活动”项目? [英] Symfony2 Elastica bundle (elasticsearch) - possible to restrict only 'active' items?
问题描述
我目前正在使用 FOQElasticaBundle 与我的弹性搜索服务器进行互动,但我不能似乎找到一种方法来限制基于每个实体上的活动标志的搜索结果。有可能在配置中设置这个吗?这是我当前的配置:
I'm currently using the FOQElasticaBundle to interact with my elasticsearch server, but I can't seem to find a way to restrict the search results based on an "active" flag on each entity. Is it possible to set this in the configuration somehow? Here is my current config:
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:
我不能在控制器/视图级别排除不活动的故事,因为这会弄乱分页 - 我需要一致数的每个页面的项目,所以搜索将需要过滤掉任何故事,其中活动
是假(或0)。我确信这是可能的,但是有人知道如何。
I can't just exclude inactive stories at the controller/view level because this will mess up the pagination - I'll need to have a consistent number of items per page so the search will need to filter out any stories where active
is false (or 0). I'm sure this must be possible, but does anybody know how.
谢谢。
编辑
没有运气为提供者指定自定义 query_builder_method
指定一个自定义存储库,并构建我的查询。但是我还没有得到这个工作。这是我的搜索方法:
After having no luck specifying a custom query_builder_method
for the provider, I had a shot at specifying a custom repository instead and building my query. But I haven't been able to get this to work yet either. Here is my search 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:
Which produces the following JSON:
{
"query": {
"query_string": {
"query": "story"
},
"filter": {
"term": {
"active": "1"
}
}
},
"from": 0,
"size": 10
}
但是由于某些原因,似乎并不喜欢过滤器的术语部分。我只是试图模仿弹性搜索文档中的内容,但是我承认我不知道我应该在做什么!
But it doesn't appear to like the "term" part of the filter for some reason. I was just attempting to mimic what is in the elasticsearch documentation but I admit I don't really know what I should be doing!
推荐答案
是的,可以使用 Doctrine查询构建器
。 阅读这里。
persistence:
driver: orm
model: Application\UserBundle\Entity\User
provider:
query_builder_method: createIsActiveQueryBuilder
这篇关于Symfony2 Elastica束(弹性搜索) - 可能仅限制“活动”项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!