举例: 根据商品名称搜索商品
-
创建索引
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" } } ] } ...
