同义词扩展

2020-06-12  本文已影响0人  乌鲁木齐001号程序员

导论

有些时候,需要给一些词配置同义词,比如用户搜“累了”,什么结果都搜不到,如果给“累了”配置了同义词“酒店”,其搜索结果和搜索“酒店”的结果一样。


创建 synomyms.txt

凯悦,锡箔,红桃

拷贝 synomyms.txt 到其他节点

重新定义索引 shop

# 删除索引 shop
DELETE /shop

# 重建索引 shop,使用自定义的,使用同义词过滤器的分词器
PUT /shop
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1,
    "analysis": {
      "filter": {
        "my_synonym_filter": {
          "type": "synonym",
          "synonyms_path": "../plugins/ik/config/synomyms.txt"
        }
      },
      "analyzer": {
        "ik_syno":{
          "type":"custom",
          "tokenizer":"ik_smart",
          "filter":["my_synonym_filter"]
        },
        "ik_syno_max":{
          "type":"custom",
          "tokenizer":"ik_max_word",
          "filter":["my_synonym_filter"]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "id":{"type": "integer"},
      "name":{"type": "text", "analyzer": "ik_syno_max", "search_analyzer": "ik_syno"},
      "tags":{"type": "text", "analyzer": "whitespace", "fielddata": true},
      "location":{"type": "geo_point"},
      "remark_score":{"type": "double"},
      "price_per_man":{"type": "integer"},
      "category_id":{"type": "integer"},
      "category_name":{"type":"keyword"},
      "seller_id":{"type": "integer"},
      "seller_remark_score":{"type":"double"},
      "seller_disabled_flag":{"type":"integer"}
    }
  }
}

重新分词 “凯悦”

GET /shop/_analyze
{
  "field": "name",
  "text": "凯悦"
}

---

{
  "tokens" : [
    {
      "token" : "凯悦",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "锡箔",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "SYNONYM",
      "position" : 0
    },
    {
      "token" : "红桃",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "SYNONYM",
      "position" : 0
    }
  ]
}

使用“凯悦”的同义词搜索,得到和搜“凯悦”一样的结果

GET /shop/_search
{
  "query": {
    "match": {
      "name": "锡箔"
    }
  }
}
上一篇下一篇

猜你喜欢

热点阅读