弹性搜索组,并获得第一记录

问题描述 投票:0回答:1

我是Elastic Search的新手,我想在弹性搜索中创建一个查询,该查询的工作方式类似于按SQL分组。这是我的SQL查询

SELECT top 100  *
FROM (
SELECT ROW_NUMBER()
OVER(PARTITION BY columnA
ORDER BY columnB) AS StRank, *
FROM table where columnA in ('a','b','c') ) n
WHERE StRank IN (1) 
GO

我希望按列A进行单个行分组,按列B进行排序

sql elasticsearch group-by elasticsearch-5
1个回答
0
投票

您可以使用collapse

允许根据字段值折叠搜索结果。的折叠是通过仅选择每个文档中排在最前面的文档来完成的折叠键

{
  "query": {
    "bool": {
      "filter": {- -> filter doesn't calculate score(if score needed use must)
        "terms": {
          "columnA": ["a","b","c"]
        }
      }
    }
  },
  "collapse": {      --> top 1 Group by column A
    "field": "columnA"
  },
  "sort": [          --> sort 
    {
      "columnB": {
        "order": "desc"
      }
    }
  ]
}
© www.soinside.com 2019 - 2024. All rights reserved.