1

我需要在弹性搜索 7.2 上进行嵌套查询。用例:假设我有一系列类别,而内部类别中有很多人。我需要能够从 categories.name = "category1" 和 categories.person.personid=1 的文档中获取诸如 select * 之类的文档。

我已经准备好嵌套映射并创建了虚拟数据。我正在寻找一个旧示例,但是当我运行嵌套查询时,它正在中断。它无法创建查询。我在弹性搜索方面有 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"
          }
4

1 回答 1

0

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

{
    "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

于 2019-07-18T06:48:17.060 回答