按字段对结果分组并排序

plupiseo  于 2021-07-13  发布在  ElasticSearch
关注(0)|答案(0)|浏览(189)

我有一个带有Map的示例索引:

released_at: {
  type: "date",
},
name: {
  type: "text",
  analyzer: "analyzer_startswith",
},

和自定义分析器 analyzer_startwith :

analyzer_startswith: {
  tokenizer: "keyword",
  filter: "lowercase",
},

通过它我可以搜索 name 以短语开头:

{
    "query": {
        "match_phrase_prefix": {
            "name": "for"
        }
    }
}

样本结果(字段 name ):

[
  "Force of Vigor",
  "Former",
  "Forest",
  "Force of Rage",
  "Forest",
]

现在,我需要“分组”重复和排序 released_at 但不改变结果顺序,即 Forest 文件应按列表的第一个位置进行分组和排序(之后 Former ).
所以如果我们有关于结构的结果:

[
  {
    "name": "Force of vigor",
    "released_at": "2020-12-03"
  },
  {
    "name": "Former",
    "released_at": "2004-06-03"
  },
  {
    "name": "Forest",
    "released_at": "2021-04-25"
  },
  {
    "name": "Force of Rage",
    "released_at": "2020-12-03"
  },
  {
    "name": "Forest",
    "released_at": "2021-01-01"
  }
]

应该是:

[
  {
    "name": "Force of vigor",
    "released_at": "2020-12-03"
  },
  {
    "name": "Former",
    "released_at": "2004-06-03"
  },
  {
    "name": "Forest",
    "released_at": "2021-01-01"
  },
  {
    "name": "Forest",
    "released_at": "2021-04-25"
  },
  {
    "name": "Force of Rage",
    "released_at": "2020-12-03"
  }
]

因此,第一次出现的原始位置没有改变,但记录已经被排序。
你知道怎么做吗?抱歉,我不知道从哪里开始:/

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题