# Elasticsearch Suggestions using shingle

**URL:** <https://discuss.elastic.co/t/elasticsearch-suggestions-using-shingle/214699>\
**Category:** Elasticsearch\
**Created:** [January 11, 2020, 9:23am UTC](https://discuss.elastic.co/t/elasticsearch-suggestions-using-shingle/214699 "2020-01-11T09:23:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dobra\_Adrian](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dobra_adrian/32/60656_2.png) [@Dobra\_Adrian](https://discuss.elastic.co/u/Dobra_Adrian)\
**Post date:** [January 11, 2020, 9:23am UTC](https://discuss.elastic.co/t/elasticsearch-suggestions-using-shingle/214699/1 "2020-01-11T09:23:40Z")

</div>

The ideea is to suggest words (search-as-you-type) like Amazon searchbox suggestions

 ![amazon](https://us1.discourse-cdn.com/elastic/original/3X/f/2/f2d0d5883c72b8d16c14738d5f61831e55f5c0fe.png)

I have a working solution but i don't think is the better one.

SETTINGS

```auto
PUT store
{
  "settings": {
    "max_shingle_diff" : 50,
    "analysis": {
      "analyzer": {
        "suggestions": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": ["suggestions_shingle", "lowercase"]
        }
      },
      "filter": {
        "suggestions_shingle": {
          "type": "shingle",
          "min_shingle_size": 2,
          "max_shingle_size": 50
        }
      }
    }
  }
}

```

ANALYZER

```auto
POST store/_analyze
{
  "analyzer": "suggestions",
  "text": "Telefon mobil Samsung S10"
}

```

MAPPINGS

```auto
POST store/_mappings
{
  "properties": {
    "name": {
      "type": "text",
      "fields": {
        "suggestions": {
          "type": "text",
          "analyzer": "suggestions",
          "fielddata": true
        }
      }
    }
  }
}

```

```auto
POST store/_doc
{

 "name": "Apple iPhone X (64GB) - Silver"
}

POST store/_doc
{
 "name": "Apple iPhone 11 (128GB) - White"
}

POST store/_doc
{
 "name": "Apple iPhone X (64 GB) - Space Grey",
}

POST store/_doc
{
 "name": "Apple iPhone 6s 32GB Rose Gold",
}

POST store/_doc
{
 "name": "Samsung Galaxy A10 Dual-SIM 32GB",
}

POST store/_doc
{
 "name": "Samsung Galaxy A70 Dual-SIM 128GB",
}

POST store/_doc
{
 "name": "Samsung Galaxy S10 128 GB",

}

POST store/_doc
{
 "name": "Samsung Galaxy M30s Black"
}

POST store/_doc
{
 "name": "Samsung Galaxy A20e"
}

```

SEARCH

```auto
GET store/_search
{
  "size": 0,
  "aggs":{
    "description_suggestions":{
      "terms":{
        "field":"name.suggestions",
        "include":"ip(.*)",
        "size": 4
      }
    }
  }
}

```

RESULTS

```auto
{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : []
  },
  "aggregations" : {
    "description_suggestions" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 3,
      "buckets" : [
        {
          "key" : "iphone",
          "doc_count" : 2
        },
        {
          "key" : "iphone 11",
          "doc_count" : 1
        },
        {
          "key" : "iphone 11 128gb",
          "doc_count" : 1
        },
        {
          "key" : "iphone 11 128gb white",
          "doc_count" : 1
        }
      ]
    }
  }
}

```

I don't think this is the better solution, is there a way to optimize this?

---

<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:** [February 8, 2020, 9:23am UTC](https://discuss.elastic.co/t/elasticsearch-suggestions-using-shingle/214699/2 "2020-02-08T09:23:41Z")

</div>

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