我们有一些员工,需要找到那些尚未进入生日或在1963年3月1日之前出生的员工:
{
"query": {
"bool": {
"should": [
{
"bool": {
"must_not": [{ "exists": { "field": "birthday" } }]
}
},
{
"bool": {
"filter": [{ "range": {"birthday": { "lte": 19630301 }} }]
}
}
]
}
}
}
我们现在需要得到不同的名字……我们只需要1个Jason或1个Susan等。我们如何对“名字”字段应用不同的过滤器,同时仍然像上面一样过滤生日?我试过了:
{
"query": {
"bool": {
"should": [
{
"bool": {
"must_not": [
{
"exists": {
"field": "birthday"
}
}
]
}
},
{
"bool": {
"filter": [
{
"range": {
"birthday": {
"lte": 19630301
}
}
}
]
}
}
]
}
},
"aggs": {
"uniq_gender": {
"terms": {
"field": "name"
}
}
},
"from": 0,
"size": 25
}
但是只要用重复的Jason和Susans就可以得到结果。在底部,它会告诉我有10个寿司和12个贾森。不知道如何得到独特的。
编辑:
我的地图很简单。名称字段不需要是关键字…可以是文本或其他任何内容,因为它只是在查询中返回的字段。
{
"mappings": {
"birthdays": {
"properties": {
"name": {
"type": "keyword"
},
"birthday": {
"type": "date",
"format": "basic_date"
}
}
}
}
}