课程名称:Java架构师-技术专家
课程章节: 第10周 手把手带你快速搞定分布式搜索引擎ES
课程讲师: 慕课讲师团队
index:默认true,设置为false的话,那么这个字段就不会被索引(不被索引是查询不到的)
PUT /index_str { "settings": { "index": { "number_of_shards": "2", "number_of_replicas": "0" } }, "mappings": { "properties": { "realname": { "type": "text",/*字符串,会被分词,会创建倒排索引*/ "index": true }, "username": { "type": "keyword",/*字符串,不会被分词,也不会被倒排索引;是一种精确匹配的搜索,比如说订单状态,用户的QQ号、手机号*/ "index": false } } } }
GET /index_str/_analyze { "field":"realname", "text":"I am good" }
GET /index_str/_analyze { "field":"realname", "text":"I am good" }
POST /index_str/_mapping { "properties":{ "id": { "type": "long" }, "age": { "type": "integer" }, "nickname": { "type": "keyword" }, "money1": { "type": "float" }, "money2": { "type": "double" }, "sex": { "type": "byte" }, "score": { "type": "short" }, "is_teenager": { "type": "boolean" }, "birthday": { "type": "date" }, "relationship": { "type": "object" } } }
注意:某个属性一旦被建立,就不能修改了,但是可以新增额外属性
- text:文字类需要被分词被倒排索引的内容,比如商品名称、商品详情、商品介绍,使用text。
- keyword:不会被分词,不会被倒排索引,直接匹配搜索,比如订单状态、微信号、手机号、用户qq,这些精确匹配,无需分词
POST /my_doc/_doc/1 -> {索引名}/_doc/{索引ID}(是指索引在es中的id,而不是这条记录的id,比如记录的id从数据库来是1001,并不是这个。如果不写,则自动生成一个字符串。建议和数据id保持一致> ) { "id": 1001, "name": "imooc-1", "desc": "imooc is very good, 慕课网非常牛!", "create_date": "2019-12-24" } { "id": 1002, "name": "imooc-2", "desc": "imooc is fashion, 慕课网非常时尚!", "create_date": "2019-12-25" } { "id": 1003, "name": "imooc-3", "desc": "imooc is niubility, 慕课网很好很强大!", "create_date": "2019-12-26" } { "id": 1004, "name": "imooc-4", "desc": "imooc is good~!", "create_date": "2019-12-27" } { "id": 1005, "name": "imooc-5", "desc": "慕课网 is 强大!", "create_date": "2019-12-28" } { "id": 1006, "name": "imooc-6", "desc": "慕课是一个强大网站!", "create_date": "2019-12-29" } { "id": 1007, "name": "imooc-7", "desc": "慕课网是很牛网站!", "create_date": "2019-12-30" } { "id": 1008, "name": "imooc-8", "desc": "慕课网是很好看!", "create_date": "2019-12-31" } { "id": 1009, "name": "imooc-9", "desc": "在慕课网学习很久!", "create_date": "2020-01-01" }
DELETE /my_doc/_doc/1
POST /my_doc/_doc/1/_update { "doc":{ "name":"wkq" } }
PUT /my_doc/_doc/1 { "id":1001, "name":"imooc-1", "desc":"imooc is very good,慕课网非常牛!", "create_date":"2022-03-26" }
每次修改后,version 会更改
GET /index_demo/_doc/1 GET /index_demo/_doc/_search
{ "_index": "my_doc", "_type": "_doc", "_id": "2", "_score": "1.0", "_version": "9", "_source": { "id": "1002", "name": "desc", "desc": "imooc is fashion", "create_time": "2022-03-26" } }
GET /index_demo/_doc/1?_source=id,name GET /index_demo/_doc/_search?_source=id,name
HEAD /index_demo/_doc/1
今天学习了 ES 的相关操作。