跨多个索引的多个字段的 Elasticsearch 聚合

tam*_*ama 8 elasticsearch elasticsearch-aggregation

我有两个索引 - 一个用于Application模型,另一个用于Databases模型(多对多关系)。

每个文档都被非规范化以包含来自另一个模型的属性

Application
          |_ vendor_name
          |_ databases
                 |_ db_1
                 |_ db_2


Database
       |_ database_applications
                 |_ app_1
                        |_vendor_name
                 |_ app_2
                        |_ vendor_name
Run Code Online (Sandbox Code Playgroud)

对供应商名称执行多索引搜索 - 似乎我从两个索引中都得到了正确的结果。

挑战在 vendor_name 字段上正确聚合

当结果仅来自数据库时,使用以下聚合似乎有效。我也尝试过,field: '*vendor_name'但似乎不起作用。

我错过了什么?模型应该改变吗?

   aggregation:
       vendor_name: {
            terms: {
                field: "database_applications.vendor_name"
            }
        }
Run Code Online (Sandbox Code Playgroud)

更新1:

根据@Andrie-Stefan - 这是两个索引映射的更准确表示(缩写为简短):

数据库

Application
          |_ vendor_name
          |_ databases
                 |_ db_1
                 |_ db_2


Database
       |_ database_applications
                 |_ app_1
                        |_vendor_name
                 |_ app_2
                        |_ vendor_name
Run Code Online (Sandbox Code Playgroud)

应用

