[英]Elastic search Query - in array of objects
我創建了一個包含 100 多個文檔的索引,這里是示例 2 文檔
文件 1:
“OfficeId”:1,“Officename”:“Washers Ltd”,“客戶”:[
{ "customerid":10, "customername": Mike, "customerphone": 1111111111 }, { "customerid":20, "customername": Angie, "customerphone": 2222222222 } ]
文件 2:
“OfficeId”:2,“Officename”:“Coldwell Ltd”,“客戶”:[
{ "customerid":30, "customername": Nathan, "customerphone": 1111111111 } ]
在 UI 中,我們可以按客戶名稱或客戶電話進行搜索。 當我通過電話1111111111搜索時,我應該得到 2 個文檔(命中 0,命中 1),但在第一個文檔/命中 0 中,我如何才能過濾僅顯示 1 object?
您需要將嵌套查詢與inner_hits一起使用
添加帶有索引數據、映射、搜索查詢和搜索結果的工作示例
索引映射:
{
"mappings": {
"properties": {
"customers": {
"type": "nested"
}
}
}
}
指數數據:
{
"OfficeId": 2,
"Officename": "Coldwell Ltd",
"customers": [
{
"customerid": 30,
"customername": "Nathan",
"customerphone": 1111111111
}
]
}
{
"OfficeId": 1,
"Officename": "Washers Ltd",
"customers": [
{
"customerid": 10,
"customername": "Mike",
"customerphone": 1111111111
},
{
"customerid": 20,
"customername": "Angie",
"customerphone": 2222222222
}
]
}
搜索查詢:
{
"query": {
"nested": {
"path": "customers",
"query": {
"bool": {
"must": [
{
"match": {
"customers.customerphone": "1111111111"
}
}
]
}
},
"inner_hits": {}
}
}
}
搜索結果將是
"hits": [
{
"_index": "67228476",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"OfficeId": 1,
"Officename": "Washers Ltd",
"customers": [
{
"customerid": 10,
"customername": "Mike",
"customerphone": 1111111111
},
{
"customerid": 20,
"customername": "Angie",
"customerphone": 2222222222
}
]
},
"inner_hits": {
"customers": {
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "67228476",
"_type": "_doc",
"_id": "1",
"_nested": {
"field": "customers",
"offset": 0
},
"_score": 1.0,
"_source": {
"customerid": 10, // note this
"customername": "Mike",
"customerphone": 1111111111
}
}
]
}
}
}
},
{
"_index": "67228476",
"_type": "_doc",
"_id": "2",
"_score": 1.0,
"_source": {
"OfficeId": 2,
"Officename": "Coldwell Ltd",
"customers": [
{
"customerid": 30,
"customername": "Nathan",
"customerphone": 1111111111
}
]
},
"inner_hits": {
"customers": {
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "67228476",
"_type": "_doc",
"_id": "2",
"_nested": {
"field": "customers",
"offset": 0
},
"_score": 1.0,
"_source": {
"customerid": 30, // note this
"customername": "Nathan",
"customerphone": 1111111111
}
}
]
}
}
}
}
]
更新1:
如果您想再包含一個match
子句,請使用此查詢
{
"query": {
"nested": {
"path": "customers",
"query": {
"bool": {
"must": [
{
"match": {
"customers.customerphone": "1111111111"
}
},
{
"match": {
"customers.customername": "Nathan"
}
}
]
}
},
"inner_hits": {}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.