天天看點

elasticsearch-建構同義詞、近義詞查詢

1.建立同義詞庫

elasticsearch-6.3.1\config\下建立檔案夾analysis

在檔案夾analysis下建立synonyms.txt檔案

其中synonyms用于存放同義詞庫,如:

蕃茄,番茄,洋芋,馬鈴薯

社保,公積金

(注意逗号是英文的逗号)

2.建立索引

索引建立流程:setting--> mapping-->put

在kibana6.3.1版本頁面中可以常見索引了,如下:

PUT /test

{

    "settings": {

    "index": {

      "analysis": {

        "analyzer": {

          "jt_cn": {

            "type": "custom",

            "use_smart": "true",

            "tokenizer": "ik_smart",

            "filter": ["jt_tfr","jt_sfr"],

            "char_filter": ["jt_cfr"]

          },

          "ik_smart": {

            "type": "ik_smart",

            "use_smart": "true"

          },

          "ik_max_word": {

            "type": "ik_max_word",

            "use_smart": "false"

          }

        },

        "filter": {

          "jt_tfr": {

            "type": "stop",

            "stopwords": [" "]

          },

          "jt_sfr": {

            "type": "synonym",

"synonyms_path": "analysis/synonyms.txt"

          }

        },

        "char_filter": {

            "jt_cfr": {

                "type": "mapping",

                "mappings": [

                    "| => \\|"

                ]

            }

        }

      }

    }

  }

}

注意标紅的部分指定了同義詞庫的位置

3.建立映射

PUT /test/haizhi/_mapping

{

    "haizhi": {

      "properties": {

        "title": {

          "analyzer": "jt_cn",

          "term_vector": "with_positions_offsets",

          "boost": 8,

          "store": true,

          "type": "text"

        }

      }

    }

 }

注意标紅處指定了剛才建立索引的分詞器jt_cn

4.測試-構模組化拟資料

PUT /test/haizhi/1

{

    "title": "番茄"

}

PUT /test/haizhi/2

{

    "title": "蕃茄"

}

PUT /test/haizhi/3

{

    "title": "我是蕃茄"

}

PUT /test/haizhi/4

{

    "title": "我是番茄"

}

PUT /test/haizhi/5

{

    "title": "洋芋"

}

PUT /test/haizhi/6

{

    "title": "aa"

}

5.測試-是否能檢索到同義詞

POST /test/haizhi/_search?pretty

{

  "query": {

    "match_phrase": {

      "title": {

        "query": "蕃茄",

        "analyzer": "jt_cn"

      }

    }

  },

"highlight": {

    "pre_tags": [

      "<tag1>",

      "<tag2>"

    ],

    "post_tags": [

      "</tag1>",

      "</tag2>"

    ],

    "fields": {

      "title": {}

    }

繼續閱讀