# ElasticSearch query to filter out distinct results

**URL:** <https://discuss.elastic.co/t/elasticsearch-query-to-filter-out-distinct-results/243622>\
**Category:** Elasticsearch\
**Created:** [August 3, 2020, 8:20pm UTC](https://discuss.elastic.co/t/elasticsearch-query-to-filter-out-distinct-results/243622 "2020-08-03T20:20:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![karna98](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/karna98/32/46559_2.png) [@karna98](https://discuss.elastic.co/u/karna98)\
**Post date:** [August 3, 2020, 8:20pm UTC](https://discuss.elastic.co/t/elasticsearch-query-to-filter-out-distinct-results/243622/1 "2020-08-03T20:20:04Z")

</div>

Consider a table, where artifacts are mapped to publications.

```auto
------------------------------------------------------------------
   id | artifact_id | publication_id | A | B | C |   
------------------------------------------------------------------
   1 | 1 | 1 | _H_ | ___|___ |
------------------------------------------------------------------
   2 | 1 | 2 | ___| _H_ |___ |
------------------------------------------------------------------
   3 | 2 | 1 | ___| _H_ |___ |   
------------------------------------------------------------------

```

One artifact can have multiple publications, and the table has A, B, C fields(columns).

I'm searching a keyword in A, B, C using query\_string.

Using the following query

```auto

    {
        "query": {
            "query_string": {
                "query": "keyword_to_be_searched",
                "fields": ["A, B, C"]
            }
        },
       "from" : starting_no,
        "size": results_per_page
    }

```

if I search for keyword **H** which matched all the 3 rows (as shown in table).

The response returns all 3 records.

```auto
      # For Understanding purpose
       response : {
                "id" : 1,
                "id" : 2,
                "id" : 3
       }           

```

There are many hits for different publications, but many of them are mapped to the same artifacts.

In the end, I want to display **distinct artifacts** (no repetition)

```auto
      # For Understanding purpose
       Expected response: {
                "id" : 1,
                "id" : 3
       }
        
      OR 

      Expected response: {
                "id" : 2,
                "id" : 3
       }
           

```

SInce both **publication\_id** : **1** and **publication\_id** : **2** are mappd to same **artifact\_id** : **1**

How do I rewrite the query to group\_by or distinct by **artifact\_id** along with pagination?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 4, 2020, 9:26am UTC](https://discuss.elastic.co/t/elasticsearch-query-to-filter-out-distinct-results/243622/2 "2020-08-04T09:26:38Z")

</div>

Check out [field collapsing](https://www.elastic.co/guide/en/elasticsearch/reference/current/collapse-search-results.html), that might help you in this use-case

---

<div class="post-metadata">

**Author:** ![karna98](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/karna98/32/46559_2.png) [@karna98](https://discuss.elastic.co/u/karna98)\
**Post date:** [August 4, 2020, 3:30pm UTC](https://discuss.elastic.co/t/elasticsearch-query-to-filter-out-distinct-results/243622/3 "2020-08-04T15:30:21Z")

</div>

Hey, @spinscale thanks for the response.

I was able to get result with distinct artifacts.

And for pagination, I followed this article [Total hits with field collapsing](https://discuss.elastic.co/t/total-hits-with-field-collapsing/105061)

---

<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:** [September 1, 2020, 3:30pm UTC](https://discuss.elastic.co/t/elasticsearch-query-to-filter-out-distinct-results/243622/4 "2020-09-01T15:30:24Z")

</div>

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