1. 程式人生 > 實用技巧 >elasticsearch 同義詞配置搜尋

elasticsearch 同義詞配置搜尋


同義詞的配置如下:


杯子,保溫杯




PUT tongyiciv2 {
"settings": { "analysis": { "filter": { "word_sync": { "type": "synonym", "synonyms_path": "analysis/synonym.txt" } }, "analyzer": { "ik_sync_smart": { "filter": [ "word_sync"
], "type": "custom", "tokenizer": "ik_smart" } } } }, "mappings": { "properties": { "goodsName": { "type": "text", "analyzer": "ik_sync_smart", "search_analyzer": "ik_sync_smart" }, "goodsContent
": { "type": "text", "analyzer": "ik_sync_smart", "search_analyzer": "ik_sync_smart" } } } }

POST tongyiciv2/_doc/1
{
"id":1,
"goodsName":"江蘇潮流杯子加個實惠"
}
POST tongyiciv2/_doc/2
{
"id":2,
"goodsName":"烏魯木齊潮流杯子樣式絕美"
}
POST tongyiciv2/_doc/3
{
"id":3,
"goodsName":"Momscook 潮流 保溫杯"
}


POST tongyiciv2/_doc/5
{
"id":4,
"goodsName":"上海潮流保溫杯好用"
}



============用"保溫杯"分詞後的結果===============
GET tongyiciv2/_analyze { "text": "保溫杯", "analyzer": "ik_sync_smart" }

{

"tokens" : [
{
"token" : "保溫杯",
"start_offset" : 0,
"end_offset" : 3,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "杯子",
"start_offset" : 0,
"end_offset" : 3,
"type" : "SYNONYM",
"position" : 0
}
]
}


============用"杯子"分詞後的結果===============

GET tongyiciv2/_analyze
{
"text": "杯子",
"analyzer": "ik_sync_smart"
}

{
"tokens" : [
{
"token" : "杯子",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "保溫杯",
"start_offset" : 0,
"end_offset" : 2,
"type" : "SYNONYM",
"position" : 0
}
]
}


===========用"杯子"查詢=================

GET tongyiciv2/_search
{
  "query": {
    "match": {
      "goodsName": "杯子"
    }
  }
}

===========用"保溫杯"查詢================= GET tongyiciv2
/_search { "query": { "match": { "goodsName": "保溫杯" } } }
查詢的結果是一樣的:

{
"took" : 7,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : 0.41501677,
"hits" : [
{
"_index" : "tongyiciv2",
"_type" : "_doc",
"_id" : "PFBLa3YBxN1KAZdSYRfw",
"_score" : 0.41501677,
"_source" : {
"id" : 1,
"goodsName" : "江蘇潮流杯子加個實惠"
}
},
{
"_index" : "tongyiciv2",
"_type" : "_doc",
"_id" : "R8Iba3YBNdF4r2WqF8xZ",
"_score" : 0.2876821,
"_source" : {
"id" : 4,
"goodsName" : "上海潮流保溫杯好用"
}
},
{
"_index" : "tongyiciv2",
"_type" : "_doc",
"_id" : "PVBLa3YBxN1KAZdSwRdi",
"_score" : 0.24309544,
"_source" : {
"id" : 2,
"goodsName" : "烏魯木齊潮流杯子樣式絕美"
}
},
{
"_index" : "tongyiciv2",
"_type" : "_doc",
"_id" : "RsIba3YBNdF4r2WqEcyq",
"_score" : 0.21110919,
"_source" : {
"id" : 3,
"goodsName" : "Momscook 潮流 保溫杯"
}
}
]
}
}

注意:

我是在本地測試,在配置或者刪除同義詞時要重啟es 伺服器 bin/.elasticsearch

同義詞配置有兩種:

1,a=>b: 通俗的來講,就是儘管使用者輸入的是a,但是es在查詢的是會轉成b去搜索,比如 保溫杯=>杯子,使用者輸入的是"保溫杯",但是es會用"杯子"去做搜尋

2,a,b:通俗的來講,就是不管使用者輸入的是a還是b,es在查詢的是用a,或者b搜尋.比如 保溫杯,杯子,使用者輸入的是"保溫杯",es會用"杯子"去做搜尋,也會用"保溫杯"搜尋