【问题标题】:Elastic search Query - in array of objects弹性搜索查询 - 在对象数组中
【发布时间】:2021-07-17 13:47:15
【问题描述】:

我创建了一个包含 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 个对象?

【问题讨论】:

    标签: elasticsearch elasticsearch-nested


    【解决方案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,
              "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": {}
        }
      }
    }
    

    【讨论】:

    • 感谢您立即回复。还有一个问题,我们不知道他们将如何从 UI 中搜索,他们可以通过 customerphone 或 customername 进行搜索。在给定的查询中,在“必须”下,我可以为客户名再包含一个“匹配项”吗? @ECoder
    • @RKGaz 是的,您可以再包含一个 match 子句
    • @RKGaz 更新了答案,增加了一个查询。请仔细检查一下,如果这能解决您的问题,请告诉我?
    • 非常感谢,它成功了。你能指出我在哪里可以下载任何工具/东西来在我的本地 Windows 机器上测试 ES 吗?
    • 抱歉忘记更新,在查询中我们从“必须”改为“应该”。
    猜你喜欢
    • 2021-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-09
    • 1970-01-01
    相关资源
    最近更新 更多