居然是你

elasticsearch去重

Elasticsearch | 作者 qqq1234567 | 发布于2018年11月08日 | 阅读数:4178

类似这样的sql语句
select distinct(code) from (select * from user where age=7)怎么实现
已邀请:

rochy - rochy_he

赞同来自:

请参考 Cardinality Aggregation
https://www.elastic.co/guide/e ... .html

qqq1234567

赞同来自:

我是想问这种先查询再聚合的应该怎么写

rochy - rochy_he

赞同来自:

POST /sales/_search?size=0
{
"query" : {
"term" : { "user" : "kimchy" }
},
"aggs" : {
"type_count" : {
"cardinality" : {
"field" : "type"
}
}
}
}

qqq1234567

赞同来自:

聚合后怎么对文档排序
select distinct(code) from (select * from user where age=7) order by age这样的

qqq1234567

赞同来自:

我是做去重,不是做统计,所以没用cardinality,我是这样写的
"aggregations" : {
    "titleCard" : {
      "terms" : {
        "field" : "title"
      },
      "aggregations" : {
        "title_top" : {
          "top_hits" : {
            "sort" : [
              {
                "time" : {
                  "order" : "desc"
                }
              }
            ]
          }
        }
      }
    }
  }
只是需要在最后用其他字段排序

rochy - rochy_he

赞同来自:

如果不是统计 可以使用 Field Collapsing
具体参考:https://www.elastic.co/guide/e ... .html

qqq1234567

赞同来自:

那能不能用上面那这种方法去重后按时间进行排序呢

zz_hello

赞同来自:

你可以在你的第一个聚合里面再写一个时间的聚合,然后在第一个聚合里面按下面那个时间max的聚合排序。下面是例子。
GET logstash/_search
{
"size": 0,
"aggs": {
"extension": {
"terms": {
"field": "extension",
"size": 10,
"order": {
"time": "asc"
}
},
"aggs": {
"time": {
"max": {
"field": "timestamp"
}
}
}
}
}
}

要回复问题请先登录注册