如何在 kibana 中搜索嵌套键

How to search for a nested key in kibana

我有这样的 kibana 文档

{
  "_index": "echo.caspian-test.2020-06-11.idx.2",
  "_type": "status",
  "_id": "01754abe95fd084495da20646194fdf7",
  "_score": 1,
  "_source": {
    "applicationVersion": "9f80e49dea1c647fa1baf2e70665aba3a74158eb",
    "echoClientVersion": "1.5.1",
    "echoMetadata": {
      "transportType": "echo"
    },
    "dataCenter": "hdc-digital-non-prod",
    "echoLoggerVersion": "EchoLogbackAppender-1.5.1",
    "host": "e22ab1e4-9256-438b-5855-ad04",
    "type": "INFO",
    "message": "AddUpdate process method ends",
    "messageDetail": {
      "logger": "com.kroger.cxp.app.transformer.processor.AddUpdateTransformerImpl",
      "thread": "DispatchThread: [com.ibm.mq.jmqi.remote.impl.RemoteSession[:/1f6e1b6c][connectionId=414D5143514D2E4150504C2E54455354967C7F5F0407B82E]]"
    },
    "routingKey": "caspian-test",
    "timestamp": "1603276805250"
  },
  "fields": {
    "timestamp": [
      "2020-10-21T10:40:05.250Z"
    ]
  }
}

我需要搜索具有特定 connectionId 的所有文档,该 connectionId 存在于

"messageDetail": { "logger": "com.kroger.cxp.app.transformer.processor.AddUpdateTransformerImpl", "thread": "DispatchThread: [com.ibm.mq.jmqi.remote.impl.RemoteSession[:/1f6e1b6c][connectionId=414D5143514D2E4150504C2E54455354967C7F5F0407B82E]]" }

我该怎么做。我试过搜索 messageDetail.thread=%$CONNECTION_ID% 但没成功

您需要在搜索查询中添加 nested path 才能使其生效,并且您的 messageDetail 必须是 nested datatype,如下所示

{
    "query": {
        "nested": {
            "path": "messageDetail", --> note this
            "query": {
                "bool": {
                    "must": [
                        {
                            "match": {
                                "messageDetail. thread": "CONNECTION_ID"
                            }
                        }
                    ]
                }
            }
        }
    }
}

添加带有映射、搜索查询和结果的工作示例

索引映射

{
    "mappings": {
        "properties": {
            "messageDetail": {
                "type" : "nested"
            }
        }
    }
}

索引示例文档

{
    "applicationVersion": "9f80e49dea1c647fa1baf2e70665aba3a74158eb",
    "echoClientVersion": "1.5.1",
    "echoMetadata": {
        "transportType": "echo"
    },
    "dataCenter": "hdc-digital-non-prod",
    "echoLoggerVersion": "EchoLogbackAppender-1.5.1",
    "host": "e22ab1e4-9256-438b-5855-ad04",
    "type": "INFO",
    "message": "AddUpdate process method ends",
    "messageDetail": {
        "logger": "com.kroger.cxp.app.transformer.processor.AddUpdateTransformerImpl",
        "thread": "DispatchThread: [com.ibm.mq.jmqi.remote.impl.RemoteSession[:/1f6e1b6c][connectionId=414D5143514D2E4150504C2E54455354967C7F5F0407B82E]]"
    },
    "routingKey": "caspian-test",
    "timestamp": "1603276805250"
}

和搜索查询

{
    "query": {
        "nested": {
            "path": "messageDetail",
            "query": {
                "bool": {
                    "must": [
                        {
                            "match": {
                                "messageDetail.thread": "DispatchThread"
                            }
                        }
                    ]
                }
            }
        }
    }
}

并搜索资源

"hits": [
            {
                "_index": "nestedmsg",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.2876821,
                "_source": {
                    "applicationVersion": "9f80e49dea1c647fa1baf2e70665aba3a74158eb",
                    "echoClientVersion": "1.5.1",
                    "echoMetadata": {
                        "transportType": "echo"
                    },
                    "dataCenter": "hdc-digital-non-prod",
                    "echoLoggerVersion": "EchoLogbackAppender-1.5.1",
                    "host": "e22ab1e4-9256-438b-5855-ad04",
                    "type": "INFO",
                    "message": "AddUpdate process method ends",
                    "messageDetail": {
                        "logger": "com.kroger.cxp.app.transformer.processor.AddUpdateTransformerImpl",
                        "thread": "DispatchThread: [com.ibm.mq.jmqi.remote.impl.RemoteSession[:/1f6e1b6c][connectionId=414D5143514D2E4150504C2E54455354967C7F5F0407B82E]]"
                    },
                    "routingKey": "caspian-test",
                    "timestamp": "1603276805250"
                }
            }
        ]