# How do I return only one result per type?

**URL:** <https://discuss.elastic.co/t/how-do-i-return-only-one-result-per-type/78936>\
**Category:** Elasticsearch\
**Created:** [March 16, 2017, 9:35pm UTC](https://discuss.elastic.co/t/how-do-i-return-only-one-result-per-type/78936 "2017-03-16T21:35:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![coogle](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/coogle/32/15175_2.png) [@coogle](https://discuss.elastic.co/u/coogle)\
**Post date:** [March 16, 2017, 9:35pm UTC](https://discuss.elastic.co/t/how-do-i-return-only-one-result-per-type/78936/1 "2017-03-16T21:35:50Z")

</div>

Hello,

I have a nested mapping as follows:

```auto
{
  "fbs": {
    "mappings": {
      "institution": {
        "properties": {
          "document": {
            "type": "nested",
            "properties": {
              "document_id": {
                "type": "long"
              },
              "expiration_date": {
                "type": "date"
              },
              "flags": {
                "type": "text",
                "norms": false,
                "analyzer": "comma"
              },
              "is_active": {
                "type": "boolean"
              },
              "is_current": {
                "type": "boolean"
              },
              "name": {
                "type": "text"
              },
              "section": {
                "type": "nested",
                "properties": {
                  "created_at": {
                    "type": "date"
                  },
                  "data": {
                    "type": "text",
                    "fields": {
                      "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                      }
                    }
                  },
                  "file": {
                    "type": "nested",
                    "properties": {
                      "author": {
                        "type": "text",
                        "fields": {
                          "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                          }
                        }
                      },
                      "content": {
                        "type": "text",
                        "analyzer": "standard",
                        "search_analyzer": "fbs_search_analyzer"
                      },
                      "content_length": {
                        "type": "long"
                      },
                      "content_type": {
                        "type": "text",
                        "fields": {
                          "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                          }
                        }
                      },
                      "date": {
                        "type": "date"
                      },
                      "keywords": {
                        "type": "text",
                        "fields": {
                          "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                          }
                        }
                      },
                      "language": {
                        "type": "text",
                        "fields": {
                          "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                          }
                        }
                      },
                      "title": {
                        "type": "text",
                        "fields": {
                          "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                          }
                        }
                      }
                    }
                  },
                  "filename": {
                    "type": "text",
                    "norms": false
                  },
                  "fingerprint": {
                    "type": "text",
                    "norms": false
                  },
                  "flags": {
                    "type": "text",
                    "norms": false,
                    "analyzer": "comma"
                  },
                  "is_active": {
                    "type": "boolean"
                  },
                  "name": {
                    "type": "text"
                  },
                  "section_id": {
                    "type": "long"
                  },
                  "updated_at": {
                    "type": "date"
                  }
                }
              },
              "start_date": {
                "type": "date"
              }
            }
          },
          "institution_id": {
            "type": "long"
          },
          "name_en": {
            "type": "text",
            "norms": false
          },
          "name_fr": {
            "type": "text",
            "norms": false
          },
          "region_id": {
            "type": "long"
          }
        }
      }
    }
  }
}

```

I am currently searching on `document.section.file.content` for example to pull documents out that match my keywords. However, what I want to do is limit the results to only _one_ match (the highest relevancy) per unique document\_id.

I.e. I have a single logical "document" that is represented by 10 PDF "sections", I am searching those sections but I only want to return the top result per document (i.e. limit 1 result per document\_id)

Is there a way I can coax ES into doing this or am I better off just post-processing the results?

John

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [March 16, 2017, 10:05pm UTC](https://discuss.elastic.co/t/how-do-i-return-only-one-result-per-type/78936/2 "2017-03-16T22:05:07Z")

</div>

I'd use `msearch` for that.

---

<div class="post-metadata">

**Author:** ![coogle](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/coogle/32/15175_2.png) [@coogle](https://discuss.elastic.co/u/coogle)\
**Post date:** [March 17, 2017, 12:45am UTC](https://discuss.elastic.co/t/how-do-i-return-only-one-result-per-type/78936/3 "2017-03-17T00:45:25Z")

</div>

I'm not sure how msearch would be used to limit the results to one section per document?

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [March 17, 2017, 2:34pm UTC](https://discuss.elastic.co/t/how-do-i-return-only-one-result-per-type/78936/4 "2017-03-17T14:34:37Z")

</div>

Ah, reread your problem. I thought you wanted something else. I don't know nested documents super well so I can't help you off hand. There might be something special for nested documents I don't know about, but this problem looks similar to [field collapsing](https://www.elastic.co/guide/en/elasticsearch/reference/5.x/search-request-collapse.html) which is implemented in the next minor version of Elasticsearch.

---

<div class="post-metadata">

**Author:** ![coogle](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/coogle/32/15175_2.png) [@coogle](https://discuss.elastic.co/u/coogle)\
**Post date:** [March 18, 2017, 6:31am UTC](https://discuss.elastic.co/t/how-do-i-return-only-one-result-per-type/78936/5 "2017-03-18T06:31:54Z")

</div>

I was able in the end to mostly solve my problem by using a top\_hits aggregation. Unfortunately one of the problems there is that in doing so I've lost the ability to page results. Thankfully I have a small enough data set where I can programmatically page once I get the results back without too much trouble but I really wish ES had some sort of way of doing that internally.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 15, 2017, 6:32am UTC](https://discuss.elastic.co/t/how-do-i-return-only-one-result-per-type/78936/6 "2017-04-15T06:32:40Z")

</div>

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