# Distinct List of Hits after Filter

**URL:** <https://discuss.elastic.co/t/distinct-list-of-hits-after-filter/67342>\
**Category:** Elasticsearch\
**Created:** [November 28, 2016, 12:16pm UTC](https://discuss.elastic.co/t/distinct-list-of-hits-after-filter/67342 "2016-11-28T12:16:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fungus1487](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fungus1487/32/13452_2.png) [@fungus1487](https://discuss.elastic.co/u/fungus1487)\
**Post date:** [November 28, 2016, 12:16pm UTC](https://discuss.elastic.co/t/distinct-list-of-hits-after-filter/67342/1 "2016-11-28T12:16:56Z")

</div>

Hi all, new to ElasticSearch and I have made good progress with a number of queries I am looking to execute. However one seems to elude me.

I have the following document structure.

```
{
  "resourceId": 101,
  "sourceId": 201,
  "sourceTypeId": 301,
  "attributeId": 401,
  "term": "The cat sat on the mat"
}

```

There can exist multiple documents with the same resourceId but never a document with the same attributeId and resourceId (my composite key).

I am currently running this query to get me all results that match a sourceTypeId and attributeId (or number of them) and a search term:

```
{
  "size": 20,
  "from": 0,
  "query" : {
    "bool": {
      "filter": [ {
        "terms": {
          "sourceTypeId": [1000150]
        }
      }, {
        "terms": {
          "attributeId": [1000697]
        }
      } ],
      "must": {
        "fuzzy": { "term": "cat" }
      }
    }
  }
}

```

However I get duplicate resource id's which is to be expected, how would I go about extending this to return only documents with a distinct resourceId?

I am using AWS ElasticSearch so am locked to version 2.3 if that helps.

---

<div class="post-metadata">

**Author:** ![fungus1487](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fungus1487/32/13452_2.png) [@fungus1487](https://discuss.elastic.co/u/fungus1487)\
**Post date:** [November 28, 2016, 12:57pm UTC](https://discuss.elastic.co/t/distinct-list-of-hits-after-filter/67342/2 "2016-11-28T12:57:48Z")

</div>

Realized I might need more data for people to help out, so given this example set of documents:

```
{
  "resourceId": 101,
  "sourceId": 201,
  "sourceTypeId": 301,
  "attributeId": 401,
  "term": "The cat sat on the mat"
}, {
  "resourceId": 101,
  "sourceId": 201,
  "sourceTypeId": 301,
  "attributeId": 402,
  "term": "Fat"
}, {
  "resourceId": 102,
  "sourceId": 201,
  "sourceTypeId": 301,
  "attributeId": 401,
  "term": "Prat"
}, {
  "resourceId": 102,
  "sourceId": 201,
  "sourceTypeId": 301,
  "attributeId": 402,
  "term": "Double drat!"
}, {
  "resourceId": 103,
  "sourceId": 201,
  "sourceTypeId": 301,
  "attributeId": 404,
  "term": "Nothing fits here"
}

```

I want to fuzzy query on the field "term" for the text "at" and return the following resource id's:

[101, 102]

I need to be able to paginate over this data too as there are potentially thousands of distinct resource id's that will match.

So I believe I want some sort of size/from support once the query has been made and the documents have been made distinct. Hope that's clearer?

---

<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:** [December 26, 2016, 12:58pm UTC](https://discuss.elastic.co/t/distinct-list-of-hits-after-filter/67342/3 "2016-12-26T12:58:03Z")

</div>

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