Solr Elasticsearch:是否可以在镶嵌面时排除过滤器?(就像在索尔)
我正在考虑从Solr变为ES。 我找不到的信息之一是ES是否允许我在刻面时定义排除过滤器Solr Elasticsearch:是否可以在镶嵌面时排除过滤器?(就像在索尔),solr,
elasticsearch,faceted-search,Solr,
elasticsearch,Faceted Search,我正在考虑从Solr变为ES。 我找不到的信息之一是ES是否允许我在刻面时定义排除过滤器 例如考虑代码>产品类型< /代码>值: A、B、C ,我想在这方面进行描述(即:显示计数)。还要考虑查询被限制为产品类型:< /代码> . 在本例中,Solr允许我指定我要排除containtproducttype:A对producttype上的镶嵌面产生影响。此时,它会显示producttype上的计数,就好像约束producttype:A尚未应用一样 如何在Solr中执行此操作请参见:>标记和排除过滤器
例如考虑代码>产品类型< /代码>值:<代码> A、B、C <代码>,我想在这方面进行描述(即:显示计数)。还要考虑查询被限制为<代码>产品类型:< /代码> . 在本例中,Solr允许我指定我要排除containt
producttype:A
对producttype
上的镶嵌面产生影响。此时,它会显示producttype
上的计数,就好像约束producttype:A
尚未应用一样
如何在Solr中执行此操作请参见:>标记和排除过滤器
在ElasticSearch中有什么方法可以做到这一点吗 是的,你可以
虽然您可以在查询DSL中使用过滤器,但搜索API还接受顶级的过滤器
参数,用于在计算面后过滤搜索结果
例如:
1) 首先,创建索引,由于希望将产品类型
视为枚举,请将其设置为未分析
:
curl -XPUT 'http://127.0.0.1:9200/my_index/?pretty=1' -d '
{
"mappings" : {
"product" : {
"properties" : {
"product_type" : {
"index" : "not_analyzed",
"type" : "string"
},
"product_name" : {
"type" : "string"
}
}
}
}
}
'
2) 为一些文档编制索引(注意,文档3具有不同的产品名称
):
3) 搜索名称中包含foo
(不包括doc 3,因此product\u type
C
)的产品,计算product\u type
中包含foo
的所有文档的,然后按产品类型
==A
过滤搜索结果:
curl -XGET 'http://127.0.0.1:9200/my_index/product/_search?pretty=1' -d '
{
"query" : {
"text" : {
"product_name" : "foo"
}
},
"filter" : {
"term" : {
"product_type" : "A"
}
},
"facets" : {
"product_type" : {
"terms" : {
"field" : "product_type"
}
}
}
}
'
# {
# "hits" : {
# "hits" : [
# {
# "_source" : {
# "product_type" : "A",
# "product_name" : "foo bar"
# },
# "_score" : 0.19178301,
# "_index" : "my_index",
# "_id" : "1",
# "_type" : "product"
# }
# ],
# "max_score" : 0.19178301,
# "total" : 1
# },
# "timed_out" : false,
# "_shards" : {
# "failed" : 0,
# "successful" : 5,
# "total" : 5
# },
# "facets" : {
# "product_type" : {
# "other" : 0,
# "terms" : [
# {
# "count" : 1,
# "term" : "B"
# },
# {
# "count" : 1,
# "term" : "A"
# }
# ],
# "missing" : 0,
# "_type" : "terms",
# "total" : 2
# }
# },
# "took" : 3
# }
4) 在product_name
中搜索foo
,但通过指定global
参数计算索引中所有产品的面:
# [Wed Jan 18 17:15:09 2012] Protocol: http, Server: 192.168.5.10:9200
curl -XGET 'http://127.0.0.1:9200/my_index/product/_search?pretty=1' -d '
{
"query" : {
"text" : {
"product_name" : "foo"
}
},
"filter" : {
"term" : {
"product_type" : "A"
}
},
"facets" : {
"product_type" : {
"global" : 1,
"terms" : {
"field" : "product_type"
}
}
}
}
'
# [Wed Jan 18 17:15:09 2012] Response:
# {
# "hits" : {
# "hits" : [
# {
# "_source" : {
# "product_type" : "A",
# "product_name" : "foo bar"
# },
# "_score" : 0.19178301,
# "_index" : "my_index",
# "_id" : "1",
# "_type" : "product"
# }
# ],
# "max_score" : 0.19178301,
# "total" : 1
# },
# "timed_out" : false,
# "_shards" : {
# "failed" : 0,
# "successful" : 5,
# "total" : 5
# },
# "facets" : {
# "product_type" : {
# "other" : 0,
# "terms" : [
# {
# "count" : 1,
# "term" : "C"
# },
# {
# "count" : 1,
# "term" : "B"
# },
# {
# "count" : 1,
# "term" : "A"
# }
# ],
# "missing" : 0,
# "_type" : "terms",
# "total" : 3
# }
# },
# "took" : 4
# }
更新以回答OP中的扩展问题:
您还可以将过滤器直接应用于每个方面-这些被称为facet\u过滤器
与之前类似的示例:
1) 创建索引:
curl -XPUT 'http://127.0.0.1:9200/my_index/?pretty=1' -d '
{
"mappings" : {
"product" : {
"properties" : {
"color" : {
"index" : "not_analyzed",
"type" : "string"
},
"name" : {
"type" : "string"
},
"type" : {
"index" : "not_analyzed",
"type" : "string"
}
}
}
}
}
'
2) 索引一些数据:
curl -XPUT 'http://127.0.0.1:9200/my_index/product/1?pretty=1' -d '
{
"color" : "red",
"name" : "foo bar",
"type" : "A"
}
'
curl -XPUT 'http://127.0.0.1:9200/my_index/product/2?pretty=1' -d '
{
"color" : [
"red",
"blue"
],
"name" : "foo bar",
"type" : "B"
}
'
curl -XPUT 'http://127.0.0.1:9200/my_index/product/3?pretty=1' -d '
{
"color" : [
"green",
"blue"
],
"name" : "bar",
"type" : "C"
}
'
3) 搜索、筛选同时具有类型
==A
和颜色
=蓝色
的产品,然后在每个属性上运行面,不包括“其他”过滤器:
谢谢在这个简单的例子中,它是有效的。如果我有两个属性,例如:productcategory
和color
,怎么样。我想在这两个属性上刻面,并排除我正在筛选的属性上的任何过滤器集。因此productcategory
上的刻面排除任何productcategory
过滤器,而color
上的刻面排除任何color
过滤器。在这种情况下,使facets全局化是行不通的(我相信),因为我希望productcategory
上的facets考虑到任何可能的color
-过滤器,反之亦然。顺便说一句,我意识到这是一个不同的问题。我扩展了上面的答案,以演示如何了解facet_过滤器的存在。我是否正确理解在方面上指定facet\u过滤器将覆盖(全局)过滤器集?我本以为我需要在facet上设置global:1
,并指示facet忽略全局设置的过滤器。facet的范围可以是与“main”查询匹配的文档,也可以是“global”(即索引/类型中的所有文档).facet
\u过滤器`只影响该facet,无论它使用什么范围。顺便说一句,您可以在此处阅读有关facet的更多信息:
curl -XPUT 'http://127.0.0.1:9200/my_index/product/1?pretty=1' -d '
{
"color" : "red",
"name" : "foo bar",
"type" : "A"
}
'
curl -XPUT 'http://127.0.0.1:9200/my_index/product/2?pretty=1' -d '
{
"color" : [
"red",
"blue"
],
"name" : "foo bar",
"type" : "B"
}
'
curl -XPUT 'http://127.0.0.1:9200/my_index/product/3?pretty=1' -d '
{
"color" : [
"green",
"blue"
],
"name" : "bar",
"type" : "C"
}
'
curl -XGET 'http://127.0.0.1:9200/my_index/product/_search?pretty=1' -d '
{
"filter" : {
"and" : [
{
"term" : {
"color" : "blue"
}
},
{
"term" : {
"type" : "A"
}
}
]
},
"facets" : {
"color" : {
"terms" : {
"field" : "color"
},
"facet_filter" : {
"term" : {
"type" : "A"
}
}
},
"type" : {
"terms" : {
"field" : "type"
},
"facet_filter" : {
"term" : {
"color" : "blue"
}
}
}
}
}
'
# [Wed Jan 18 19:58:25 2012] Response:
# {
# "hits" : {
# "hits" : [],
# "max_score" : null,
# "total" : 0
# },
# "timed_out" : false,
# "_shards" : {
# "failed" : 0,
# "successful" : 5,
# "total" : 5
# },
# "facets" : {
# "color" : {
# "other" : 0,
# "terms" : [
# {
# "count" : 1,
# "term" : "red"
# }
# ],
# "missing" : 0,
# "_type" : "terms",
# "total" : 1
# },
# "type" : {
# "other" : 0,
# "terms" : [
# {
# "count" : 1,
# "term" : "C"
# },
# {
# "count" : 1,
# "term" : "B"
# }
# ],
# "missing" : 0,
# "_type" : "terms",
# "total" : 2
# }
# },
# "took" : 3
# }