如何聚合和展平Elasticsearch内部命中结果?

时间:2018-08-21 07:35:47

标签: elasticsearch elasticsearch-aggregation

我试图特别搜索文档的nested字段,并仅使用inner_hits功能返回该嵌套子文档的数据。我尝试使用composite aggregation,但无法实现。

数据:

{
    "name": "Foo",
    "distributor": [
        {
            "id":"11",
            "address": "some addr",
            "note":"A company"
        },
        {
            "id":"12",
            "address": "XYZ",
            "note":"B industry"
        }
    ]
}

Elasticsearch查询:

"query" : {
        "nested" : {
            "path" : "distributor",
            "query" : {
                "query_string":{
                    "fields":["distributor.*"],
                    "query": "company"
                }
            },
            "inner_hits": { 
                "size": 100
            }
        }
    },
    "size": 2,
    "_source" : false
}

返回以下数据:

{
  ...
  "hits": {
    ...
    "hits": [
      {
        ...
        "_id": "1",
        "inner_hits": {
          "distributor": {
            "hits": {
              "total": 3,
              "max_score": 0.5276359,
              "hits": [
                {
                  "_nested": {
                    "field": "distributor",
                    "offset": 4
                  },
                  "_score": 0.5276359,
                  "_source": {
                    // returned data 1
                  }
                },
                {
                  "_nested": {
                    "field": "distributor",
                    "offset": 3
                  },
                  "_score": 0.5276359,
                  "_source": {
                    //returned data 2
                  }
                }
              ]
            }
          }
        }
      },
            {
        ... 
        "_id": "2",
        "inner_hits": {
          "distributor": {
            "hits": {
              "total": 3,
              "max_score": 0.5276359,
              "hits": [
                {
                  "_nested": {
                    "field": "distributor",
                    "offset": 4
                  },
                  "_score": 0.5276359,
                  "_source": {
                    // returned data 3
                  }
                },
                {
                  "_nested": {
                    "field": "distributor",
                    "offset": 3
                  },
                  "_score": 0.5276359,
                  "_source": {
                    //returned data 4
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}

但是,我想以以下方式返回展平结果:

{
  ...
  "hits": {
    ...
    "hits": [
      {
        ...
        "_id": "1",
        entity_data: {
            returned data 1
        }
      },
      {
        ...
        "_id": "1",
        entity_data: {
            returned data 2
        }
      },
      {
        ...
        "_id": "2",
        entity_data: {
            returned data 3
        }
      },
      {
        ...
        "_id": "2",
        entity_data: {
            returned data 4
        }
      }
    ]
  }
}

0 个答案:

没有答案