Elasticsearch個人心得筆記(四)

                        ELASTICSEARCH的mapping設置


目錄

                        ELASTICSEARCH的mapping設置

一.mapping映射結構

1.1動態映射

2.靜態手動映射

3.對比數據庫

4.對象與json的轉化

1.一個普通類的對象

2.稍微複雜一些的對象

3.之前JSON數據轉換爲Java對象

二.ik分詞器插件引入到ELASTICSEARCH

1.文件拷貝

2.安裝依賴(已有不要安裝)

3.解壓

4.刪除安裝包

5.進入到Config文件夾

6.將elasticsearch的文件夾名稱修改成analysis-ik

7.重新啓動es加載plugins中的ik分詞器

8.訪問es提供的接口,實現不同的分詞器使用

9.配置擴展和停用詞典

1.plugins目錄下的ik分詞器中config文件夾中找到IKAnalyzer.cfg.xml

2.沒找到,去上傳一份

3.找錯文件夾 上一步可以不用做

4.配置擴展和停用

5.生成對應的詞典們

6.輸出結果

10.通過mapping設置將ik_max_word作爲字符串類型的分詞器使用

1.index05中,定義article

2.新建一個index05

3.在index05中添加一個article類型的自定義mapping結構

4.index05的article類型中新增幾個document

5.驗證是否使用了ik分詞器將title中java編程思想分詞計算爲{java}{編程}{思想}


​​​​​​​

一.mapping映射結構

1.1動態映射

在新增任何數據之前的空索引中,動態mapping沒有配置,空的內容

1.添加一個新的index03

2.查詢mapping 狀態

返回結果

{"index03":{"mappings":{}}}

  • 一旦添加任何數據到索引中,mapping結構會根據你的數據類型

  1. {
  2.     "index03": {
  3.         "mappings": {
  4.             "article": { //article類型定義的映射配置
  5.                 "properties": { //mapping中的具體屬性
  6.                     "content": { //content域的設置
  7.                         "type": "text", //字符串類型,並且根據text定義分詞計算
  8.                         "fields": { //對一個域的屬性做擴展
  9.                             "keyword": { //查詢可用域
  10.                                 "type": "keyword", //keyword類型相當於lucene底層使用的StringField類型, 整體也可以作爲一詞項使用
  11.                                 "ignore_above": 256//作爲整體使用時,字符串超過256字節,keyword類型就不生效了
  12.                            }
  13.                         }
  14.                     },
  15.                     "id": {
  16.                         "type": "text",
  17.                         "fields": {
  18.                             "keyword": {
  19.                                 "type": "keyword",
  20.                                 "ignore_above": 256
  21.                             }
  22.                         }
  23.                     },
  24.                     "title": {
  25.                         "type": "text",
  26.                         "fields": {
  27.                             "keyword": {
  28.                                 "type": "keyword",
  29.                                 "ignore_above": 256
  30.                             }
  31.                         }
  32.                     }
  33.                 }
  34.             }
  35.         }
  36.     }
  37. }
  38.  
  • 在index03中添加一個新的域,給定一個值不是字符串是整數"product_id":1
  1. {
  2.     "index03": {
  3.         "mappings": {
  4.             "article": {
  5.                 "properties": {
  6.                     "content": {
  7.                         "type": "text",
  8.                         "fields": {
  9.                             "keyword": {
  10.                                 "type": "keyword",
  11.                                 "ignore_above": 256
  12.                             }
  13.                         }
  14.                     },
  15.                     "id": {
  16.                         "type": "text",
  17.                         "fields": {
  18.                             "keyword": {
  19.                                 "type": "keyword",
  20.                                 "ignore_above": 256
  21.                             }
  22.                         }
  23.                     },
  24.                     "product_id": {
  25.                         "type": "long"
  26.                     },
  27.                     "title": {
  28.                         "type": "text",
  29.                         "fields": {
  30.                             "keyword": {
  31.                                 "type": "keyword",
  32.                                 "ignore_above": 256
  33.                             }
  34.                         }
  35.                     }
  36.                 }
  37.             }
  38.         }
  39.     }
  40. }

對於某個索引,中某個類型中的某個域屬性,一旦動態映射完成,無法修改;添加數據之前提前將mapping做好

2.靜態手動映射

新增一個index04,新增數據之前,將其對應artilce類型中的content只定義爲text,id定義爲"integer"/"int",title定義爲"text",imgUrl定義爲"keyword"

新建完自定義的mapping重新獲取index04的內容

  1. {
  2.     "index04": {
  3.         "mappings": {
  4.             "book": {
  5.                 "properties": {
  6.                     "content": {
  7.                         "type": "text"
  8.                     },
  9.                     "id": {
  10.                         "type": "integer"
  11.                     },
  12.                     "imgUrl": {
  13.                         "type": "keyword"
  14.                     },
  15.                     "title": {
  16.                         "type": "text"
  17.                     }
  18.                 }
  19.             }
  20.         }
  21.     }
  22. }

3.對比數據庫

