如何查询elasticsearch获取嵌套属性

How to query elasticsearch to get nested property

我是 elasticsearch 的新手,我正在尝试为以下 json 数据组合一个查询(在 Kibana 中):

{
    "RepoCount": 2,
    "ThirdPartyRepoCount": 1,
    "RepoReadMeCount": 1,
    "date": "2019-10-25 12:02:41",
    "projects": [
        {
            "key": "GA",
            "id": 884,
            "name": "Company Archive",
            "description": "An archive for old dormant or abandoned git projects",
            "type": "NORMAL",
            "Repos": [
                {
                    "id": 28,
                    "name": "address",
                    "scmId": "git",
                    "NumberFiles": 319,
                    "HasReadMe": false,
                    "GitRepoUrl": "blablabla.git",
                    "LastCommitDate": "08/16/2013",
                },
                {
                    "id": 364,
                    "name": "api",
                    "scmId": "git",
                    "NumberFiles": 377,
                    "HasReadMe": false,
                    "GitRepoUrl": "blablabla.git",
                    "LastCommitDate": "07/01/2014",
                }
            ]
        }
    ]
}

我想编写查询以获取项目和 'Repos' 下的数据。 我已将 json 数据导入到名为 gitrepos 的索引中,并成功创建了一个索引模式。我试过这样的查询:

GET gitrepos/projects/_search
GET gitrepos/projects/REPOS/_search

但没有运气。 我唯一可以开始工作的是:
GET gitrepos
这只会把一切都还给我。
我想更深入地研究,但上面的方法不起作用。
这是怎么做到的?

您的索引名为 gitrepos,因此您的搜索端点是 GET /gitrepos/_search。索引中的每个文档都类似于您的上述文档;您可以 return 具有符合某些条件的 Repos 的文档。

GET gitrepos/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "projects.Repos.GitRepoUrl": "blablabla.git" }}
      ]
    }
  }
}

对于要匹配单个文档中多个字段的更复杂的搜索,您需要使用 nested query

如果您想直接搜索单个 Repo 文档,您需要在它们自己的索引中为每个元素编制索引。