舉例: 根據商品名稱搜索商品
-
創建索引
product_v1
為什么要帶v1
呢,后面方便升級#新增索引 put product_v1?pretty #查看一下 get _cat/indices?v
-
定義
map
,簡單設置,只定義id
與name
,還有updatetime
#設置map POST product_v1/_mapping/doc { "properties": { "name":{ "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" }, "id":{ "type": "long" }, "updatetime": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss" } } } #查看map GET /product_v1/_mapping/
-
添加別名,將
product_v1
設置為product
,后面就可以針對別名操作。設置別名最大的好處,是后期變更map
時很方便,開發時連接product
即可POST /_aliases { "actions" : [ { "add" : { "index" : "product_v1", "alias" : "product" } } ] }
-
添加一些索引隨便添加,日期就不寫了
PUT product/doc/1?pretty { "name":"網絡紅外半球攝像機" } PUT product/doc/2?pretty { "name":"網絡高清硬盤錄像機" } PUT product/doc/3?pretty { "name":"網絡紅外高清中速球機" }
-
搜索測試數據
GET product/_search { "query": { "match": { "name": "網絡" } } }
-
升級變更
map
, 比如name
變更為productName
。 添加新字段typeName
#1.建立新索引 put product_v2?pretty #2.設置新map POST product_v2/_mapping/doc { "properties": { "productName":{ "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" }, "typeName":{ "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" } ... ... } } #3.得新添加索引到`product_v2`, PUT product_v2/doc/1?pretty { "productName":"網絡紅外半球攝像機", ... ... } #4.設置別名指向新索引,原索引自己決定是否刪除 POST /_aliases { "actions" : [ { "add" : { "index" : "product_v2", "alias" : "product" } } ] } #或者同時刪除掉原索引 POST /_aliases { "actions" : [ { "remove" : { "index" : "product_v1", "alias" : "product" } }, { "add" : { "index" : "product_v2", "alias" : "product" } } ] } #或者單獨刪除都可以 DELETE product_v1
-
升級變更
map
, 比如字段類型需要變更,如給typeName
添加一個keyword
的類型#1.新建索引 put product_v3?pretty #2.設置新`map` POST product_v3/_mapping/doc { "properties": { "typeName":{ "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word", "fields": { "keyword": { "type": "keyword" } } } ... ... } } #3.使用`reindex`得做索引 post _reindex { "source": { "index": "product" }, "dest": { "index": "product_v2" } } #4.將別名切到新索引,同上 POST /_aliases { "actions" : [ { "add" : { "index" : "product_v2", "alias" : "product" } } ] } ...