Elasticsearch不同的过滤器值

时间:2014-11-19 15:42:09

标签: elasticsearch filter distinct nosql

我在elasticsearch中有一个大型文档存储,并希望检索不同的过滤器值以便在HTML下拉列表中显示。

一个例子就像是

[
    {
        "name": "John Doe",
        "deparments": [
            {
                "name": "Accounts"
            },
            {
                "name": "Management"
            }
        ]
    },
    {
        "name": "Jane Smith",
        "deparments": [
            {
                "name": "IT"
            },
            {
                "name": "Management"
            }
        ]
    }
]

下拉列表应包含部门列表,即IT,帐户和管理。

有些人请指出我正确的方向从弹性搜索中检索一个不同的部门列表吗?

由于

1 个答案:

答案 0 :(得分:7)

这是terms聚合(documentation)的作业。

您可以拥有不同的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.nameanalyzed字符串),添加此员工:

{
  "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
            }
         ]
      }
   }
}

希望这有帮助!