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

Elasticsearch不同的过滤器值

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

Elasticsearch不同的过滤器值

这是

terms
聚合(文档)的工作。

您可以使用以下不同的

departments
值:

POST company/employee/_search{  "size":0,  "aggs": {    "by_departments": {      "terms": {        "field": "departments.name",        "size": 0 //see note 1      }    }  }}

在您的示例中,输出:

{   ...   "aggregations": {      "by_departments": {         "buckets": [ {    "key": "management", //see note 2    "doc_count": 2 }, {    "key": "accounts",    "doc_count": 1 }, {    "key": "it",    "doc_count": 1 }         ]      }   }}

另外两个注意事项:

  • 设置
    size
    为0会将最大存储桶数设置为Integer.MAX_VALUE。如果有太多
    departments
    不同的值,请不要使用它。
  • 您会看到这些键是
    terms
    分析
    departments
    值的结果。确保
    terms
    在映射为的字段上使用汇总
    not_analyzed

例如,使用我们的默认映射(

departments.name
是一个
analyzed
字符串),添加此员工:

{  "name": "Bill Gates",  "departments": [    {      "name": "IT"    },    {      "name": "Human Resource"    }  ]}

会导致这种结果:

{   ...   "aggregations": {      "by_departments": {         "buckets": [ {    "key": "it",    "doc_count": 2 }, {    "key": "management",    "doc_count": 2 }, {    "key": "accounts",    "doc_count": 1 }, {    "key": "human",    "doc_count": 1 }, {    "key": "resource",    "doc_count": 1 }         ]      }   }}

使用正确的映射:

POST company{  "mappings": {    "employee": {      "properties": {        "name": {          "type": "string"        },        "departments": {          "type": "object",          "properties": { "name": {   "type": "string",   "index": "not_analyzed" }          }        }      }    }  }}

相同的请求最终输出:

{   ...   "aggregations": {      "by_departments": {         "buckets": [ {    "key": "IT",    "doc_count": 2 }, {    "key": "Management",    "doc_count": 2 }, {    "key": "Accounts",    "doc_count": 1 }, {    "key": "Human Resource",    "doc_count": 1 }         ]      }   }}

希望这可以帮助!



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

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

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