首页 > 解决方案 > 显示弹性搜索结果时如何将_index和_type分组到_source的父级

问题描述

假设我需要像下面那样查询多个索引和文档,我想将 _source 元素分组到他们自己的 _index,_type 作为父级,但不知何故,它给了每个索引,每个块的类型,我很难管理结果

GET /index_1,index_2,hockey,bookdb_index/_search?size=200

        {
        "_index": "hockey",
        "_type": "player",
        "_id": "10",
        "_score": 1,
        "_source": {
            "first": "mikael",
            "last": "backlund",
            "goals": [
                3,
                15,
                13
            ],
            ...
        }
    },
    {
        "_index": "bookdb_index",
        "_type": "book",
        "_id": "2",
        "_score": 1,
        "_source": {
            "title": "Taming Text:...
        }
    },

如何将索引和类型分组为父元素,而 _source 是子元素

"hockey": {
    "player": [{
       "first": "mikael",
        "last": "backlund",
        "goals": [
            3,
            15,
            13
        ] }
      }, { "first": ...

      }]
}, 
"bookdb_index": { ...

标签: elasticsearch

解决方案


我为此目的创建了客户端等。

Nginx 弹性客户端

它是在 nginx.conf 中编写的纯弹性搜索查询,上游到弹性服务器,您可以选择index_docs获得您想要的结果。有关更多信息,请参阅自述文件。

 location /test {
    elastic_pass http://elastic_upstream;
    elastic_send POST /testindex/testdoc/_search?size=100 index_docs;

    elastic_query '{"query":
       {
        "match_all": {}
       }
    }';
 }

推荐阅读