Distinct value and search

My documents are like this.

{
    "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

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.

{
  "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.

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

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

"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.

1 Like

Nice! Thank you!

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