Elasticsearch:在分面时排除过滤器可能吗? (就像在 Solr 中一样)

2023-12-28

我正在考虑从 Solr 更改为 ES。 我找不到相关信息的一件事是 ES 是否允许我在分面时定义排除过滤器。

例如考虑producttype具有值:A,B,C我想关注这一点(即:显示计数)。还要考虑查询被限制为producttype: A.

在这种情况下,Solr 允许我指定我想要排除约束producttype: A影响刻面producttype。 IOW,它显示计数producttype就好像约束producttype: A尚未应用。

如何在 Solr 中执行此操作请参阅:http://wiki.apache.org/solr/SimpleFacetParameters http://wiki.apache.org/solr/SimpleFacetParameters> 标记和排除过滤器

在 ElasticSearch 中有什么办法可以做到这一点吗?


是的你可以。

虽然您可以在查询 DSL 中使用过滤器,但搜索 API 还接受顶级filter参数,用于在计算facet之后过滤搜索结果。

例如:

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(不包括 doc 3,因此product_type C),计算面product_type对于所有具有foo in the 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 in the product_name,但通过指定来计算索引中所有产品的构面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_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==Aand 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
# }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Elasticsearch:在分面时排除过滤器可能吗? (就像在 Solr 中一样) 的相关文章

  • solr 中的文本字段排序

    我正在使用 solr 3 4 并希望 solr 搜索结果在文本字段上排序 如何实现像 int 自然排序一样对文本字段进行排序 有没有办法在查询时将文本字段转换为int 我的排序字段是字符串类型 我希望它在排序时表现得像 int 字段 我无法
  • yii2 作曲家更新致命错误

    当我更新我的作曲家以添加yii2 solr扩展我的项目时 我遇到如下错误 The yiisoft yii2 composer plugin requires composer plugin api 1 0 0 this WILL break
  • Elasticsearch 对字符串排序未返回预期结果

    当对包含多个单词的字符串字段进行排序时 Elasticsearch 会拆分字符串值并使用最小值或最大值作为排序值 即 当对值为 老虎之眼 的字段进行升序排序时 排序值为 Eye 当按降序排序时 排序值为 Tiger 假设我的索引中有 老虎之
  • 使elasticsearch中的所有对象嵌套对象

    是否可以让elasticsearch中的所有嵌套对象自动映射到默认嵌套的类型 而不是对象 是的 您可以使用以下方法来做到这一点动态模板 https www elastic co guide en elasticsearch referenc
  • 在ElasticSearch中搜索没有时间的日期字段值

    我的数据中有一个日期字段为 type date format dateOptionalTime 现在我的日期字段和值是 INITIAL EXTRACT DATE 2015 04 02T06 47 57 78 05 30 在搜索时 我仅根据
  • 如何在弹性搜索(aws)中存储日期范围数据并搜索范围?

    我正在尝试在弹性搜索中存储酒店房间可用性 然后我需要 搜索从某个日期到另一个日期可用的房间 我想出了 存储数据以确保可用性的两种方式 如下 这里可用性字典存储了所有日期 每个日期键的值是 true 或 false 代表其可用 那天与否 id
  • SolrNet:过滤查询时保留 Facet 计数

    当我查询时 我收到以下方面 Field1 Key Best Facet 1 Value 999 Key Best Facet 2 Value 999 Field2 Key Second Best Facet 1 Value 421 Key
  • Elasticsearch 崩溃后无法恢复

    磁盘空间不足 导致 Elasticsearch 分片崩溃 三个节点现在为红色 两个节点已恢复 它们的状态为黄色 ES 的 CPU 利用率为 150 内存利用率很高 正在尝试恢复它们 但似乎存在一些版本匹配冲突 我清理了磁盘空间并删除了分片的
  • 在 ElasticSearch 7+ 中,如何搜索所有文本字段?

    我想在 Elasticsearch 7 3 中存储的文档中搜索单词 我希望在以前版本的 Elasticsearch 上运行的一个示例是 query bool must match all oliver must not should fro
  • Solr 4.0 中的 BaseTokenFilterFactory 去哪儿了?

    用于创建您自己的标记和字符过滤器的 Solr 文档说明如下 http wiki apache org solr AnalyzersTokenizersTokenFilters Specifying an Analyzer in the sc
  • ckan本地安装,solr JSP支持未配置500错误

    我正在尝试使用 Ubuntu 14 04 LTS 在本地计算机上安装 CKAN 我按照从找到的源安装的说明进行操作here http docs ckan org en latest maintaining installing instal
  • 随着索引和文档数量恒定,elasticsearch 批量索引会随着时间的推移而变慢

    我遇到了使用 NET NEST 客户端和 ElasticSearch 进行批量索引的性能随着时间的推移 索引数量和文档数量恒定而降低的情况 我们正在奔跑ElasticSearch Version 0 19 11 JVM 23 5 b02在具
  • 局部敏感哈希 - Elasticsearch

    有没有允许在 Elasticsearch 上使用 LSH 的插件 如果是的话 您能否指出该位置并告诉我如何使用它 谢谢 编辑 我发现ES使用了MinHash插件 我怎样才能用这个来比较文件呢 查找重复项的最佳设置是什么 有一个Elastic
  • Solr MoreLikeThis 不适用于多个分片?

    我在 SolrCloud 中有 5 个节点集群 每个节点有 2 个分片 Solr版本 6 3 0 现在 当我运行 mlt 查询时 它仅返回每个节点的结果 并且不会将它们分布在所有分片 节点上 即 没有给出任何结果 给出结果 我什至尝试将其指
  • 从 App Engine 连接到 Kubernetes 引擎

    我们希望使用应用程序引擎灵活的流程来更新位于 Google Kubernetes Engine 上的 ElasticSearch 索引 我们需要通过 http s 地址连接到 ElasticSearch 推荐的方法是什么 我们不想将集群暴露
  • 需要在 java api 中的 Solr 搜索中搜索文本及其周围的几行

    我正在使用 solr 7 7 2 并且我使用 solrj 在 Solr 中编写了一个 Java 程序 该程序在一个巨大的文本文件中搜索单词 我使用以下代码来显示代表整个文本的搜索结果 SolrQuery params new SolrQue
  • ElasticSearch 定义自定义映射与默认“_doc”映射冲突

    尝试创建自定义映射类型时会发生此问题 为第一个插入弹性创建自定义映射后想要创建 doc映射类型和冲突就发生在这里 第一步我创建一个映射 mappings properties field1 type keyword field2 type
  • 在弹性搜索中使用 GET/POST 时的不同结果

    我正在通过 Elastic Search Head 插件尝试弹性搜索 当我通过 POST 提交查询时 结果符合预期 但是 当我使用 GET 尝试相同的查询时 我总是会返回索引中的所有值 那么 如何通过 GET 将查询传递到弹性搜索服务器 以
  • 在Windows Xampp上安装和使用elasticsearch php客户端

    我下载的是elasticsearch 5 1 1 zip来自https www elastic co downloads elasticsearch https www elastic co downloads elasticsearch
  • ElasticSearch - 仅获取与搜索响应中所有顶级字段匹配的嵌套对象

    假设我有以下文档 id 1 name xyz users name abc surname def name xyz surname wef name defg surname pqr 我只想获取与搜索响应中的所有顶级字段匹配的嵌套对象 我

随机推荐