嵌套弹性搜索嵌套查询问题ES7.2

时间:2019-07-17 09:46:15

标签: elasticsearch elasticsearch-nested

我需要在弹性搜索7.2上进行嵌套查询。 用例: 可以说我有多个类别,而内部类别中有多个人。 我需要能够获取诸如 从类别为“ category1”且类别为person.personid = 1的文档中选择*。

我已经准备了嵌套​​映射并创建了虚拟数据。 我正在寻找一个旧示例,但是当我运行嵌套查询时,它就坏了。它无法创建查询。 我在弹性搜索方面有2个小时的经验。

PUT http://localhost:9200/t
    {
      "mappings": {
        "properties": {
          "t": {
            "type": "nested",
            "properties": {
              "categories": {
                "type": "nested",
                "properties": {
                  "name": {
                    "type": "text"
                  },
                  "list": {
                    "type": "nested",
                    "properties": {
                      "url_site": {
                        "type": "text"
                      },
                      "persons": {
                        "type": "nested",
                        "properties": {
                          "total_customers": {
                            "type": "integer"
                          },
                          "total_subscribers": {
                            "type": "integer"
                          },
                          "details": {
                            "type": "nested",
                            "properties": {
                              "person_id": {
                                "type": "text"
                              },
                              "person_date_registration": {
                                "type": "date"
                              },
                              "person_date_subscription": {
                                "type": "date"
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }

用于索引示例文档的API

PUT http://localhost:9200/t/_doc/1
        {
          "categories" : {
            "name" : "cat1",
            "list" : {
              "url_site" : "www.bla.org",
              "persons" : {
                "total_customers" : 10,
                "total_subscribers" : 10,
                "details" : {
                  "person_id" : 1
                }
              }
            }
          }
        }



PUT http://localhost:9200/t/_doc/2
    {
      "categories" : {
        "name" : "cat2",
        "list" : {
          "url_site" : "www.bleep.org",
          "persons" : {
            "total_customers" : 10,
            "total_subscribers" : 10,
            "details" : {
              "person_id" : 2
            }
          }
        }
      }
    }

PUT http://localhost:9200/t/_doc/3
    {
      "categories" : {
        "name" : "cat3",
        "list" : {
          "url_site" : "www.blubb.org",
          "persons" : {
            "total_customers" : 10,
            "total_subscribers" : 10,
            "details" : {
              "person_id" : 3
            }
          }
        }
      }
    }

搜索API

 GET http://localhost:9200/t/_search
        {
          "query": {
            "nested": {
              "path": "categories",
              "query": {
                "nested": {
                  "path": "categories.list",
                  "query": {
                    "nested": {
                      "path": "categories.list.persons.details",
                      "query": {
                        "bool": {
                          "must": {
                            "match": {
                              "categories.list.persons.details.person_id": 1
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }

查询应在特定条件下返回给定人员的数据,但会出现错误:

"index_uuid": "Np_exl7iSrysn-ixnMeLOw",
          "index": "t",
          "caused_by": {
            "type": "illegal_state_exception",
            "reason": "[nested] nested object under path [categories] is not of nested type"
          }

1 个答案:

答案 0 :(得分:1)

我在本地系统中再次尝试了此操作,并且能够使用您的映射创建索引,为3个示例文档建立了索引,并能够使用查询进行搜索。我唯一更改的是映射,并且提供了映射,该映射是在删除JSON中的"type": "nested",键时使用的。

{
    "mappings": {
        "properties": {
            "categories": {
                "type": "nested",
                "properties": {
                    "name": {
                        "type": "text"
                    },
                    "list": {
                        "type": "nested",
                        "properties": {
                            "url_site": {
                                "type": "text"
                            },
                            "persons": {
                                "type": "nested",
                                "properties": {
                                    "total_customers": {
                                        "type": "integer"
                                    },
                                    "total_subscribers": {
                                        "type": "integer"
                                    },
                                    "details": {
                                        "type": "nested",
                                        "properties": {
                                            "person_id": {
                                                "type": "text"
                                            },
                                            "person_date_registration": {
                                                "type": "date"
                                            },
                                            "person_date_subscription": {
                                                "type": "date"
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

您还可以导入我的邮递员收藏并亲自尝试https://www.getpostman.com/collections/2098eb05863cb8f8e419