溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

ES中怎么添加 IK 分詞器

發布時間:2021-06-22 14:54:54 來源:億速云 閱讀:243 作者:Leah 欄目:大數據

ES中怎么添加 IK 分詞器,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

1.下載IK分詞器,一定要注意和ES的版本一致

 

2 .下載之后放到 ES 的 \plugins 目錄下面去  重啟 ES 服務

測試:http://localhost:9200/blog1/_analyze    

{
   "text":"中華人民共和國MN","tokenizer": "ik_max_word"
}

結果:

{
    "tokens": [
        {
            "token": "中華人民共和國",
            "start_offset": 0,
            "end_offset": 7,
            "type": "CN_WORD",
            "position": 0
        },
        {
            "token": "中華人民",
            "start_offset": 0,
            "end_offset": 4,
            "type": "CN_WORD",
            "position": 1
        },
        {
            "token": "中華",
            "start_offset": 0,
            "end_offset": 2,
            "type": "CN_WORD",
            "position": 2
        },
        {
            "token": "華人",
            "start_offset": 1,
            "end_offset": 3,
            "type": "CN_WORD",
            "position": 3
        },
        {
            "token": "人民共和國",
            "start_offset": 2,
            "end_offset": 7,
            "type": "CN_WORD",
            "position": 4
        },
        {
            "token": "人民",
            "start_offset": 2,
            "end_offset": 4,
            "type": "CN_WORD",
            "position": 5
        },
        {
            "token": "共和國",
            "start_offset": 4,
            "end_offset": 7,
            "type": "CN_WORD",
            "position": 6
        },
        {
            "token": "共和",
            "start_offset": 4,
            "end_offset": 6,
            "type": "CN_WORD",
            "position": 7
        },
        {
            "token": "國",
            "start_offset": 6,
            "end_offset": 7,
            "type": "CN_CHAR",
            "position": 8
        },
        {
            "token": "mn",
            "start_offset": 7,
            "end_offset": 9,
            "type": "ENGLISH",
            "position": 9
        }
    ]
}
  1. ik_max_word 和 ik_smart 什么區別?

ik_max_word: 會將文本做最細粒度的拆分,比如會將“中華人民共和國國歌”拆分為“中華人民共和國,中華人民,中華,華人,人民共和國,人民,人,民,共和國,共和,和,國國,國歌”,會窮盡各種可能的組合,適合 Term Query;

ik_smart: 會做最粗粒度的拆分,比如會將“中華人民共和國國歌”拆分為“中華人民共和國,國歌”,適合 Phrase 查詢。

ES中怎么添加 IK 分詞器

# 測試分詞器
GET _analyze
{
  "analyzer": "ik_smart",
  "text": "我愛你中國"
}

GET _analyze
{
  "analyzer": "ik_max_word",
  "text": "我愛你中國"
}


# 存儲數據
PUT /test3/_doc/1
{
  "name":"施爺",
  "age":13,
  "birth":"2020-07-05"
}

# 修改數據 (全部修改,birth沒有會被刪除)
PUT /test3/_doc/1
{
  "name":"施爺222",
  "age":13
}

# 修改數據,只會修改name這個屬性,別的不會變
POST /test3/_doc/1/_update
{
  "doc":{
    "name":"我是用post方式進行了修改"
  }
}


# 獲取對象結構
GET /test3

# 通過id來獲取文檔
GET /test3/_doc/1

# 查看數據庫中全部存儲的統計信息
GET _cat/indices?v

# 刪除數據
DELETE /test3/_doc/1


# 存儲,跟新數據
PUT /shiye/user/6
{
  "name":"shiye施爺成績好",
  "age":30,
  "desc":"一看操作猛如虎,一戰戰績0-5",
  "tags":["靚仔","旅游","爬山"]
}

# 通過id來查詢數據
GET /shiye/user/A001

 # 通過名稱搜索
 GET /shiye/user/_search?q=name:shiye
 
 # 通過構建復雜查詢,查詢指定屬性_source
 GET /shiye/user/_search
 {
   "query": {
     "match": {
       "name": "shiye"
     }
   },
   "_source":["name","desc","age"]
 }

# must查詢 相當于 and
GET /shiye/user/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "shiye"
          }
        },{
          "match": {
            "name": "施爺"
          }
        }
      ]
    }
  }
}


# should 查詢,相當于or
GET /shiye/user/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "name": "shiye"
          }
        },{
          "match": {
            "name": "施爺"
          }
        }
      ]
    }
  }
}

# 加上 must+filter 
GET /shiye/user/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "shiye"
          }
        }
      ],
      "filter": {
        "range": {
          "age": {
            "gte": 20,
            "lte": 40
          }
        }
      }
    }
  }
}

# 查詢 tags 中匹配到山的
GET /shiye/user/_search
{
  "query": {
    "match": {
      "tags": "山"
    }
  }
}

#############測試 text,keyword ###############
# text     可以分詞
# keyword  不分詞
# 指定索引各個字段的創建規則
PUT testdb
{
  "mappings": {
    "properties":{
      "name":{
        "type":"text"
      },
      "desc":{
        "type": "keyword"
      }
    }
  }
}

# 添加數據
PUT testdb/_doc/2
{
  "name":"武松",
  "desc":"打老虎"
}

# 查詢
GET testdb/_search
{
  "query": {
    "match": {
      "desc": "軍事"
    }
  }
}

#查詢+高亮
GET testdb/_search
{
  "query": {
    "match": {
      "name": "施"
    }
  },
  "highlight": {
    "pre_tags": "<p style='color:red'>", 
    "post_tags": "</p>", 
    "fields": {"name":{}}
  }
}

看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

es
AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女