集群健康
请求
GET http://192.168.247.8:9200/_cluster/health
结果
{ "cluster_name": "kevin-elasticsearch", "status": "green", "timed_out": false, "number_of_nodes": 1, "number_of_data_nodes": 1, "active_primary_shards": 5, "active_shards": 5, "relocating_shards": 0, "initializing_shards": 0, "unassigned_shards": 0, "delayed_unassigned_shards": 0, "number_of_pending_tasks": 0, "number_of_in_flight_fetch": 0, "task_max_waiting_in_queue_millis": 0, "active_shards_percent_as_number": 100.0 }
创建索引
请求
PUT http://192.168.247.8:9200/index_str { "settings": { "index": { "number_of_shards": "2", "number_of_replicas": "0" } } }
结果:
{ "acknowledged": true, "shards_acknowledged": true, "index": "index_str" }
查看索引
请求
GET http://192.168.247.8:9200/_cat/indices?v
结果
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size green open index_demo xUOiKc5QT5-UWWcJjqRkIw 5 0 0 0 1.3kb 1.3kb green open index_str BniDy5eJQ26Qg26oRbz_Rg 2 0 0 0 460b 460b
删除索引
DELETE http://192.168.247.8:9200/index_str
结果
{ "acknowledged": true }
索引的mappings映射
索引分词概念
index:默认true,设置为false的话,那么这个字段就不会被索引
创建索引的同时创建mappings
请求
PUT http://192.168.247.8:9200/index_str { "mappings": { "properties": { "realname": { "type": "text", "index": true }, "username": { "type": "keyword", "index": false } } } }
返回
{ "acknowledged": true, "shards_acknowledged": true, "index": "index_str" }
查看分词效果
请求
GET http://192.168.247.8:9200/index_str/_analyze { "field": "realname", "text": "kevin is god" }
结果
{ "tokens": [ { "token": "kevin", "start_offset": 0, "end_offset": 5, "type": "<ALPHANUM>", "position": 0 }, { "token": "is", "start_offset": 6, "end_offset": 8, "type": "<ALPHANUM>", "position": 1 }, { "token": "god", "start_offset": 9, "end_offset": 12, "type": "<ALPHANUM>", "position": 2 } ] }
修改mappings
请求
POST http://192.168.247.8:9200/index_str/_mapping { "properties": { "name": { "type": "long" } } }
结果
{ "acknowledged": true }
某个属性一旦被建立,就不能修改了,但是可以新增额外属性
为已存在的索引创建mappings
请求
POST http://192.168.247.8:9200/index_str/_mapping { "properties": { "id": { "type": "long" }, "age": { "type": "integer" }, "nickname": { "type": "keyword" } } }
结果
{ "acknowledged": true }
主要数据类型
- text, keyword
- long, integer, short, byte
- double, float
- boolean
- date
- object
- 数组不能混,类型一致
字符串
- text:文字类需要被分词被倒排索引的内容,比如商品名称,商品详情,商品介绍,使用text。
- keyword:不会被分词,不会被倒排索引,直接匹配搜索,比如订单状态,用户qq,微信号,手机号等,这些精确匹配,无需分词。
添加文档数据
POST /my_doc/_doc/1 -> {索引名}/_doc/{索引ID}(是指索引在es中的id,而不是这条记录的id,比如记录的id从数据库来是1001,并不是这个。如果不写,则自动生成一个字符串。建议和数据id保持一致> ) { "id": 1001, "name": "kevin-1", "desc": "kevin is very good, 渣渣猿非常牛!", "create_date": "2019-12-24" }
返回
{ "_index": "my_doc", "_type": "_doc", "_id": "1", "_version": 1, "result": "created", "_shards": { "total": 1, "successful": 1, "failed": 0 }, "_seq_no": 0, "_primary_term": 1 }
文档修改
局部修改
请求
POST http://192.168.247.8:9200/my_doc/_doc/2/_update { "doc":{ "name":"maomao" } }
返回
{ "_index": "my_doc", "_type": "_doc", "_id": "2", "_version": 2, "result": "updated", "_shards": { "total": 1, "successful": 1, "failed": 0 }, "_seq_no": 1, "_primary_term": 1 }
全量替换
请求
PUT http://192.168.247.8:9200/my_doc/_doc/2 { "id": 1002, "name": "kevin_2", "desc": "我是渣渣猿,紧紧抱住坤哥大腿!", "create_date": "2020-02-04" }
结果
{ "_index": "my_doc", "_type": "_doc", "_id": "2", "_version": 3, "result": "updated", "_shards": { "total": 1, "successful": 1, "failed": 0 }, "_seq_no": 2, "_primary_term": 1 }
注意:每次修改后,version都会更改
删除文档
请求
DELETE http://192.168.247.8:9200/my_doc/_doc/2
返回
{ "_index": "my_doc", "_type": "_doc", "_id": "2", "_version": 4, "result": "deleted", "_shards": { "total": 1, "successful": 1, "failed": 0 }, "_seq_no": 3, "_primary_term": 1 }
查询
查询文档
GET http://192.168.247.8:9200/my_doc/_doc/1 # 结果 { "_index": "my_doc", "_type": "_doc", "_id": "1", "_version": 2, "_seq_no": 1, "_primary_term": 1, "found": true, "_source": { "id": 1001, "name": "毛毛", "desc": "kevin is very good, 渣渣猿非常牛!", "create_date": "2019-12-24" } } GET http://192.168.247.8:9200/my_doc/_doc/_search # 结果 { "took": 17, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 1, "relation": "eq" }, "max_score": 1.0, "hits": [ { "_index": "my_doc", "_type": "_doc", "_id": "1", "_score": 1.0, "_source": { "id": 1001, "name": "毛毛", "desc": "kevin is very good, 渣渣猿非常牛!", "create_date": "2019-12-24" } } ] } }
- _index:文档数据所属那个索引,理解为数据库的某张表即可。
- _type:文档数据属于哪个类型,新版本使用_doc。
- _id:文档数据的唯一标识,类似数据库中某张表的主键。可以自动生成或者手动指定。
- _score:查询相关度,是否契合用户匹配,分数越高用户的搜索体验越高。
- _version:版本号。
- _source:文档数据,json格式。
定制结果集
GET http://192.168.247.8:9200/my_doc/_doc/1?_source=id,name # 结果 { "_index": "my_doc", "_type": "_doc", "_id": "1", "_version": 2, "_seq_no": 1, "_primary_term": 1, "found": true, "_source": { "name": "毛毛", "id": 1001 } } GET /index_demo/_doc/_search?_source=id,name
判断文档是否存在
HEAD /index_demo/_doc/1 查看返回状态
好了,这篇先学习到这。玩的开心!
来源:https://www.cnblogs.com/zhenghengbin/p/12285169.html