1
votes

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"
          }
1
I am not able to create a index with your mappinguser156327
I just re checked the same json, its working fine for me.I am using elastic search 7.2.0jimy page
can you please accept and upvote my answer as it solves ur query, this would help the community to find the answers easily also let me know if you have a follow-up questionuser156327

1 Answers

0
votes

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