elasticsearch 如何在多个字段上编写弹性聚合

j0pj023g  于 2023-06-21  发布在  ElasticSearch
关注(0)|答案(1)|浏览(110)

我需要在多个字段上聚合Elastic Search。
我有一个名为Working days的嵌套字段,下面是相同的Map。

"workingDays": {
    "type": "nested",
    "properties": {
        "mon": {
            "type": "nested",
            "properties": {
                "availability": {
                    "type": "boolean"
                },
                "title": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                           "type": "keyword"
                         }
                    }
                },
                "notes": {
                    "type": "text"
                }
            }
        },
        "tue": {
            "type": "nested",
            "properties": {
                "availability": {
                    "type": "boolean"
                },
                "title": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                           "type": "keyword"
                         }
                    }
                },
                "notes": {
                    "type": "text"
                }
            }
        }
    }
}

以这种方式,Map持续所有的日子。
现在,数据如下所示

"workingdays": {
        "mon": {
            "availability": true,
            "title": "Monday",
            "notes": ""
        },
        "tue": {
            "title": "Tuesday",
            "notes": "On Tuesdays, drop off and pick up times must be scheduled between 12pm to 2pm",
            "availability": false
        }
    }

所以,我的要求如下。我需要汇总并得出在特定日期可用的所有数据,类似于此。

"buckets": [
   {
      "key": "Monday",
      "doc_count": 3
   },
   {
      "key": "Tuesday",
      "doc_count": 5
   }
]

我尝试过嵌套聚合、组合聚合和多字段聚合,但它们都没有产生可用的格式。

g6ll5ycj

g6ll5ycj1#

您可以在嵌套聚合的filter aggregation内部使用nested aggregations:)。

PUT test_days
{
  "mappings": {
    "properties": {
      "workingdays": {
        "type": "nested",
        "properties": {
          "mon": {
            "type": "nested",
            "properties": {
              "availability": {
                "type": "boolean"
              },
              "title": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword"
                  }
                }
              },
              "notes": {
                "type": "text"
              }
            }
          },
          "tue": {
            "type": "nested",
            "properties": {
              "availability": {
                "type": "boolean"
              },
              "title": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword"
                  }
                }
              },
              "notes": {
                "type": "text"
              }
            }
          }
        }
      }
    }
  }
}
POST test_days/_doc?refresh
{
  "workingdays": {
    "mon": {
      "availability": true,
      "title": "Monday",
      "notes": ""
    },
    "tue": {
      "title": "Tuesday",
      "notes": "On Tuesdays, drop off and pick up times must be scheduled between 12pm to 2pm",
      "availability": false
    }
  }
}
GET test_days/_search
{
  "size": 0,
  "aggs": {
    "total": {
      "nested": {
        "path": "workingdays"
      },
      "aggs": {
        "monday": {
          "filters": {
            "filters": {
              "monday_availability": {
                "nested": {
                  "path": "workingdays.mon",
                  "query": {
                    "term": {
                      "workingdays.mon.availability": {
                        "value": true
                      }
                    }
                  }
                }
              },
              "tuesday_availability": {
                "nested": {
                  "path": "workingdays.tue",
                  "query": {
                    "term": {
                      "workingdays.tue.availability": {
                        "value": true
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

注意:区分大小写!!请仔细检查是否使用workingDaysworkingdays

相关问题