首页 > 解决方案 > 具有自定义格式响应的 Elasticsearch 查询搜索

问题描述

我对弹性搜索查询有疑问,因为我需要以特定方式格式化响应。

我有这个数据(从弹性搜索中提取的数据样本)

 {
  "_index": "testmg",
  "_type": "_doc",
  "_id": "6e4747d1-8c11-47d4-85d4-1777b245b06f",
  "_version": 1,
  "_score": 0,
  "fields": {
    "PLSystemGeneralData.ISTATCode._value.keyword": [
      "12058091"
    ],
 
     "POD": [
      {
        "POD_IT012345678901.PODCode._value": [
          "IT012345678901"
        ],
        "POD_IT012345678901.PODGeneralData.LocationReference._value": [
          "secondo incrocio"
        ],
        "POD_IT012345678901.PODGeneralData.PODPreservationStatus._value": [
          "02"
        ],
        }
  ],
"ElectricPanel": [
      {
        "ElectricPanel_QE_02.ElectricPanelGeneralData.AccessibilityType._value": [
          "02"
        ],
        "ElectricPanel_quadro1.ElectricPanelGeneralData.OnlyPublicLighitingFlag._value": [
          false
        ],
        "ElectricPanel_quadro1.ElectricPanelGeneralData.Point.pos.coords.lat": [
          222
        ],
        "ElectricPanel_QE_01.ElectricPanelOperatingData.PowerOnMeans._value": [
          "04"
        ],
"ElectricPanel_QE_01.ElectricPanelID._value": [
          "QE_01"
        ],
        "ElectricPanel_QE_01.ElectricPanelTechnicalData.ElectricPhases._value": [
          "03"
        ],
        "ElectricPanel_QE_02.ElectricPanelGeneralData.NumberOfOwnedLightSpots._value": [
          5
        ],
        "ElectricPanel_quadro1.ElectricPanelGeneralData.ValidityStartDate._value": [
          "2019-05-03T00:00:00.000Z"
        ],
        ElectricPanel_QE_01.ElectricPanelGeneralData.ElectricPanelInstalledPower.uom.keyword": [
          "kW"
        ],
        "ElectricPanel_QE_01.ElectricPanelOperatingData.ValidityStartDate._value": [
          "2019-05-03T00:00:00.000Z"
        ],
        "ElectricPanel_QE_01.ElectricPanelGeneralData.OnlyPublicLighitingFlag._value": [
          true
        ], "ElectricPanel_QE_01.ElectricPanelMaintenance.CurrentYearOtherOrdinaryMaintenanceAmount._value": [
          2000
        ],
        "ElectricPanel_QE_01.ElectricPanelOperatingData.Scale._value": [
          "04"
        ],
}]
        ...
        

我需要有这样的输出:

"type": "POD",
"ISTATCode": "12058091",
"PODCode": "UVAX"
}, {
"type": "EP",
"ISTATCode": "12058091",
"comune": "Roma",
"PODCode": "UVAX",
"DistributorName": "Pippo 2",
"SupplierName": "Pippo 3",
"InstalledPower": "0.8",
"ElectricPanelID": "UVAXPANELID",
"ElectricPanelInstalledPower": "0.2",
"NumberOfConnectedLightSpots": "1",
"lat": "-30.7",
"lng": "134.5"
}, {
"type": "LS",
"ISTATCode": "12058091",
"comune": "Roma",
"PODCode": "UVAX",
"DistributorName": "Pippo 2",
"SupplierName": "Pippo 3",
"InstalledPower": "0.8",
"ElectricPanelID": "UVAXPANELID",
"ElectricPanelInstalledPower": "0.2",
"LightSpotID": "PL001",
"EquipmentType": "03",
"Height": "7",
"SupportMaterial": "04",
"lat": "-30.7",
"lng": "134.2"
}, {
"type": "POD",
"ISTATCode": "12058091",
"comune": "Roma",
"PODCode": "IT001E04172906",
"DistributorName": "Ena spa",
"SupplierName": "Alia spa",
"InstalledPower": "0.8",
"lat": "-30.7",
"lng": "134.1"
}]

其中 type 是“POD”或“ElectricPanel”,其他是字段。

我尝试使用 Elasticsearch sql 查询来直接返回字段,但是我在子查询和聚合方面遇到了一些问题,特别是在数组值和嵌套字段方面;我尝试使用 DSL 和聚合,但我不确定是否有可能以简单的方式获得此结果。我也尝试了字段别名,但我遇到了嵌套字段的问题:

例如我补充说:

 "pod": {
    "type": "alias",
    "path": "POD.properties.PODCode._value" 
  }

但我返回了一个错误

“type”:“mapper_parsing_exception”,“reason”:“字段别名 [pod] 的 [path] 值 [POD.properties.PODCode._value] 无效:别名必须引用映射中的现有字段。”

映射是:

"properties": {
    "POD": {
      "type": "nested",
      "properties": {
        "PODCode": {
          "properties": {
            "_value": {
              "type": "text",
              "fields": {
                "keyword": {
                  "ignore_above": 256.0,
                  "type": "keyword"
                }
              }
            }
          }
        },
        "PODGeneralData": {
          "properties": {
            "PODPreservationStatus": {
              "properties": {
                "_value": {
                  "type": "text",
                  "fields": {
                    "keyword": {
                      "ignore_above": 256.0,
                      "type": "keyword"
                    }
                  }
                }
              }
            },
            "LocationReference": {
              "properties": {
                "_value": {
                  "type": "text",
                  "fields": {
                    "keyword": {
                      "ignore_above": 256.0,
                      "type": "keyword"
                    }
                  }
                }
              }
            }
}},
"ElectricPanel": {
      "type": "nested",
      "properties": {
        "ElectricPanelGeneralData": {
          "properties": {
            "NumberOfSupports": {
              "properties": {
                "_value": {
                  "type": "long"
                }
              }
            },
            "OnlyPublicLighitingFlag": {
              "properties": {
                "_value": {
                  "type": "boolean"
                }
              }
            },
            "ConstructionYear": {
              "properties": {
                "_value": {
                  "type": "text",
                  "fields": {
                    "keyword": {
                      "ignore_above": 256.0,
                      "type": "keyword"
                    }
                  }
                }
              }
            },
            "KeyLockFlag": {
              "properties": {
                "_value": {
                  "type": "boolean"
                }
              }
            },
            "NumberOfOverheadLines": {
              "properties": {
                "_value": {
                  "type": "long"
                }
              }
            },
            "ValidityStartDate": {
              "properties": {
                "_value": {
                  "type": "date"
                }
              }
            }
          }
        },
        "PODCode": {
          "properties": {
            "_value": {
              "type": "text",
              "fields": {
                "keyword": {
                  "ignore_above": 256.0,
                  "type": "keyword"
                }
              }
            }
          }
        }
      }
    },
    }},
"PLSystemGeneralData": {
      "properties": {
        "ISTATCode": {
          "properties": {
            "_value": {
              "type": "text",
              "fields": {
                "keyword": {
                  "ignore_above": 256.0,
                  "type": "keyword"
                }
              }
            }
          }
        }
}}


你有什么想法吗?有人可以帮帮我吗?谢谢你

标签: elasticsearchelasticsearch-aggregationelasticsearch-dslelasticsearch-sql

解决方案


推荐阅读