ElasticSearch基础篇
安装
官网
下载地址
下载完成后对文件进行解压,项目结构如下
-
进入bin目录点击
elasticsearch.bat
启动服务 -
9300 端口为 Elasticsearch 集群间组件的通信端口, 9200 端口为浏览器访问的 http协议 RESTful 端口
-
打开浏览器,输入地址: http://localhost:9200,返回结果如下
{"name" : "暗影精灵8","cluster_name" : "elasticsearch","cluster_uuid" : "9oaD9__3R_6WxskWlWe4iA","version" : {"number" : "7.8.0","build_flavor" : "default","build_type" : "zip","build_hash" : "757314695644ea9a1dc2fecd26d1a43856725e65","build_date" : "2020-06-14T19:35:50.234439Z","build_snapshot" : false,"lucene_version" : "8.5.1","minimum_wire_compatibility_version" : "6.8.0","minimum_index_compatibility_version" : "6.0.0-beta1"},"tagline" : "You Know, for Search" }
倒排索引
什么是倒排索引?
倒排索引(英语:Inverted index),也常被称为反向索引、置入档案或反向档案,是一种索引方法,被用来存储在全文搜索下某个单词在一个文档或者一组文档中的存储位置的映射。它是文档检索系统中最常用的数据结构。通过倒排索引,可以根据单词快速获取包含这个单词的文档列表。倒排索引主要由两个部分组成:“单词词典”和“倒排文件”
正排索引
id | content |
---|---|
1001 | my name is zhang san |
1002 | my name is li si |
- 对id创建索引,通过id查询对应的内容,如果通过id查询匹配关键字效率极低
倒排索引
keyword | id |
---|---|
name | 1001,1002 |
zhang | 1001 |
- 根据关键字查询对应的id
简单理解:正排索引是根据存储位置获取对应的内容,倒排索引是通过对应的内容获取存储位置
MySQL与ES结构关系
ES 里的 Index 可以看做一个库,而 Types 相当于表, Documents 则相当于表的行。这里 Types 的概念已经被逐渐弱化, Elasticsearch 6.X 中,一个 index 下已经只能包含一个type, Elasticsearch 7.X 中, Type 的概念已经被删除了
基本操作
创建索引
PUT http://localhost:9200/index_name
创建成功
{"acknowledged": true,"shards_acknowledged": true,"index": "shopping"
}
创建失败:索引已存在
{"error": {"root_cause": [{"type": "resource_already_exists_exception","reason": "index [shopping/8ra6Z1w3RiudhIMMlYkkNw] already exists","index_uuid": "8ra6Z1w3RiudhIMMlYkkNw","index": "shopping"}],"type": "resource_already_exists_exception","reason": "index [shopping/8ra6Z1w3RiudhIMMlYkkNw] already exists","index_uuid": "8ra6Z1w3RiudhIMMlYkkNw","index": "shopping"},"status": 400
}
查询索引
GET http://localhost:9200/shopping
响应结果
{"shopping": {"aliases": {},"mappings": {},"settings": {"index": {"creation_date": "1690355410054","number_of_shards": "1","number_of_replicas": "1","uuid": "8ra6Z1w3RiudhIMMlYkkNw","version": {"created": "7080099"},"provided_name": "shopping"}}}
}
查询所有索引
GET http://localhost:9200/_cat/indices?v
响应结果
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open shopping 8ra6Z1w3RiudhIMMlYkkNw 1 1 0 0 208b 208b
删除索引
DELETE http://localhost:9200/shopping
响应结果
{"acknowledged": true
}
创建文档
POST http://127.0.0.1:9200/shopping/_docbody type:json{"title":"小米手机","category":"小米","images":"http://www.gulixueyuan.com/xm.jpg","price":3999.00
}
响应结果
{"_index": "shopping","_type": "_doc","_id": "44MZkYkBpGe0S7oK7CQ2","_version": 1,"result": "created","_shards": {"total": 2,"successful": 1,"failed": 0},"_seq_no": 0,"_primary_term": 1
}
- _id:每次创建会生成唯一的id标识符,如果需要指定id可以使用如下接口
http://127.0.0.1:9200/shopping/_doc/1001
主键查询
通过指定id
查询
GET http://127.0.0.1:9200/shopping/_doc/1001
响应结果
{"_index": "shopping","_type": "_doc","_id": "1001","_version": 1,"_seq_no": 1,"_primary_term": 1,"found": true,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 3999.00}
}
全查询
GET http://127.0.0.1:9200/shopping/_search
响应结果
{"took": 122,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 2,"relation": "eq"},"max_score": 1.0,"hits": [{"_index": "shopping","_type": "_doc","_id": "44MZkYkBpGe0S7oK7CQ2","_score": 1.0,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 3999.00}},{"_index": "shopping","_type": "_doc","_id": "1001","_score": 1.0,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 3999.00}}]}
}
全量修改
PUT http://127.0.0.1:9200/shopping/_doc/1001body type:json
{"title":"华为手机","category":"华为","images":"http://www.gulixueyuan.com/hw.jpg","price":1999.00
}
响应结果
{"_index": "shopping","_type": "_doc","_id": "1001","_version": 2,"result": "updated","_shards": {"total": 2,"successful": 1,"failed": 0},"_seq_no": 2,"_primary_term": 1
}
局部修改
POST http://127.0.0.1:9200/shopping/_update/1001body type:json
{"doc":{"title":"苹果手机"}
}
响应结果
{"_index": "shopping","_type": "_doc","_id": "1001","_version": 3,"result": "updated","_shards": {"total": 2,"successful": 1,"failed": 0},"_seq_no": 3,"_primary_term": 1
}
删除文档
DELETE http://127.0.0.1:9200/shopping/_doc/1001
响应结果
{"_index": "shopping","_type": "_doc","_id": "1001","_version": 4,"result": "deleted","_shards": {"total": 2,"successful": 1,"failed": 0},"_seq_no": 4,"_primary_term": 1
}