1

I need to make nested query on elastic search 7.2. use case: Lets say I have array of categories and inside categories I have array of persons. I need to be able to fetch documents such as select * from documents where categories.name = "category1" and categories.person.personid=1.

I have prepared nested mappings and created dummy data. I was looking to an old example but when I run the nested query it is breaking. It is not able to create the queries. I have like 2 hrs of experience in Elastic search.

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 to indexed sample documents

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
            }
          }
        }
      }
    }

Search 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
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }

query should return data for a given person under specific condition but getting error:

"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"
          }
Amit
  • 30,756
  • 6
  • 57
  • 88
jimy page
  • 21
  • 3

1 Answers1

0

I tried this again in my local system and was able to create the index with your mapping, indexed your 3 sample documents and was able to search using your query. the only thing which I changed is the mapping and I am providing the mapping which I used where I removed the "type": "nested", key in the 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"
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

You can also import my postman collection and try it yourself https://www.getpostman.com/collections/2098eb05863cb8f8e419

Amit
  • 30,756
  • 6
  • 57
  • 88