2

有没有办法做得更好?

这个想法是建议像亚马逊搜索框建议那样的词(搜索时输入)。

搜索即键入示例图像

我有一个可行的解决方案,但我认为有更好的解决方案。

设置

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
        }
      }
    }
  }
}

分析仪

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

映射

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

和搜索

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

结果

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

我不认为这是最好的解决方案。有没有办法优化这个?

4

0 回答 0