Get count of parent documents only

Hi all,

I have a document which has some nested documents. When I do something like the following, the count that comes back includes the nested document:

GET loandetails2/loandetail/_count
{
"query": {
"bool": {
"should": [
{
"match": {
"visibility": {
"query": "3"
}
}
}
]
}
}
}

The above query is just a simplified version of what I'm actually doing. Sometimes I'm really trying to get a count based on fields in the parent document, and fields in the nested documents. But I only want the count that returns to be the number of parent documents.

How do I do that?

hey

you mixed a bit of different concepts in elasticsearch terminology here. nested is something different than parent/child than inner objects. Can you maybe explain your data model, and also provide the real query, plus sample documents, so that other can reproduce this?

--Alex

You're right, I'm not entirely sure the difference between those two concepts. I've simplified the data model for your purposes, and I've provided the mapping below:

  "loandetails": {
    "mappings": {
      "loandetail": {
        "properties": {
          "administrativeAgent": {
            "properties": {
              "id": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "name": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              }
            }
          },
          "properties": {
            "type": "nested",
            "properties": {
              "areaTotalsTotal": {
                "type": "long"
              },
              "markets": {
                "properties": {
                  "id": {
                    "type": "text",
                    "fields": {
                      "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                      }
                    }
                  },
                  "name": {
                    "type": "text",
                    "fields": {
                      "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                      }
                    }
                  }
                }
              },
              "municipality": {
                "properties": {
                  "id": {
                    "type": "text",
                    "fields": {
                      "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                      }
                    }
                  },
                  "name": {
                    "type": "text",
                    "fields": {
                      "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                      }
                    }
                  }
                }
              },
              "name": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "propertySubType": {
                "type": "keyword"
              },
              "propertyType": {
                "type": "keyword"
              }
            }
          },
          "purpose": {
            "type": "keyword"
          },
          "structure": {
            "type": "keyword"
          },
         "visibility": {
            "type": "integer"
          }
        }
      }
    }
  }
}

And a real query would be a simple query like the following:

GET loandetails/loandetail/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "visibility": 1
          }
        }
      ]
    }
  }
}

Is this sufficient, or do you really need the sample documents. If you need that, I'd rather not put them in this forum.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.