使用 elasticsearch 的邮件域的聚合计数
Aggregation count of mail domains using elastisearch
我的索引中有以下文档:
{
"name":"rakesh"
"age":"26"
"email":"rakesh@gmail.com"
}
{
"name":"sam"
"age":"24"
"email":"samjoe@elastic.com"
}
{
"name":"joseph"
"age":"26"
"email":"joseph@gmail.com"
}
{
"name":"genny"
"age":"24"
"email":"genny@hotmail.com"
}
现在我需要获取所有邮件域的数量。喜欢:
@gmail.com:2,
@hotmail.com:1,
@elastic.com:1
使用弹性搜索聚合。
我可以找到与给定查询相匹配的记录。但我需要对每个域进行计数。
在此先感谢您的帮助。
这可以通过创建一个仅包含电子邮件域名的子字段轻松实现。首先用合适的分析器创建索引:
PUT my_index
{
"settings": {
"index": {
"analysis": {
"analyzer": {
"email_domain_analyzer": {
"type": "pattern",
"pattern": "(.+)@",
"lowercase": true
}
}
}
}
},
"mappings": {
"doc": {
"properties": {
"email": {
"type": "text",
"fields": {
"domain": {
"type": "text",
"fielddata": true,
"analyzer": "email_domain_analyzer"
}
}
}
}
}
}
}
然后创建您的文档:
POST my_index/doc/_bulk
{ "index": {"_id": 1 }}
{ "name":"rakesh", "age":"26", "email":"rakesh@gmail.com" }
{ "index": {"_id": 2 }}
{ "name":"sam", "age":"24", "email":"samjoe@elastic.com" }
{ "index": {"_id": 3 }}
{ "name":"joseph", "age":"26", "email":"joseph@gmail.com" }
{ "index": {"_id": 4 }}
{ "name":"genny", "age":"24", "email":"genny@gmail.com" }
最后,您可以在 email.domain
字段上进行聚合,您将得到您所需要的:
POST my_index/_search
{
"size": 0,
"aggs": {
"domains": {
"terms": {
"field": "email.domain"
}
}
}
}
我的索引中有以下文档:
{
"name":"rakesh"
"age":"26"
"email":"rakesh@gmail.com"
}
{
"name":"sam"
"age":"24"
"email":"samjoe@elastic.com"
}
{
"name":"joseph"
"age":"26"
"email":"joseph@gmail.com"
}
{
"name":"genny"
"age":"24"
"email":"genny@hotmail.com"
}
现在我需要获取所有邮件域的数量。喜欢:
@gmail.com:2,
@hotmail.com:1,
@elastic.com:1
使用弹性搜索聚合。
我可以找到与给定查询相匹配的记录。但我需要对每个域进行计数。
在此先感谢您的帮助。
这可以通过创建一个仅包含电子邮件域名的子字段轻松实现。首先用合适的分析器创建索引:
PUT my_index
{
"settings": {
"index": {
"analysis": {
"analyzer": {
"email_domain_analyzer": {
"type": "pattern",
"pattern": "(.+)@",
"lowercase": true
}
}
}
}
},
"mappings": {
"doc": {
"properties": {
"email": {
"type": "text",
"fields": {
"domain": {
"type": "text",
"fielddata": true,
"analyzer": "email_domain_analyzer"
}
}
}
}
}
}
}
然后创建您的文档:
POST my_index/doc/_bulk
{ "index": {"_id": 1 }}
{ "name":"rakesh", "age":"26", "email":"rakesh@gmail.com" }
{ "index": {"_id": 2 }}
{ "name":"sam", "age":"24", "email":"samjoe@elastic.com" }
{ "index": {"_id": 3 }}
{ "name":"joseph", "age":"26", "email":"joseph@gmail.com" }
{ "index": {"_id": 4 }}
{ "name":"genny", "age":"24", "email":"genny@gmail.com" }
最后,您可以在 email.domain
字段上进行聚合,您将得到您所需要的:
POST my_index/_search
{
"size": 0,
"aggs": {
"domains": {
"terms": {
"field": "email.domain"
}
}
}
}