#分词器 ik_max_word POST _analyze { "analyzer": "ik_max_word", "text": "南京市长江大桥" }
#分词器 ik_smart POST _analyze { "analyzer": "ik_smart", "text": "南京市长江大桥" }
###索引库操作
#创建索引库 put /ithe #查看索引库 get /ithe #删除索引库 delete /ithe #查看索引库 get /ithe
#需求1:创建type表 ##方式一:先建库,再建type #1.1建库 put /heima #查看是否建库成功 get /heima
#1.2创建type表 put /heima/_mapping/goods { "properties":{ "title":{ "type":"text", "analyzer":"ik_max_word" }, "subtitle":{ "type":"text", "analyzer":"ik_max_word" }, "image":{ "type":"text", "index":"false" }, "price":{ "type":"float" } } }
# 查看是否创建成功 ## 方式一 get /heima/_mapping/goods ## 方式二 get /heima
##方式二:同步建库+建type put /heima2 { "setting" : {}, "mappings" : { "goods" : { "properties" : { "title" : { "type" : "text", "analyzer" : "ik_max_word" }, "price":{ "type":"float" } } } } }
##查看是否创建成功 #方式一 get /heima2 #方式二 get /heima2/_mapping/goods
##需求2 :新增记录(不指定id) post /heima/goods { "title":"xiaomi", "image":"http://www.baidu.com", "price":"1999" }
#查看 get /heima/goods post /heima/_search { "query":{ "match_all": {} }
}
##需求3 :新增记录(指定id) post /heima/goods/2 { "title":"xiaomi2", "image":"http://www.baidu.com", "price":"2999" }
#查看 get /heima/goods/2 post /heima/_search { "query":{ "match_all": {} }
}
##需求4:修改记录(put:记录存在则修改,记录不存在则新增) put /heima/goods/3 { "title":"xiaomi3", "image":"http://www.baidu.com", "price":"3999" }
#查看 #方式一 get /heima/goods/3 #方式二 post /heima/_search { "query":{ "match_all": {} }
}
#需求4:修改记录(put:记录存在则修改,记录不存在则新增) put /heima/goods/3 { "title":"xiaomi33333", "image":"http://www.baidu.com", "price":"33333" }
#查看 #方式一 get /heima/goods/3 #方式二 post /heima/_search { "query":{ "match_all": {} }
}
#需求5:文档操作:删除 #查看 post /heima/_search { "query":{ "match_all": {} } } #需求5.1:根据ID删除 delete /heima/goods/3
#需求5.2:根据条件删除 post /heima/_delete_by_query { "query":{ "match":{ "title": "xiaomi" } } } #需求5.3:删除所有
post /heima/_delete_by_query { "query":{ "match_all":{} } }
#准备工作:插入数据 POST /heima/goods/_bulk {"index":{}} {"title":"大米手机","images":"http://image.leyou.com/12479122.jpg","price":3288} {"index":{}} {"title":"小米手机","images":"http://image.leyou.com/12479122.jpg","price":2699} {"index":{}} {"title":"小米电视4A","images":"http://image.leyou.com/12479122.jpg","price":4288}
#查询所有 post /heima/_search { "query":{ "match_all": {} } }
#需求6:匹配查询 默认or post /heima/_search { "query" : { "match" : { "title" : "小米电视4A" } } }
#需求7:匹配查询 and post /heima/_search { "query" : { "match" : { "title" : { "query": "小米电视4A", "operator": "and" } } } }
#需求8:多字段查询 #准备数据 POST /heima/goods { "title": "华为手机", "images": "http://image.leyou.com/12479122.jpg", "price": 5288, "subtitle": "小米" }
#查询 post /heima/_search { "query" : { "multi_match" : { "query" : "小米", "fields": ["title", "subtitle"] } } }
#需求9:单词条精确查询 post /heima/_search { "query" : { "term" : { "price" : 2699 } } } #或者: post /heima/_search { "query" : { "term" : { "price" : { "value" : 2699 } } } }
#需求10:多词条查询 post /heima/_search { "query" : { "terms" : { "price" : [2699, 5288] } } }
#需求11:结果过滤(只显示需要的字段) #11.1 _source post /heima/_search { "_source" : ["title", "price"], "query" : { "term" : { "price" : 2699 } } }
# post /heima/_search { "_source" : ["title", "price"], "query" : { "match_all": {} } }
#11.2 _source + includes post /heima/_search { "_source" : { "includes" : ["title", "price"] }, "query" : { "term" : { "price" : 2699 } } }
#11.3 _source + excludes post /heima/_search { "_source" : { "excludes" : ["image", "subtitle"] }, "query" : { "term" : { "price" : 2699 } } }
#需求12:高级查询:布尔组合 #目标:title=小米 且 title!=电视 post /heima/_search { "query" : { "bool" : { "must" : {"match": {"title":"小米"}}, "must_not" : {"match":{"title":"电视"}} } } }
#需求13:范围查询 # 使用range 完成 3000<=价格 且 价格 < 5000的商品 post /heima/_search { "query" : { "range" : { "price" : { "gte" : 3000, "lt" : 5000 } } } }
#需求14:模糊查询(fuzzy) #准备数据 POST /heima/goods/4 { "title":"apple手机", "images":"http://image.leyou.com/12479122.jpg", "price":5899.00 }
#查询 post /heima/_search { "query" : { "fuzzy" : { "title" : "appla" } } }
post /heima/_search { "query" : { "fuzzy" : { "title" : { "value" : "apple3", "fuzziness": 2 } } } }
#需求15:单字段排序 post /heima/_search { "query" : { "match_all": {} }, "sort" : { "price" : "desc" } }
#需求16:多字段排序 post /heima/_search { "query" : { "match_all" : {} }, "sort" : [ {"price" : {"order": "desc"}}, {"_score" : {"order" : "desc"}} ] }
###需求17:高亮 post /heima/_search { "query" : { "match" : { "title" : "电视" } }, "highlight" : { "pre_tags": "<font color='pink'>", "post_tags": "</font>", "fields": { "title" : {} } } }
##需求18:分页 post /heima/_search { "query" : { "match_all": {} }, "sort" : { "price" : "desc" }, "size" : 2, "from" : 2 }