ES-同词义配置

定义

  • 语主相近或相同
  • 品牌类目关联
  • 搜索它等于搜索它

配置方法

1. 进入ES第一个节点:cd node1/config/analysis-ik

添加近义词
echo '凯悦,锡伯,红桃' > vim synonyms.txt

2. 将配置得制到节点2与节点3

cp synonyms.txt /opt/elasticsearch8.0.0/node2/config/analysis-ik/
cp synonyms.txt /opt/elasticsearch8.0.0/node3/config/analysis-ik/

3. 修改结构(定义支持同义词的门店索引结构)

要点:

增加analysis,处定义分词器
修改name的分词器

DELETE /shop

PUT /shop
{
  "settings": {
    "number_of_shards": 1, 
    "number_of_replicas": 1,
    "analysis": {
      "filter": {
          "my_synonym_filter": {
              "type": "synonym",
              "synonyms_path": "analysis-ik/synonyms.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_word","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"}
    }
  }
}

4. 验证同义词是否载入

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

5. 其他

1). 重建索引

如果结构之前就存在,要让同义词起作用,需要先删除原索引再重建结构(如上)
如果是新索引,则不需要行该步骤 .

2). 在Kibana上验证

以下会查询相同的内容


GET /shop/_search
{
  "query": {
    "match": {
      "name": "锡伯"
    }
  }
}


GET /shop/_search
{
  "query": {
    "match": {
      "name": "凯悦"
    }
  }
}

GET /shop/_search
{
  "query": {
    "match": {
      "name": "红桃"
    }
  }
}

你可能感兴趣的:(ES-同词义配置)