弹性搜索查询 - 在对象数组中

问题描述

我创建了一个包含 100 多个文档的索引,这里是示例 2 个文档

文档 1:

"OfficeId": 1,"Officename": "Washers Ltd","customers": [

{
    "customerid":10,"customername": Mike,"customerphone": 1111111111
},{
    "customerid":20,"customername": Angie,"customerphone": 2222222222
} ]

文档 2:

"OfficeId": 2,"Officename": "Coldwell Ltd","customers": [

{
    "customerid":30,"customername": Nathan,"customerphone": 1111111111
} ]

在 UI 中,我们可以按客户姓名或客户电话进行搜索。当我通过电话搜索 1111111111 时,我应该得到 2 个文档(命中 0,命中 1)但在第一个文档/命中 0 中,我如何过滤以仅显示 1 个对象?

解决方法

您需要同时使用 nested queryinner_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,"customers": [
            {
              "customerid": 10,"customerphone": 1111111111
            },{
              "customerid": 20,"customerphone": 2222222222
            }
          ]
        },"inner_hits": {
          "customers": {
            "hits": {
              "total": {
                "value": 1,"relation": "eq"
              },"max_score": 1.0,"hits": [
                {
                  "_index": "67228476","_nested": {
                    "field": "customers","offset": 0
                  },"_source": {
                    "customerid": 10,// note this
                    "customername": "Mike","customerphone": 1111111111
                  }
                }
              ]
            }
          }
        }
      },{
        "_index": "67228476","_id": "2","_source": {
          "OfficeId": 2,"customers": [
            {
              "customerid": 30,"customerphone": 1111111111
            }
          ]
        },"_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": {}
    }
  }
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...