举例: 根据商品名称搜索商品
创建索引
product_v1
为什么要带v1
呢,后面方便升级#新增索引put product_v1?pretty#查看一下get _cat/indices?v
定义
map
,简单设置,只定义id
与name
,还有updatetime
#设置mapPOST 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" } }}#查看mapGET /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.设置新mapPOST 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" } } ]}...