端口
- 内部端口:9300
- http访问端口:9200
数据格式
- 索引 ——> 数据库
- 类型 ——> 表 (新版本已删除)
- 文档 ——> 行
- 字段 ——> 列
Http操作
索引操作
-
创建索引
PUT,http://localhost:9200/索引名
响应:
{ "acknowledged": true, "shards_acknowledged": true, "index": "index001" }
-
查询索引
GET,http://localhost:9200/索引名
响应:
{ "index001": { "aliases": {}, "mappings": {}, "settings": { "index": { "creation_date": "1635474837213", "number_of_shards": "5", "number_of_replicas": "1", "uuid": "PEkqdQUnR1GOjqzC3a67QA", "version": { "created": "6040399" }, "provided_name": "index001" } } } }
-
删除索引
GET,http://localhost:9200/索引名
响应:
{ "acknowledged": true }
文档操作
-
创建文档 (如果没有id将生成随机id)
POST,http://localhost:9200/索引名/_doc/[id]
请求:
{ "name": :"zhangsan" }
响应:
{ "_index": "index001", "_type": "_doc", "_id": "GvzvyXwBC84NtfuwHmoc", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 0, "_primary_term": 1 }
-
查询文档
GET,http://localhost:9200/索引名/_doc/文档id
响应:
{ "_index": "index001", "_type": "_doc", "_id": "GvzvyXwBC84NtfuwHmoc", "_version": 1, "found": true, "_source": { "name": "zhangsan" } }
-
查询索引下全部文档
GET,http://localhost:9200/索引名/_search
响应:
{ "took": 157, // 耗费时间毫秒 "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 1.0, "hits": [ { "_index": "index001", "_type": "_doc", "_id": "GvzvyXwBC84NtfuwHmoc", "_score": 1.0, "_source": { "name": "zhangsan" } } ] } }
-
更新文档
PUT,http://localhost:9200/索引名/_doc/文档id
请求:
{ "name": :"lisi" }
响应:
{ "_index": "index001", "_type": "_doc", "_id": "GvzvyXwBC84NtfuwHmoc", "_version": 2, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 1, "_primary_term": 1 }
-
更新局部文档
POST,http://localhost:9200/索引名/_update/文档id
请求:(需修改的内容)
{ "name": :"lisi" }
响应:
{ "_index": "index001", "_type": "_doc", "_id": "GvzvyXwBC84NtfuwHmoc", "_version": 3, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 2, "_primary_term": 1 }
-
条件查询
GET,http://localhost:9200/索引名/_search?q=name:lisi_1
或
GET,http://localhost:9200/索引名/_search
请求:
{ "query":{ "match":{ // 完全匹配 "name":"lisi_1" }, "match_all":{ // 全查询 } }, "from":1, // 页码 "size":1, // 每页条数 "_source":[ // 限制显示的字段,相当于: select name "name" ], "sort":{ // 排序 "name":{ "order":"asc" } } }
响应:
{ "took": 1, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 0, "max_score": null, "hits": [] } }
-
多条件查询
GET,http://localhost:9200/索引名/_search
请求:
{ "query":{ "bool":{ "must":[ // must:模糊匹配, must_phrase:完全匹配 { "match":{ "name":"lisi_1" } }, { "match":{ "age":23 } } ] } }, "from":1, "size":1, "_source":[ "name" ], "sort":{ "name":{ "order":"asc" } } }
响应:略
-
聚合查询
GET,http://localhost:9200/索引名/_search
请求:
{ "aggs":{ "aaa":{ // 名称 "terms":{ // 分组 "field": "name" // 分组字段 } } }
响应:略
映射操作
-
创建索引
PUT,http://localhost:9200/索引名/_mapping
请求:
{ "properties":{ "name":{ "type": "text", // 将进行分词 "index": true, // 是否为该字段建立索引 }, "age":{ "type": "keyword", // 作为关键字,不会分词 "index": true, }, "name":{ "type": "keyword", "index": false, } } }
响应:
{ "properties":{ "name":{ "type": "text", "index": true, }, "age":{ "type": "keyword", "index": true, }, "name":{ "type": "keyword", "index": false, } } }
java操作
引入依赖
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.8.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.8.0</version>
</dependency>
</dependencies>