Elasticsearch Suggestions using shingle

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

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

SETTINGS

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

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

MAPPINGS

POST store/_mappings
{
  "properties": {
    "name": {
      "type": "text",
      "fields": {
        "suggestions": {
          "type": "text",
          "analyzer": "suggestions",
          "fielddata": true
        }
      }
    }
  }
}
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

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

RESULTS

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

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