ElasticSearch映射对分组文档进行折叠/执行操作的结果

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

有一个对话列表,每个对话都有一个消息列表。每个消息都有不同的字段和一个action字段。我们需要考虑的是,在对话的第一条消息中使用了动作A,在一些消息之后使用了动作A.1,过一会儿A.1.1,依此类推(有一个聊天机器人意图列表) )。

将对话的消息操作分组将类似于:A > A > A > A.1 > A > A.1 > A.1.1 ...

问题:

我需要使用ElasticSearch创建报告,该报告将返回每次对话的actions group;接下来,我需要对类似的actions groups进行分组并添加一个计数;最后会导致Map<actionsGroup, count>'A > A.1 > A > A.1 > A.1.1', 3

构建actions group,我需要消除每组重复项;我需要的不是A > A > A > A.1 > A > A.1 > A.1.1,而是A > A.1 > A > A.1 > A.1.1

我开始做的步骤

{
   "collapse":{
      "field":"context.conversationId",
      "inner_hits":{
         "name":"logs",
         "size": 10000,
         "sort":[
            {
               "@timestamp":"asc"
            }
         ]
      }
   },
   "aggs":{
   },
}

我接下来需要什么:

  1. 我需要在A > A.1 > A > A.1 > A.1.1之类的单个结果中映射折叠的结果。我已经看到,在这种情况下aggr可以在结果上使用scripts,并且可以创建一系列需要执行的操作,但是aggr可以对所有消息进行操作,不仅针对我折叠中的分组消息。是否可以在折叠内使用aggr或类似的解决方案?
  2. 我需要将所有折叠中的结果值(A > A.1 > A > A.1 > A.1.1)分组,添加一个计数并得出Map<actionsGroup, count>

Or:

  1. 使用conversationIdaggr字段将对话消息分组(我不知道该怎么做)
  2. 使用脚本迭代所有值并为每个对话创建actions group。 (不确定是否可行)
  3. 在所有值上使用另一个aggr并将重复项分组,返回Map<actionsGroup, count>

更新2:我设法取得了部分结果,但仍然有一个问题。请检查here我仍需要解决的问题。

更新1:添加一些额外的详细信息

映射:

"mappings":{
  "properties":{
     "@timestamp":{
        "type":"date",
        "format": "epoch_millis"
     }
     "context":{
        "properties":{
           "action":{
              "type":"keyword"
           },
           "conversationId":{
              "type":"keyword"
           }
        }
     }
  }
}

对话的示例文件:

Conversation 1.
{
    "@timestamp": 1579632745000,
    "context": {
        "action": "A",
        "conversationId": "conv_id1",
    }
},
{
    "@timestamp": 1579632745001,
    "context": {
        "action": "A.1",
        "conversationId": "conv_id1",
    }
},
{
    "@timestamp": 1579632745002,
    "context": {
        "action": "A.1.1",
        "conversationId": "conv_id1",
    }
}

Conversation 2.
{
    "@timestamp": 1579632745000,
    "context": {
        "action": "A",
        "conversationId": "conv_id2",
    }
},
{
    "@timestamp": 1579632745001,
    "context": {
        "action": "A.1",
        "conversationId": "conv_id2",
    }
},
{
    "@timestamp": 1579632745002,
    "context": {
        "action": "A.1.1",
        "conversationId": "conv_id2",
    }
}

Conversation 3.
{
    "@timestamp": 1579632745000,
    "context": {
        "action": "B",
        "conversationId": "conv_id3",
    }
},
{
    "@timestamp": 1579632745001,
    "context": {
        "action": "B.1",
        "conversationId": "conv_id3",
    }
}

预期结果:

{
    "A -> A.1 -> A.1.1": 2,
    "B -> B.1": 1
}
Something similar, having this or any other format.

由于我是Elasticsearch的新手,所以每个提示都值得欢迎。

elasticsearch collapse elasticsearch-aggregation elasticsearch-query
1个回答
0
投票

使用Terms aggregation中的脚本,我们可以在“ context.action”的第一个字符上创建存储桶。使用类似的术语子聚合我们可以在父存储桶(例如A-> A.1-> A.1.1 ...

下获得所有“ context.action”

查询:

{
  "size": 0,
  "aggs": {
    "conversations": {
      "terms": {
        "script": {
          "source": "def term=doc['context.action'].value; return term.substring(0,1);" 
--->  returns first character ex A,B,C etc
        },
        "size": 10
      },
      "aggs": {
        "sub_conversations": {
          "terms": {
            "script": {
              "source": "if(doc['context.action'].value.length()>1) return doc['context.action'];"--> All context.action under [A], length check to ignore [A]
            },
            "size": 10
          }
        },
        "count": {
          "cardinality": {
            "script": {
              "source": "if(doc['context.action'].value.length()>1) return doc['context.action'];"--> count of all context.action under A
            }
          }
        }
      }
    }
  }
}

由于进行弹性搜索,因此无法合并不同的文档。您将需要通过在聚合存储桶上进行迭代来获得客户端的组合密钥。

结果:

  "aggregations" : {
    "conversations" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "A",
          "doc_count" : 6,
          "sub_conversations" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "A.1",
                "doc_count" : 2
              },
              {
                "key" : "A.1.1",
                "doc_count" : 2
              }
            ]
          },
          "count" : {
            "value" : 2
          }
        },
        {
          "key" : "B",
          "doc_count" : 2,
          "sub_conversations" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "B.1",
                "doc_count" : 1
              }
            ]
          },
          "count" : {
            "value" : 1
          }
        }
      ]
    }
  }
© www.soinside.com 2019 - 2024. All rights reserved.