簡體   English   中英

使用 Elasticsearch 過濾嵌套字段

[英]Filter on nested field with Elasticsearch

我的項目、用戶和日程表中有 2 個實體

我需要創建一個頁面,以每周日歷的形式顯示所有員工及其每天的班次

示例: https : //monosnap.com/file/tEb3rUYNRmredPWOdfxRBTBpqkh36H

為此,我創建了一個新索引,其中我為所有員工編制了索引。 每個員工都有一個嵌套字段,用於存儲他的班次

問題是我無法弄清楚聚合和過濾器。

我需要

  1. 總是有一個按日期過濾,它指的是班次字段屬性。
  2. 是否有合適的班次並不重要,我們向所有員工展示
  3. 還會顯示以下 2 個聚合、用戶角色和班次類型。
  • 用戶角色過濾員工列表
  • 班次類型,顯示或隱藏相關班次

我的請求的一個例子

{

"aggs": {
    "shifts.ref_type": {
        "nested": {
            "path": "shifts"
        },
        "aggs": {
            "shifts.ref_type": {
                "terms": {
                    "field": "shifts.ref_type",
                    "size": 1000
                }
            }
        }
    },
    "role": {
        "terms": {
            "field": "role",
            "size": 1000
        }
    },
    "name": {
        "terms": {
            "field": "name",
            "size": 1000
        }
    }
},
"query": {
    "bool": {
        "must": [
            {
                "term": {
                    "_routing": "1"
                }
            }
        ],
        "should": [
            {
                "range": {
                    "shifts.date_from": {
                        "lte": 1636923600,
                        "gte": 1636318800
                    }
                }
            }
        ]
    }
},
"sort": [
    {
        "created": "ASC"
    }
],
"size": 1

}

樣本響應

