如何在Elasticearch中保存针对唯一ID的状态或状态转换

mm5n2pyu  于 2022-09-20  发布在  ElasticSearch
关注(0)|答案(1)|浏览(141)

我对ElasticSearch和解决方案的POC工作很陌生。我有一个想要跟踪票据状态更改的用例。票证ID是数据的唯一标识符。我想保存状态更改、状态更改的时间和票证上的备注。

实现这一目标的最佳方式是什么?

我将我的票证数据保存在具有如下定义的Map的索引中:

"mappings": {
    "properties":
        {
            "ticket_id": { "type": "keyword" },
            "Problem Description": { "type": "text" },
            "start_time" :{"type": "date","format":"yyyy-MM-dd HH:mm:ss"},
            "end_time" :{"type": "date","format":"yyyy-MM-dd HH:mm:ss"},
            "workflow_status":{"type": "text" }
        }
}
bxpogfeg

bxpogfeg1#

没有数据很难将问题形象化并解释解决方案,因此我创建了以下数据来更好地解释它:

索引样本数据

// below document doesn't marks the status to `done` hence just the `start_time` change with Description.

{
    "ticket_id" : "jira-001",
    "Problem_Description" : "this is first jira-001",
    "start_time" : "2022-09-14 05:30:00",
    "workflow_status" : "backlog"
}

// below document doesn't marks the status to `done` hence just the `start_time` change with Description.

{
    "ticket_id" : "jira-001",
    "Problem_Description" : "working on jira-001",
    "start_time" : "2022-09-14 07:30:00",
    "workflow_status" : "in-progress"
}

// below document marks the status to `done` hence `end_time` 
{
    "ticket_id" : "jira-001",
    "Problem_Description" : "completed jira-001",
    "start_time" : "2022-09-15 01:30:00",
    "end_time" : "2022-09-15 04:30:00",
    "workflow_status" : "done"
}

之后,当您想要获取我的示例中相同的ticket``jira-001的所有工作流时,您可以使用下面的查询来获取基于其start_time的排序

{
    "size": 10,
    "query": {
        "term": {
            "ticket_id": "jira-001"
        }
    },
    "sort": [
        {
            "start_time": "asc"
        }
    ]
}

这将给出下面的结果,希望这会有所帮助,并根据您的用例。

hits": [
            {
                "_index": "73706581",
                "_id": "1",
                "_score": null,
                "_source": {
                    "ticket_id": "jira-001",
                    "Problem_Description": "this is first jira-001",
                    "start_time": "2022-09-14 05:30:00",
                    "workflow_status": "backlog"
                },
                "sort": [
                    1663133400000
                ]
            },
            {
                "_index": "73706581",
                "_id": "2",
                "_score": null,
                "_source": {
                    "ticket_id": "jira-001",
                    "Problem_Description": "working on jira-001",
                    "start_time": "2022-09-14 07:30:00",
                    "workflow_status": "in-progress"
                },
                "sort": [
                    1663140600000
                ]
            },
            {
                "_index": "73706581",
                "_id": "3",
                "_score": null,
                "_source": {
                    "ticket_id": "jira-001",
                    "Problem_Description": "completed jira-001",
                    "start_time": "2022-09-15 01:30:00",
                    "end_time": "2022-09-15 04:30:00",
                    "workflow_status": "done"
                },
                "sort": [
                    1663205400000
                ]
            }
        ]

相关问题