我已经创建了一个包含100多个文档的索引,下面是示例2个文档
文件1:
"OfficeId":1;办公名称":"Washers Ltd"客户":[
{ "customerid":10, "customername": Mike, "customerphone": 1111111111 } , { "customerid":20, "customername": Angie, "customerphone": 2222222222 } ]
文档2:
"OfficeId":2;办公名称":"Coldwell有限公司"客户":[
{ "customerid":30, "customername": Nathan, "customerphone": 1111111111 } ]
从UI中,我们可以按客户名称或客户电话进行搜索。当我通过电话搜索时11111111111我应该得到2个文档(点击0,点击1(,但在第一个文档中/点击0,我如何过滤只显示1个对象?
您需要将嵌套查询与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": {}
}
}
}