{
"took": 3,
"timed_out": false,
"_shards": {
    "total": 4,
    "successful": 4,
    "skipped": 0,
    "failed": 0
},
"hits": {
    "total": {
        "value": 36,
        "relation": "eq"
    },
    "max_score": null,
    "hits": [
        {
            "_index": "employee_shift",
            "_type": "_doc",
            "_id": "a8abf060-25c8-45ee-a50e-02a2e2ad1c40",
            "_score": null,
            "_routing": "1",
            "_source": {
                "created": 1633967157,
                "type": "user",
                "title": null,
                "description": "",
                "uuid": "a8abf060-25c8-45ee-a50e-02a2e2ad1c40",
                "author": "System",
                "author:name": "System",
                "author:role": "",
                "acc": 1,
                "property": [
                    1
                ],
                "status": "Enabled",
                "class": [
                    ""
                ],
                "weight": "",
                "tags": [],
                "language": "en",
                "ref_source_id": null,
                "ref_source_helper": null,
                "ref_property": [
                    "test hostel2"
                ],
                "ref_property_default": "test hostel2",
                "name": "Housekeeper 1",
                "role": [
                    "Housekeeper"
                ],
                "role:weight": "2",
                "role:id": [
                    5
                ],
                "pay_rate": null,
                "experience": null,
                "supervisor": null,
                "gender": null,
                "units": [
                    "102",
                    "103",
                    "106",
                    "107",
                    "110",
                    "111",
                    "116",
                    "117",
                    "120",
                    "121",
                    "124",
                    "125",
                    "128",
                    "129",
                    "132",
                    "133",
                    "136",
                    "137"
                ],
                "task_inspection": "All tasks",
                "shifts": [
                    {
                        "uuid": "f48ae398-0668-4693-b335-2fee3baa2941",
                        "ref_type": "Work",
                        "ref_type:color": "",
                        "date_from": "1635196500",
                        "date_to": "1635197400",
                        "notes": null
                    },
                    {
                        "uuid": "8b4d8148-2583-4ccf-a1cc-ae5e6d1e728e",
                        "ref_type": "Work",
                        "ref_type:color": "",
                        "date_from": "1635287400",
                        "date_to": "1635289200",
                        "notes": null
                    },
                    {
                        "uuid": "3f5520d8-8108-4abd-8e2a-70c00faf6994",
                        "ref_type": "Work",
                        "ref_type:color": "",
                        "date_from": "1635369300",
                        "date_to": "1635373800",
                        "notes": null
                    },
                    {
                        "uuid": "d4009660-447c-47de-b0f3-3c1f2d8d8f99",
                        "ref_type": "Work",
                        "ref_type:color": "",
                        "date_from": "1635286500",
                        "date_to": "1635288300",
                        "notes": null
                    },
                    {
                        "uuid": "b3d883f0-b71f-4df7-bb63-a50f137528a4",
                        "ref_type": "Work",
                        "ref_type:color": "",
                        "date_from": "1635370200",
                        "date_to": "1635372900",
                        "notes": null
                    }
                ]
            },
            "sort": [
                1633967157000
            ]
        }
    ]
},
"aggregations": {
    "role": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
            {
                "key": "Houseman",
                "doc_count": 4
            },
            {
                "key": "Maintenance",
                "doc_count": 4
            },
            {
                "key": "Supervisor",
                "doc_count": 4
            },
            {
                "key": "Supervisor HSKP",
                "doc_count": 4
            },
            {
                "key": "Supervisor Maintenance",
                "doc_count": 4
            },
            {
                "key": "Administrator",
                "doc_count": 3
            },
            {
                "key": "Concierge dispatcher",
                "doc_count": 3
            },
            {
                "key": "Frontdesk",
                "doc_count": 3
            },
            {
                "key": "General manager",
                "doc_count": 3
            },
            {
                "key": "HKeeper",
                "doc_count": 3
            },
            {
                "key": "Housekeeper",
                "doc_count": 3
            },
            {
                "key": "Manager",
                "doc_count": 3
            }
        ]
    },
    "shifts.ref_type": {
        "doc_count": 21,
        "shifts.ref_type": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "Work",
                    "doc_count": 19
                },
                {
                    "key": "test",
                    "doc_count": 2
                }
            ]
        }
    },
    "name": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
            {
                "key": "Administrator 123",
                "doc_count": 1
            },
            {
                "key": "Administrator 223",
                "doc_count": 1
            },
            {
                "key": "Administrator 3",
                "doc_count": 1
            },
            {
                "key": "Concierge dispatcher 1",
                "doc_count": 1
            },
            {
                "key": "Concierge dispatcher 2",
                "doc_count": 1
            },
            {
                "key": "Concierge dispatcher 3",
                "doc_count": 1
            },
            {
                "key": "Frontdesk 1",
                "doc_count": 1
            },
            {
                "key": "Frontdesk 2",
                "doc_count": 1
            },
            {
                "key": "Frontdesk 3",
                "doc_count": 1
            },
            {
                "key": "General manager 1",
                "doc_count": 1
            },
            {
                "key": "General manager 2",
                "doc_count": 1
            },
            {
                "key": "General manager 3",
                "doc_count": 1
            },
            {
                "key": "HKeeper 1",
                "doc_count": 1
            },
            {
                "key": "HKeeper 2",
                "doc_count": 1
            },
            {
                "key": "HKeeper 3",
                "doc_count": 1
            },
            {
                "key": "Housekeeper 1",
                "doc_count": 1
            },
            {
                "key": "Housekeeper 2",
                "doc_count": 1
            },
            {
                "key": "Housekeeper 3",
                "doc_count": 1
            },
            {
                "key": "Houseman 1",
                "doc_count": 1
            },
            {
                "key": "Houseman 2",
                "doc_count": 1
            },
            {
                "key": "Houseman 3",
                "doc_count": 1
            },
            {
                "key": "Maintenance 1",
                "doc_count": 1
            },
            {
                "key": "Maintenance 2",
                "doc_count": 1
            },
            {
                "key": "Maintenance 3",
                "doc_count": 1
            },
            {
                "key": "Manager 1222",
                "doc_count": 1
            },
            {
                "key": "Manager 2",
                "doc_count": 1
            },
            {
                "key": "Manager 3",
                "doc_count": 1
            },
            {
                "key": "Supervisor 1",
                "doc_count": 1
            },
            {
                "key": "Supervisor 2",
                "doc_count": 1
            },
            {
                "key": "Supervisor 3",
                "doc_count": 1
            },
            {
                "key": "Supervisor HSKP 1",
                "doc_count": 1
            },
            {
                "key": "Supervisor HSKP 2",
                "doc_count": 1
            },
            {
                "key": "Supervisor HSKP 3",
                "doc_count": 1
            },
            {
                "key": "Supervisor Maintenance 1",
                "doc_count": 1
            },
            {
                "key": "Supervisor Maintenance 2",
                "doc_count": 1
            },
            {
                "key": "Supervisor Maintenance 3",
                "doc_count": 1
            }
        ]
    }
}

}

