在使用terms facets时,经常想统计一个整个字段的值出现了多少次.如果用下面的消息发送请求:
{
"query": {
"bool": {
"must": [
{
"query_string": {
"default_field": "content",
"query": "风暴"
}
}
],
"must_not": [],
"should": []
}
},
"from": 0,
"size": 50,
"sort": [],
"facets": {
"sd3560531facet": {
"terms": {
"field": "siteName",
"all_terms": true
}
}
}
}
得出的统计结果是:
facets: {
province: {
_type: terms
missing: 0
total: 18
other: 0
terms: [
{
term: 区
count: 4
}
{
term: 论
count: 3
}
{
term: 讨
count: 3
}
{
term: 战
count: 2
}
{
term: 场
count: 2
}
{
term: 风
count: 1
}
{
term: 综
count: 1
}
{
term: 纪
count: 1
}
{
term: 合
count: 1
}
{
term: 本
count: 0
}
]
}
默认统计的是每个字出现的次数.不符合实际应用要求.
重新修改消息请求如下:
{
"query": {
"bool": {
"must": [
{
"query_string": {
"default_field": "content",
"query": "风暴"
}
}
],
"must_not": [],
"should": []
}
},
"from": 0,
"size": 50,
"sort": [],
"facets": {
"sd3560531facet": {
"terms": {
"script_field": "_source.siteName",
"all_terms": true
}
}
}
}
得出的结果为:
facets: {
province: {
_type: terms
missing: 0
total: 4
other: 0
terms: [
{
term: 战场讨论区
count: 2
}
{
term: 风纪区
count: 1
}
{
term: 综合讨论区
count: 1
}
]
}
}
表示符合条件的4条记录中,siteName字段里,战场讨论区有两条,风纪区有1条,综合讨论区有一条.
{
"query": {
"bool": {
"must": [
{
"query_string": {
"default_field": "content",
"query": "风暴"
}
}
],
"must_not": [],
"should": []
}
},
"from": 0,
"size": 50,
"sort": [],
"facets": {
"sd3560531facet": {
"terms": {
"field": "siteName",
"all_terms": true
}
}
}
}
得出的统计结果是:
facets: {
province: {
_type: terms
missing: 0
total: 18
other: 0
terms: [
{
term: 区
count: 4
}
{
term: 论
count: 3
}
{
term: 讨
count: 3
}
{
term: 战
count: 2
}
{
term: 场
count: 2
}
{
term: 风
count: 1
}
{
term: 综
count: 1
}
{
term: 纪
count: 1
}
{
term: 合
count: 1
}
{
term: 本
count: 0
}
]
}
默认统计的是每个字出现的次数.不符合实际应用要求.
重新修改消息请求如下:
{
"query": {
"bool": {
"must": [
{
"query_string": {
"default_field": "content",
"query": "风暴"
}
}
],
"must_not": [],
"should": []
}
},
"from": 0,
"size": 50,
"sort": [],
"facets": {
"sd3560531facet": {
"terms": {
"script_field": "_source.siteName",
"all_terms": true
}
}
}
}
得出的结果为:
facets: {
province: {
_type: terms
missing: 0
total: 4
other: 0
terms: [
{
term: 战场讨论区
count: 2
}
{
term: 风纪区
count: 1
}
{
term: 综合讨论区
count: 1
}
]
}
}
表示符合条件的4条记录中,siteName字段里,战场讨论区有两条,风纪区有1条,综合讨论区有一条.