mapping結構設置,與數據庫中定義一個表格的結構(scheming),是一樣的意義

4.對象與json的轉化

對象和json的對應關係:

1.一個普通類的對象

class User {

private String id;

private Integer age;

}

user對象對應json字符串

{"id":"uuid1","age":18}

{
    "id": "uuid1",
    "age": 18
}

2.稍微複雜一些的對象

class Order{

private String order_id;

private OrderShipping shipping;

private List<OrderItem> orderItems;

}

class OrderShipping{

private  String receiver;

private String address;

}

order對象對應的json

{"order_id":"uuid","shipping":{"receiver":"張三","address":"beijing"}}

{

    "order_id": "uuid",

    "shipping": {

        "receiver": "張三",

        "address": "北京"

    },

     "orderItems":[{"":"","":""},{"":"","":""},{"":"","":""}]

}

3.之前JSON數據轉換爲Java對象

{
    "index04": {
        "mappings": {
            "book": {
                "properties": {
                    "content": {
                        "type": "text"
                    },
                    "id": {
                        "type": "integer"
                    },
                    "imgUrl": {
                        "type": "keyword"
                    },
                    "title": {
                        "type": "text"
                    }
                }
            }
        }
    }
}

轉換Java對象

第一層

class Object1{

private  Object2  index04;

}
  • {"index04":{Object2的json}}

第二層

		class Object2{
			private  Object3   mappings;
		}
  • {"mappings":{object3的json}}

第三層

		class Object3{
			private  Object4  book;
		}
  • {"book":{object4的json}}

第四層

		class Object4 {
			private Object5 properties;
		}
  • {"properties":{object5的json}}

第五層

		class Object5{
			private   Object6   content;
			private  Object6   title;l
			private  Object6 id;
			private  Object6  imgUrl;
		}
  • {"content":{object6的json},"title":{object6的json},"id":{object6的json},"imgUrl":{object6的json}}

第六層

		class Object6{
			private String type;
		}
  • {"type":"text/integer"}

 

二.ik分詞器插件引入到ELASTICSEARCH

1.文件拷貝

2.安裝依賴(已有不要安裝)

3.解壓

4.刪除安裝包

將安裝包一定要刪除,es的啓動會加載plugins文件夾下內容,一旦發現不認識的zip包,啓動報錯

5.進入到Config文件夾

6.將elasticsearch的文件夾名稱修改成analysis-ik

7.重新啓動es加載plugins中的ik分詞器

我的之前報錯,內存不足,直接關閉雲主機,不慣毛病,

具體錯誤,可以CSDN 不過好多博客寫全是廢話,屁都不着邊,看也白看

8.訪問es提供的接口,實現不同的分詞器使用

http://10.42.60.249:9200/index01/_analyze?analyzer=ik_max_word&text=中華人民共和國

  • index01:一個存在的索引
  • _analyze:對分詞的測試

參數:analyzer=ik_max_word,就是ik分詞器的名稱

 text=中華人民共和國,計算分詞的測試字符串

9.配置擴展和停用詞典

1.plugins目錄下的ik分詞器中config文件夾中找到IKAnalyzer.cfg.xml​​​​​​​

2.沒找到,去上傳一份

3.找錯文件夾 上一步可以不用做

4.配置擴展和停用

5.生成對應的詞典們

  • ext.dic:華人民

  • stopword.dic:人民

6.輸出結果

{
	"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": 4,
		"type": "CN_WORD",
		"position": 3
	}, {
		"token": "華人",
		"start_offset": 1,
		"end_offset": 3,
		"type": "CN_WORD",
		"position": 4
	}, {
		"token": "人民共和國",
		"start_offset": 2,
		"end_offset": 7,
		"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
	}]
}

10.通過mapping設置將ik_max_word作爲字符串類型的分詞器使用

1.index05中,定義article

  • {
  •     "properties": {
  •         "content": {
  •             "type": "text",
  •                 "analyzer":"ik_max_word"
  •         },
  •         "title": {
  •             "type": "text",
  •                 "analyzer":"ik_max_word"
  •         },
  •         "id": {
  •             "type": "integer"
  •         },
  •         "imgUrl": {
  •             "type": "keyword"
  •         }
  •     }
  • }

2.新建一個index05

3.在index05中添加一個article類型的自定義mapping結構

curl -XPUT http://10.42.60.249:9200/index05/_mapping/article -d '{"properties":{"content":{"type":"text","analyzer":"ik_max_word"},"title":{"type":"text","analyzer":"ik_max_word"},"id":{"type":"integer"},"imgUrl":{"type":"keyword"}}}'

4.index05的article類型中新增幾個document

curl -XPUT -d '{"id":1,"title":"java編程思想","content":"中華人民共和國"}' http://10.42.60.249:9200/index05/article/1

5.驗證是否使用了ik分詞器將title中java編程思想分詞計算爲{java}{編程}{思想}

curl -XGET http://10.42.60.249:9200/index05/_search -d '{"query":{"term":{"title":"編程"}}}'

IK分詞器安裝成功

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章