我的弹性映射中有一个嵌套对象。我正在尝试为该嵌套对象创建过滤器查询
Posted
技术标签:
【中文标题】我的弹性映射中有一个嵌套对象。我正在尝试为该嵌套对象创建过滤器查询【英文标题】:There is a nested object in my Elastic mapping. I am trying to create a filter query for that nested object 【发布时间】:2020-09-13 17:35:04 【问题描述】:据我所知,当我将对象声明为嵌套对象时,它创建了一个类似数组的结构来存储该对象类型的多个对象。 这是映射 json 文件的示例。
"employee":
"dynamic": "strict",
"properties":
"id":
"type": "keyword"
,
"name":
"type": "keyword"
,
"previous_job_documents":
"type": "nested",
"properties":
"id":
"type": "keyword"
,
"status":
"type": "keyword"
,
"type":
"type": "integer"
从弹性中获取的示例数据。
"data":
"nodes":
"totalCount": 465,
"edges": [
"id": "b6ecb8aa-4d12-44d6-8ed7-9ee1003d0d54",
"name": "test",
"previous_job_documents": [
"type": "RELIEVING_LETTER",
"status": "NOT_SUBMITTED"
,
"type": "OFFER_LETTER",
"status": "SUBMITTED"
,
"type": "PAYSLIP_FIRST_MONTH",
"status": "VERIFIED"
,
"type": "PAYSLIP_SECOND_MONTH",
"status": "NOT_SUBMITTED"
,
"type": "PAYSLIP_THIRD_MONTH",
"status": "NOT_SUBMITTED"
]
]
问题:我想编写一个过滤器,它会找到所有名称为 test 且 RELIEVING_LETTER 为 NOT_SUBMITTED 且 OFFER_LETTER 为 SUBMITTED 的员工。
这是我在过滤器中尝试的:
"filters":
"Employee":
"AND": ["name": "test",
"path": "previous_job_documents"
"AND": [
"term":
"previous_job_documents.type":
"value": "RELIEVING_LETTER"
,
"term":
"previous_job_documents.status":
"value": "NOT_SUBMITTED"
,
"term":
"previous_job_documents.type":
"value": "OFFER_LETTER"
,
"term":
"previous_job_documents.status":
"value": "SUBMITTED"
]
]
【问题讨论】:
【参考方案1】:您需要对匹配查询中的嵌套字段使用嵌套查询(因为嵌套字段将单独存储)。
"query":
"bool":
"must":[
"match":"name":"user2"
,
"nested":
"path": "previous_job_documents",
"query":
"bool":
"must":[
"match": "previous_job_documents.type": "RELIEVING_LETTER",
"match":"previous_job_documents.status":"NOT_SUBMITTED"
]
,
"nested":
"path": "previous_job_documents",
"query":
"bool":
"must":[
"match": "previous_job_documents.type": "OFFER_LETTER",
"match":"previous_job_documents.status":"SUBMITTED"
]
]
【讨论】:
以上是关于我的弹性映射中有一个嵌套对象。我正在尝试为该嵌套对象创建过滤器查询的主要内容,如果未能解决你的问题,请参考以下文章