弹性搜索组按前提条件和计数

时间:2019-12-25 09:02:11

标签: elasticsearch

在这种模式下,我有一些聊天记录: {   id:UUID   chatId:字符串,   日期:字符串|日期   文字:字串 } 另外,我有一些要在特定条件下计数的字符串(短语)。

我想获取短语的数量并获取与其中一个短语相对应的最后一条消息。

例如:

const phrases = ["hello to all", "dog & cat"]
const Chats = [
  {
    id: "UUID-1",
    chatId: "1",
    date: "1-1-2016",
    message: "hello world to all, hello to all, dog & cat 1"
  },
  {
    id: "UUID-2",
    chatId: "1",
    date: "1-1-2015",
    message: "hello world to all, hello to all, dog & cat 2"
  },
  {
    id: "UUID-3",
    chatId: "2",
    date: "1-1-2016",
    message: "dog & cat"
  },
  {
    id: "UUID-4",
    chatId: "2",
    date: "1-1-2016",
    message: "hello to all"
  },
  {
    id: "UUID-5",
    chatId: "3",
    date: "1-1-2015",
    message: "abc"
  }
]

对于上面的示例,我想得到结果:

[
  {
    chatId: 1,
    lastMessage: {
      id: "UUID-1",
      chatId: "1",
      date: "1-1-2016",
      message: "hello world to all, hello to all, dog & cat 1"
    },
    count: {
      “hello to all”: 2,
      “dog & cat” : 2
    }
  },
  {
    chatId: 2,
    lastMessage: {
      id: "UUID-3",
      chatId: "2",
      date: "1-1-2016",
      message: "dog & cat"
    },
    count: {
      “hello to all”: 1,
      “dog & cat” : 1
    }
  },
]

0 个答案:

没有答案