{
  "applications": {
    "aliases": {},
    "mappings": {
      "application": {
        "properties": {
          "application_view": {
            "properties": {
              "app_name": {
                "type": "string"
              },
              "app_status": {
                "type": "string"
              },
              "app_steward_name": {
                "type": "string"
              },
              "app_suite": {
                "type": "string"
              },
              "app_vendor_name": {
                "type": "string"
              },
              "app_version": {
                "type": "string"
              },
              "assignment_group": {
                "type": "string"
              },
              "business_domain_name": {
                "type": "string"
              },
              "exception": {
                "type": "string"
              },
              "id": {
                "type": "long"
              },
              "it_owner_name": {
                "type": "string"
              },
              "service_level": {
                "type": "string"
              }
            }
          },
          "assignment_group": {
            "type": "string"
          },
          "company_databases": {
            "properties": {
              "backup_history_info": {
                "type": "string"
              },
              "company_applications": {
                "properties": {
                  "alternate_name": {
                    "type": "string"
                  },
                  "application_id": {
                    "type": "long"
                  },
                  "application_name": {
                    "type": "string"
                  },
                  "business_owner": {
                    "type": "string"
                  },
                  "company_system_applications": {
                    "properties": {
                      "aka": {
                        "type": "string"
                      },
                      "allow_add_request": {
                        "type": "string"
                      },
                      "allow_remove_request": {
                        "type": "string"
                      },
                      "asset_type": {
                        "type": "string"
                      },
                      "contract_number": {
                        "type": "string"
                      },
                      "cost_level": {
                        "type": "string"
                      },
                      "company_alternate_name": {
                        "type": "string"
                      },
                      "company_application_id": {
                        "type": "long"
                      },
                      "company_application_name": {
                        "type": "string"
                      },
                      "company_business_owner": {
                        "type": "string"
                      },
                      "company_division_id": {
                        "type": "long"
                      },
                      "company_it_app_steward": {
                        "type": "string"
                      },
                      "company_notes": {
                        "type": "string"
                      },
                      "company_system_id": {
                        "type": "long"
                      },
                      "company_vendor": {
                        "type": "string"
                      },
                      "description": {
                        "type": "string"
                      },
                      "display_in_catalog": {
                        "type": "string"
                      },
                      "id": {
                        "type": "long"
                      },
                      "inform_of_removal": {
                        "type": "string"
                      },
                      "is_restricted": {
                        "type": "string"
                      },
                      "it_app_steward": {
                        "type": "string"
                      },
                      "it_owner": {
                        "type": "string"
                      },
                      "last_modified": {
                        "type": "string"
                      },
                      "last_modified_by": {
                        "type": "string"
                      },
                      "media_location": {
                        "type": "string"
                      },
                      "media_source": {
                        "type": "string"
                      },
                      "name": {
                        "type": "string"
                      },
                      "os_environment": {
                        "type": "string"
                      },
                      "owned_by": {
                        "type": "string"
                      },
                      "retirement_date": {
                        "type": "date",
                        "format": "dateOptionalTime"
                      },
                      "status": {
                        "type": "string"
                      },
                      "status_id": {
                        "type": "long"
                      },
                      "suite_name": {
                        "type": "string"
                      },
                      "system_application": {
                        "properties": {
                          "assignment_group": {
                            "type": "string"
                          },
                          "division": {
                            "type": "string"
                          },
                          "id": {
                            "type": "long"
                          },
                          "name": {
                            "type": "string"
                          },
                          "owner_id": {
                            "type": "string"
                          },
                          "status": {
                            "type": "string"
                          },
                          "steward_id": {
                            "type": "string"
                          },
                          "suite": {
                            "type": "string"
                          },
                          "vendor_name": {
                            "type": "string"
                          },
                          "vendor_url_web_site": {
                            "type": "string"
                          },
                          "version": {
                            "type": "string"
                          }
                        }
                      },
                      "vendor_name": {
                        "type": "string"
                      },
                      "version": {
                        "type": "string"
                      }
                    }
                  },
                  "division_id": {
                    "type": "long"
                  },
                  "it_app_steward": {
                    "type": "string"
                  },
                  "notes": {
                    "type": "string"
                  },
                  "software_inventory_id": {
                    "type": "long"
                  },
                  "vendor": {
                    "type": "string"
                  }
                }
              },
              "company_instances": {
                "properties": {
                  "business_environment_id": {
                    "type": "long"
                  },
                  "cgi_service_id": {
                    "type": "long"
                  },
                  "char_set": {
                    "type": "string"
                  },
                  "confirmed_license_purchase_dt": {
                    "type": "string"
                  },
                  "company_server": {
                    "properties": {
                      "business_environment_id": {
                        "type": "long"
                      },
                      "division_id": {
                        "type": "long"
                      },
                      "domain": {
                        "type": "string"
                      },
                      "hw_platform_id": {
                        "type": "long"
                      },
                      "ip_address": {
                        "type": "string"
                      },
                      "location_id": {
                        "type": "long"
                      },
                      "no_of_cpu": {
                        "type": "long"
                      },
                      "notes": {
                        "type": "string"
                      },
                      "os_platform_id": {
                        "type": "long"
                      },
                      "os_version": {
                        "type": "string"
                      },
                      "server_id": {
                        "type": "long"
                      },
                      "server_name": {
                        "type": "string"
                      }
                    }
                  },
                  "description": {
                    "type": "string"
                  },
                  "division_id": {
                    "type": "long"
                  },
                  "edition_id": {
                    "type": "long"
                  },
                  "instance_id": {
                    "type": "long"
                  },
                  "instance_name": {
                    "type": "string"
                  },
                  "itap_have_access": {
                    "type": "string"
                  },
                  "listener_port": {
                    "type": "long"
                  },
                  "location_id": {
                    "type": "long"
                  },
                  "notes": {
                    "type": "string"
                  },
                  "patch_number": {
                    "type": "string"
                  },
                  "rdbms_type_id": {
                    "type": "long"
                  },
                  "server_id": {
                    "type": "long"
                  },
                  "service_level_id": {
                    "type": "long"
                  },
                  "version": {
                    "type": "string"
                  }
                }
              },
              "db_security_model_id": {
                "type": "long"
              },
              "notes": {
                "type": "string"
              },
  

Pet*_*ses 3

如果字段名称相似,则只能跨索引聚合字段。聚合字段没有通配符语法。

以下是您的映射当前定义的内容:


指数company-company_databases

类型company_database

字段名称

  • company_applications.company_system_applications.vendor_name
  • company_applications.company_system_applications.system_application.vendor_name

指数applications

类型application

字段名称

  • company_databases.company_applications.company_system_applications.vendor_name
  • company_databases.company_applications.company_system_applications.system_application.vendor_name

就 Elasticsearch 而言,这些字段没有任何共同点(尽管部分路径是vendor_name)。

如果您的目标是聚合vendor_name跨越两个索引的查询,请考虑重组索引/映射来实现此目的。


请注意,Elasticsearch不建模多对多关系

如果您可以避免在应用程序之间复制数据库信息,您也许可以将关系重新表述为层次结构,例如:

指数applications

--

类型application

字段vendor_name等...

--

类型database_application

字段vendor_namedatabases.<inner fields>等...

--

然后,您就可以在同一字段路径上跨类型进行聚合vendor_name,并获得查询单个applications索引的额外好处。


Copyright Info

© Copyright 2013-2021 admin@qa.1r1g.com

如未特别说明,本网站的内容使用如下协议:
Creative Commons Atution-NonCommercial-ShareAlike 4.0 International license
.

用以下方式浏览
回到顶部