ElasticSearch查询嵌套对象
问题描述:
我有以下的文档结构:ElasticSearch查询嵌套对象
"IDNumberTypes": {
"ID": [
{
"@IDType": "Company Identification No.",
"IDValue": [
{
"#text": "CompanyID"
}
]
},
{
"@IDType": "Reg. Number",
"IDValue": [
{
"#text": "RegNumber"
}
]
},
{
"@IDType": "Tax ID Number",
"IDValue": [
{
"#text": "TaxNumber"
}
]
}
]
}
我想写一个匹配TaxNumber但只有在“国税号”的情况下查询。 像这样的东西在pseudoSQL:
IDNumberTypes.ID.IDValue.#text="TaxNumber" WHERE [email protected]="Tax ID Number"
只是做它像这样明显地导致返回包含一个对象,具有“@IDType”文件:“税号”
"query": {
"bool": {
"must": {
"match": {
"IDNumberTypes.ID.IDValue.#text": {
"query": "TaxNumber",
"operator": "and"
}
}
},
"filter": {
"match": {
"[email protected]": {
"query": "Tax ID Number",
"operator": "and"
}
}
}
}
}
...但没有按我不确定我要找的对象必须具有以下特定结构:
{
"@IDType": "Tax ID Number",
"IDValue": [{
"#text": "TaxNumber"
}]
}
如何正确地过滤/构造我的查询?
我已经整理出来,加入适当的映射:
{
"mappings": {
"doxx": {
"properties": {
"IDNumberTypes": {
"properties": {
"ID": {
"type": "nested",
"properties": {
"@IDType": {
"type": "string"
},
"IDValue": {
"type": "nested",
"properties": {
"#text": {
"type": "string"
},
"@IDnotes": {
"type": "string"
}
}
}
}
}
}
}
}
}
}
}
答
正确映射:
{
"mappings": {
"doxx": {
"properties": {
"IDNumberTypes": {
"properties": {
"ID": {
"type": "nested",
"properties": {
"@IDType": {
"type": "string"
},
"IDValue": {
"type": "nested",
"properties": {
"#text": {
"type": "string"
},
"@IDnotes": {
"type": "string"
}
}
}
}
}
}
}
}
}
}
}
可能请您提供更多的细节:其中ES版本,您使用的?什么是你的ES文件的映射? –
我已经通过添加适当的映射对它进行了整理:请参阅OP – user2429408