目前,除了一點之外,一切似乎都在正常工作。 按班次類型聚合總是輸出數據,盡管它們不應該被日期的過濾器找到。

有什么建議嗎? 謝謝你

我根據我的評論中的假設做出答案:

  • 您希望您的查詢返回所有員工
  • 您希望 ref_type 聚合僅包括與您的日期范圍匹配的班次
  • 您希望結果下的“班次”集合同樣只包括與您的日期范圍相匹配的班次

如果我誤解了你的問題,我深表歉意。

首先要讓開的一件事是,盡管您可能已經意識到:查詢的should部分不會限制結果,它只會影響分數,因為您已經有了must

作為推論,它不會影響聚合結果,為此您需要使用過濾器聚合

"aggs": {
        "shifts.ref_type": {
            "nested": {
                "path": "shifts"
            },
            "aggs": {
                "shifts.ref_type": {
                    "filter": { 
                        "range": { 
                            "shifts.date_from": {
                                "gte": 1635370100,
                                "lte": 1635370300
                            }
                        } 
                    },
                    "aggs": {
                        "shifts.ref_type": {
                            "terms": {
                                "field": "shifts.ref_type",
                                "size": 1000
                            }
                        }
                    }
                }
            }
        },

這將為您提供聚合中的過濾計數,但這仍然不會過濾您的結果 - 您將獲得點擊中的所有“變化”。 因此,對於嵌套文檔需要注意的一點是,您的查詢將根據嵌套文檔中的匹配限制返回的文檔,但實際上不會過濾掉不匹配的嵌套文檔。 為了做到這一點,您有另一個功能, 嵌套內部命中,它可以讓您准確找出匹配的嵌套文檔。 在您的情況下,這仍然不夠,因為您不想完全過濾掉結果(因此即使沒有“內部命中”,您仍然希望返回文檔,或者至少讓它成為聚合的一部分)。 所以現在您還有另一個功能, post-filter ,您可以使用它在聚合過濾文檔。 將兩者結合起來:

    "post_filter": {
        "nested": {
            "path": "shifts",
            "query": {
                "bool": {
                    "must": [
                        {
                            "range": {
                                "shifts.date_from": {
                                    "lte": 635370200,
                                    "gte": 635370200
                                }
                            }
                        }
                    ]
                }
            },
            "inner_hits": {}
        }
    },

如果您現在設置_source: false ,您將不會有命中,您只會得到匹配的班次(然后您的聚合結果中仍然有員工姓名和角色):

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.08701137,
        "hits": [
            {
                "_index": "employee_shift",
                "_type": "_doc",
                "_id": "-tRnLn0B5PjpsgKgGXlB",
                "_score": 0.08701137,
                "inner_hits": {
                    "shifts": {
                        "hits": {
                            "total": {
                                "value": 1,
                                "relation": "eq"
                            },
                            "max_score": 1,
                            "hits": [
                                {
                                    "_index": "employee_shift",
                                    "_type": "_doc",
                                    "_id": "-tRnLn0B5PjpsgKgGXlB",
                                    "_nested": {
                                        "field": "shifts",
                                        "offset": 4
                                    },
                                    "_score": 1,
                                    "_source": {
                                        "notes": null,
                                        "ref_type:color": "",
                                        "date_to": 635372900,
                                        "ref_type": "Work",
                                        "uuid": "b3d883f0-b71f-4df7-bb63-a50f137528a4",
                                        "date_from": 635370200
                                    }
                                }
                            ]
                        }
                    }
                }
            }
        ]
    },
    "aggregations": {
        "role": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "Housekeeper",
                    "doc_count": 5
                }
            ]
        },
        "shifts.ref_type": {
            "doc_count": 25,
            "shifts.ref_type": {
                "doc_count": 4,
                "shifts.ref_type": {
                    "doc_count_error_upper_bound": 0,
                    "sum_other_doc_count": 0,
                    "buckets": [
                        {
                            "key": "Work",
                            "doc_count": 3
                        },
                        {
                            "key": "Work2",
                            "doc_count": 1
                        }
                    ]
                }
            }
        },
        "name": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "Housekeeper 4",
                    "doc_count": 2
                },
                {
                    "key": "Housekeeper 1",
                    "doc_count": 1
                },
                {
                    "key": "Housekeeper 2",
                    "doc_count": 1
                },
                {
                    "key": "Housekeeper 3",
                    "doc_count": 1
                }
            ]
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM