Elasticsearch:在分面时排除过滤器可能吗? (就像在 Solr 中一样)
我正在考虑从 Solr 更改为 ES。 我找不到相关信息的一件事是 ES 是否允许我在分面时定义排除过滤器。
例如,考虑 producttype
的值:A,B,C
,我想对其进行分面(即:显示计数)。另请考虑查询仅限于 producttype: A
。
在这种情况下,Solr 允许我指定要排除约束 producttype: A
来影响 producttype
上的分面。 IOW,它显示 producttype
上的计数,就好像尚未应用约束 producttype: A
一样。
如何在 Solr 中执行此操作,请参阅:http://wiki.apache.org/solr/SimpleFacetParameters >标记和排除过滤器
在 ElasticSearch 中有什么方法可以做到这一点?
I'm looking into changing from Solr to ES.
One of the things I can't find info about is whether ES lets me define exclusion filters when faceting.
For example consider producttype
with values: A,B,C
which I want to facet on (i.e: show counts for). Also consider that the query is constrained to producttype: A
.
In this case Solr allows me to specify that I want to exclude the contraint producttype: A
from impacting faceting on producttype
. IOW, it displays counts on producttype
as if the constraint producttype: A
has not been applied.
How to do this in Solr see: http://wiki.apache.org/solr/SimpleFacetParameters > Tagging and excluding Filters
Is there any way to do this in ElasticSearch?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,你可以。
虽然您可以在查询 DSL 中使用过滤器,但搜索 API 还接受顶级
filter
参数,该参数用于在计算 Facet 后过滤搜索结果。例如:
1) 首先,创建索引,并且由于您希望将
product_type
视为枚举,因此将其设置为not_analyzed
:2) 索引一些文档(注意,文档 3 有不同的
product_name
):3) 搜索名称包含
foo
的产品(不包括文档 3,因此不包括product_type
C
),计算product_name
中包含foo
的所有文档的product_type
的构面,然后过滤搜索结果byproduct_type
==A
:4) 在
product_name
中搜索foo
,但计算所有的构面索引中的产品,通过指定global
参数:更新以回答来自OP的扩展问题:
您还可以将过滤器直接应用于每个方面 - 这些称为
facet_filters
。与之前的示例类似:
1) 创建索引:
2) 索引一些数据:
3) 搜索、过滤同时具有
type
==A
和color 的产品
==blue
,然后对除“其他”过滤器之外的每个属性运行构面:Yes you can.
While you can use filters within the query DSL, the search API also accepts a top-level
filter
parameter, which is used for filtering the search results AFTER the facets have been calculated.For example:
1) First, create your index, and because you want
product_type
to be treated as an enum, set it to benot_analyzed
:2) Index some docs (note, doc 3 has a different
product_name
):3) Perform a search for products whose name contains
foo
(which excludes doc 3 and thusproduct_type
C
), calculate facets forproduct_type
for all docs which havefoo
in theproduct_name
, then filter the search results byproduct_type
==A
:4) Perform a search for
foo
in theproduct_name
, but calculate facets for all products in the index, by specifying theglobal
parameter:UPDATE TO ANSWER THE EXPANDED QUESTION FROM THE OP:
You can also apply filters directly to each facet - these are called
facet_filters
.Similar example to before:
1) Create the index:
2) Index some data:
3) Search, filtering on products that have both
type
==A
andcolor
==blue
, then run facets on each attribute excluding, the "other" filter: