# Distinct value and search

**URL:** <https://discuss.elastic.co/t/distinct-value-and-search/86838>\
**Category:** Elasticsearch\
**Created:** [May 23, 2017, 4:47pm UTC](https://discuss.elastic.co/t/distinct-value-and-search/86838 "2017-05-23T16:47:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hairinwind](https://avatars.discourse-cdn.com/v4/letter/h/b487fb/32.png) [@hairinwind](https://discuss.elastic.co/u/hairinwind)\
**Post date:** [May 23, 2017, 4:47pm UTC](https://discuss.elastic.co/t/distinct-value-and-search/86838/1 "2017-05-23T16:47:28Z")

</div>

My documents are like this.

```auto
{
    "sourceTitle": "Arrival",
    "otherFields": ...
}
{
    "sourceTitle": "Arrival",
    "otherFields": ...
}
{
    "sourceTitle": "Eye in the Sky",
    "otherFields": ...
}

```

I need two types queries on the field sourceTitle.

First, I need get all distinct values of sourceTitle, like this

```auto
Arrival
Eye in the Sky

```

Second, I need to support search. For example, if the user input "eye", i need find all the documents which sourceTitle contains "eye".

For the first query, I am using the term aggregation.

```auto
{
  "size": 0, 
  "aggs": {
    "sourceTitle": {
      "terms": {
        "field": "sourceTitle",
        "size": 10
      }
    }
  }
}

```

Because the source title may contain space, to avoid tokenizing, I have to make sourceTilte type to be "keyword". Then the query for searching "eye" does not return anything to me. How can I support both queries.

```auto
{
  "query": {
    "match": {
      "sourceTitle": "Eye"
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [May 26, 2017, 8:50am UTC](https://discuss.elastic.co/t/distinct-value-and-search/86838/2 "2017-05-26T08:50:57Z")

</div>

If you were to use dynamic mapping when indexing your documents, Elasticsearch would map the `sourceTitle` as follows:

```auto
"sourceTitle": {
  "type": "text",
  "fields": {
    "keyword": {
      "type": "keyword"
    }
  }
}

```

You can specify this same mapping yourself, without relying on dynamic mapping.

Then you would query the `sourceTitle` field, but run aggregations on the `sourceTitle.keyword` field.

---

<div class="post-metadata">

**Author:** ![hairinwind](https://avatars.discourse-cdn.com/v4/letter/h/b487fb/32.png) [@hairinwind](https://discuss.elastic.co/u/hairinwind)\
**Post date:** [May 26, 2017, 5:57pm UTC](https://discuss.elastic.co/t/distinct-value-and-search/86838/3 "2017-05-26T17:57:09Z")

</div>

Nice! Thank you!

---

<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:** [June 23, 2017, 5:57pm UTC](https://discuss.elastic.co/t/distinct-value-and-search/86838/4 "2017-06-23T17:57:12Z")

</div>

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