栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Elasticsearch:排除筛选器时可以进行分面吗?(例如在Solr中)

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Elasticsearch:排除筛选器时可以进行分面吗?(例如在Solr中)

是的你可以。

虽然您可以在查询DSL中使用过滤器,但是搜索API也接受一个顶级

filter
参数,该参数用于在计算构面之后过滤搜索结果。

例如:

1)首先,创建您的索引,并且由于您希望

product_type
将其视为枚举,因此将其设置为
not_analyzed

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具有不同的

product_name
):

curl -XPUT 'http://127.0.0.1:9200/my_index/product/1?pretty=1'  -d '{   "product_type" : "A",   "product_name" : "foo bar"}'curl -XPUT 'http://127.0.0.1:9200/my_index/product/2?pretty=1'  -d '{   "product_type" : "B",   "product_name" : "foo bar"}'curl -XPUT 'http://127.0.0.1:9200/my_index/product/3?pretty=1'  -d '{   "product_type" : "C",   "product_name" : "bar"}'

3)执行的产品名称中包含的搜索

foo
(其不包括文档3,因此
product_type

C
),计算小面为
product_type
对于具有所有文档
foo
product_name
,然后过滤通过搜索结果
product_type
==
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),用于进行搜索

foo
product_name
,但计算小面为索引中的所有产品,通过指定
global
参数:

# [Wed Jan 18 17:15:09 2012] Protocol: http, Server: 192.168.5.10:9200curl -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_filters

与之前类似的示例:

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)搜索并过滤同时具有

type
==
A
color
==的产品
blue
,然后对每个属性(不包括“其他”过滤器)运行构面:

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# }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/417615.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号