Elasticsearch - 如何匹配字符串中的数字范围

Elasticsearch - How to match number range in string

我想编写一个查询来查找包含名称和数字 +/- 5 的日志事件。因为我不知道这个日志事件的确切结构,所以我无法单独提取数字诠释领域。

我的查询目前看起来像这样。

    baseQuery = {
            "query": {
                    "bool": {
                    "must": [
                            {"match": {"sim_session" : "session_id"}},
                            {"match": {"event" : "putFilenameHere"}},
                            {"bool": {
                                    "should": [
                                            {"match_phrase": {"event" : "Name"}},
                                            #Search for the line in the event text
                                            {"match": {"event" : "42"}}
                                    ],
                                    "minimum_should_match" : 2
                            }}
                    ]
                    }
            }
            }

所以我可以找到包含 "Name" 和数字“42”(被视为字符串)的事件,但我想找到包含 "Name" 和 37 或38 或 39 或 40 或 41 或 42 或 43 或 44 或 45 或 46 或 47。 有没有更聪明的方法来实现这一点,然后用不同的数字执行同一个查询 11 次?

执行该查询的脚本写在python中。

感谢

您可以对要查询的范围进行预处理:

  1. 如果您的字段 event 是文本,您的查询字符串将被自动标记化并使用每个数字标记进行查询:
"match": {
  "event": "37 38 39 40 41 42 43 44 45"
}
  1. 如果您的字段 event 是关键字,则使用术语查询:
"terms": {
  "event": ["37", "38", "39", "40", "41", "42", "43", "44", "45"]
}