Alibaba Cloud Elasticsearch memungkinkan Anda memperbarui file kamus sinonim yang telah diunggah. Setelah mengunggah file kamus sinonim yang diperbarui, Anda dapat menerapkannya ke kluster Alibaba Cloud Elasticsearch dan menggunakan kamus baru untuk pencarian. Anda dapat menggunakan sinonim melalui salah satu dari dua metode berikut: menggunakan file kamus sinonim atau merujuk sinonim. Topik ini menjelaskan cara menggunakan kedua metode tersebut.
Informasi latar belakang
Semua perintah dalam topik ini dapat dijalankan di konsol Kibana. Untuk informasi tentang cara masuk ke konsol Kibana, lihat Masuk ke Konsol Kibana.Metode 1: Gunakan file kamus sinonim
Prasyarat: File kamus sinonim telah diunggah. Untuk informasi lebih lanjut, lihat Unggah File Kamus Sinonim.
Dalam contoh berikut, sinonim digunakan berdasarkan filter, dengan file aliyun_synonyms.txt yang berisi begin, start sebagai file uji.
- Jalankan perintah berikut untuk membuat indeks:
PUT /aliyun-index-test { "settings": { "index":{ "analysis": { "analyzer": { "by_smart": { "type": "custom", "tokenizer": "ik_smart", "filter": ["by_tfr","by_sfr"], "char_filter": ["by_cfr"] }, "by_max_word": { "type": "custom", "tokenizer": "ik_max_word", "filter": ["by_tfr","by_sfr"], "char_filter": ["by_cfr"] } }, "filter": { "by_tfr": { "type": "stop", "stopwords": [" "] }, "by_sfr": { "type": "synonym", "synonyms_path": "analysis/aliyun_synonyms.txt" } }, "char_filter": { "by_cfr": { "type": "mapping", "mappings": ["| => |"] } } } } } } - Konfigurasikan bidang sinonim title.
- Perintah untuk kluster Elasticsearch versi sebelum V7.0:
PUT /aliyun-index-test/_mapping/doc { "properties": { "title": { "type": "text", "analyzer": "by_max_word", "search_analyzer": "by_smart" } } } - Perintah untuk kluster Elasticsearch versi V7.0 atau lebih baru:
PUT /aliyun-index-test/_mapping/ { "properties": { "title": { "type": "text", "analyzer": "by_max_word", "search_analyzer": "by_smart" } } }null Pada Elasticsearch sumber terbuka versi 7.0 dan lebih baru, tipe pemetaan sudah tidak digunakan lagi, dan_docsecara otomatis digunakan. Anda tidak perlu menentukan tipe pemetaan dalam konfigurasi pemetaan suatu indeks. Jika Anda menentukan tipe pemetaan, kesalahan akan dilaporkan.
- Perintah untuk kluster Elasticsearch versi sebelum V7.0:
- Jalankan perintah berikut untuk memverifikasi sinonim:
GET /aliyun-index-test/_analyze { "analyzer": "by_smart", "text":"begin" }Jika perintah berhasil dijalankan, hasil berikut dikembalikan:{ "tokens": [ { "token": "begin", "start_offset": 0, "end_offset": 5, "type": "ENGLISH", "position": 0 }, { "token": "start", "start_offset": 0, "end_offset": 5, "type": "SYNONYM", "position": 0 } ] } - Tambahkan data untuk pengujian lebih lanjut.
- Perintah untuk kluster Elasticsearch versi sebelum V7.0:
PUT /aliyun-index-test/doc/1 { "title": "Shall I begin?" }PUT /aliyun-index-test/doc/2 { "title": "I start work at nine." } - Perintah untuk kluster Elasticsearch versi V7.0 atau lebih baru:
PUT /aliyun-index-test/_doc/1 { "title": "Shall I begin?" }PUT /aliyun-index-test/_doc/2 { "title": "I start work at nine." }
- Perintah untuk kluster Elasticsearch versi sebelum V7.0:
- Jalankan perintah berikut untuk melakukan tes pencarian dan memverifikasi sinonim:
GET /aliyun-index-test/_search { "query" : { "match" : { "title" : "begin" }}, "highlight" : { "pre_tags" : ["<red>", "<bule>"], "post_tags" : ["</red>", "</bule>"], "fields" : { "title" : {} } } }Jika perintah berhasil dijalankan, hasil berikut dikembalikan:{ "took": 11, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 2, "max_score": 0.41048482, "hits": [ { "_index": "aliyun-index-test", "_type": "doc", "_id": "2", "_score": 0.41048482, "_source": { "title": "I start work at nine." }, "highlight": { "title": [ "I <red>start</red> work at nine." ] } }, { "_index": "aliyun-index-test", "_type": "doc", "_id": "1", "_score": 0.39556286, "_source": { "title": "Shall I begin?" }, "highlight": { "title": [ "Shall I <red>begin</red>?" ] } } ] } }
Metode 2: Referensi sinonim
Dalam contoh berikut, sinonim dirujuk dan tokenizer ik_smart digunakan untuk pemisahan kata.
- Jalankan perintah berikut untuk membuat indeks:
PUT /my_index { "settings": { "analysis": { "analyzer": { "my_synonyms": { "filter": [ "lowercase", "my_synonym_filter" ], "tokenizer": "ik_smart" } }, "filter": { "my_synonym_filter": { "synonyms": [ "begin,start" ], "type": "synonym" } } } } }Perintah sebelumnya bekerja berdasarkan prinsip-prinsip berikut:- Konfigurasikan filter sinonim my_synonym_filter dan kamus sinonim.
- Konfigurasikan analyzer my_synonyms dan gunakan tokenizer ik_smart untuk memisahkan kata.
- Setelah pemisahan kata selesai, gunakan tokenizer ik_smart untuk mengubah semua huruf menjadi huruf kecil dan memproses semua kata yang dipisah sebagai sinonim.
- Konfigurasikan bidang sinonim title.
- Perintah untuk kluster Elasticsearch versi sebelum V7.0:
PUT /my_index/_mapping/doc { "properties": { "title": { "type": "text", "analyzer": "my_synonyms" } } } - Perintah untuk kluster Elasticsearch versi V7.0 atau lebih baru:
PUT /my_index/_mapping/ { "properties": { "title": { "type": "text", "analyzer": "my_synonyms" } } }null Pada Elasticsearch sumber terbuka versi 7.0 dan lebih baru, tipe pemetaan sudah tidak digunakan lagi, dan_docsecara otomatis digunakan. Anda tidak perlu menentukan tipe pemetaan dalam konfigurasi pemetaan suatu indeks. Jika Anda menentukan tipe pemetaan, kesalahan akan dilaporkan.
- Perintah untuk kluster Elasticsearch versi sebelum V7.0:
- Jalankan perintah berikut untuk memverifikasi sinonim:
GET /my_index/_analyze { "analyzer":"my_synonyms", "text":"Shall I begin?" }Jika perintah berhasil dijalankan, hasil berikut dikembalikan:{ "tokens": [ { "token": "shall", "start_offset": 0, "end_offset": 5, "type": "ENGLISH", "position": 0 }, { "token": "i", "start_offset": 6, "end_offset": 7, "type": "ENGLISH", "position": 1 }, { "token": "begin", "start_offset": 8, "end_offset": 13, "type": "ENGLISH", "position": 2 }, { "token": "start", "start_offset": 8, "end_offset": 13, "type": "SYNONYM", "position": 2 } ] } - Tambahkan data untuk pengujian lebih lanjut.
- Perintah untuk kluster Elasticsearch versi sebelum V7.0:
PUT /my_index/doc/1 { "title": "Shall I begin?" }PUT /my_index/doc/2 { "title": "I start work at nine." } - Perintah untuk kluster Elasticsearch versi V7.0 atau lebih baru:
PUT /my_index/_doc/1 { "title": "Shall I begin?" }PUT /my_index/_doc/2 { "title": "I start work at nine." }
- Perintah untuk kluster Elasticsearch versi sebelum V7.0:
- Jalankan perintah berikut untuk melakukan tes pencarian dan memverifikasi sinonim:
GET /my_index/_search { "query" : { "match" : { "title" : "begin" }}, "highlight" : { "pre_tags" : ["<red>", "<bule>"], "post_tags" : ["</red>", "</bule>"], "fields" : { "title" : {} } } }Jika perintah berhasil dijalankan, hasil berikut dikembalikan:{ "took": 11, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 2, "max_score": 0.41913947, "hits": [ { "_index": "my_index", "_type": "doc", "_id": "2", "_score": 0.41913947, "_source": { "title": "I start work at nine." }, "highlight": { "title": [ "I <red>start</red> work at nine." ] } }, { "_index": "my_index", "_type": "doc", "_id": "1", "_score": 0.39556286, "_source": { "title": "Shall I begin?" }, "highlight": { "title": [ "Shall I <red>begin</red>?" ] } } ] } }