如何使 Elasticsearch 聚合只创建 1 个桶?
How to make Elasticsearch aggregation only create 1 bucket?
我有一个 Elasticsearch 索引,其中包含一个名为 "host" 的字段。我正在尝试向 Elasticsearch 发送查询以获取索引中主机的所有唯一值的列表。这是目前我能得到的最接近的:
{
"size": 0,
"aggs": {
"hosts": {
"terms": {"field": "host"}
}
}
}
哪个returns:
"buckets": [
{
"key": "04",
"doc_count": 201
},
{
"key": "cyn",
"doc_count": 201
},
{
"key": "pc",
"doc_count": 201
}
]
然而主机的实际名称是04-cyn-pc。我的理解是它将它们分成关键字,所以我尝试这样的事情:
{
"properties": {
"host": {
"type": "text",
"fields": {
"raw": {
"type": "text",
"analyzer": "keyword",
"fielddata": true
}
}
}
}
}
但是returnsillegal_argument_exception"reason": "Mapper for [host.raw] conflicts with existing mapping in other types:\n[mapper [host.raw] has different [index] values, mapper [host.raw] has different [analyzer]]"
您可能会说我是 Elasticsearch 的新手,任何帮助或指导都将非常有用,谢谢!
试试这个:
{
"properties": {
"host": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
}
}
}
}
如果您不指定映射,Elastic 会自动将字符串字段索引为文本和关键字类型。在您的示例中,如果您不希望对您的字段进行全文搜索分析,您应该将该字段的类型定义为关键字。所以你可以摆脱分析文本字段的负担。使用下面的映射,您可以在不更改聚合查询的情况下轻松解决问题。
"properties": {
"host": {
"type": "keyword"
}
}
我有一个 Elasticsearch 索引,其中包含一个名为 "host" 的字段。我正在尝试向 Elasticsearch 发送查询以获取索引中主机的所有唯一值的列表。这是目前我能得到的最接近的:
{
"size": 0,
"aggs": {
"hosts": {
"terms": {"field": "host"}
}
}
}
哪个returns:
"buckets": [
{
"key": "04",
"doc_count": 201
},
{
"key": "cyn",
"doc_count": 201
},
{
"key": "pc",
"doc_count": 201
}
]
然而主机的实际名称是04-cyn-pc。我的理解是它将它们分成关键字,所以我尝试这样的事情:
{
"properties": {
"host": {
"type": "text",
"fields": {
"raw": {
"type": "text",
"analyzer": "keyword",
"fielddata": true
}
}
}
}
}
但是returnsillegal_argument_exception"reason": "Mapper for [host.raw] conflicts with existing mapping in other types:\n[mapper [host.raw] has different [index] values, mapper [host.raw] has different [analyzer]]"
您可能会说我是 Elasticsearch 的新手,任何帮助或指导都将非常有用,谢谢!
试试这个:
{
"properties": {
"host": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
}
}
}
}
如果您不指定映射,Elastic 会自动将字符串字段索引为文本和关键字类型。在您的示例中,如果您不希望对您的字段进行全文搜索分析,您应该将该字段的类型定义为关键字。所以你可以摆脱分析文本字段的负担。使用下面的映射,您可以在不更改聚合查询的情况下轻松解决问题。
"properties": {
"host": {
"type": "keyword"
}
}