ElasticSearch 7.4
7.4與5.5-6.8的主要區別在於建立索引是是否需要指定類型等,前者不需要(默認使用_doc作爲類型),後者需要
一、基礎操作
啓動elasticSearch-head命令:
npm run start
訪問localhost:9100
即可訪問到elasticSearch-head
啓動elasticSearch,window直接雙擊bin目錄下的elasticsearch.bat
既可以
請求地址:http:127.0.0.1:9200/people
請求參數:
{
"settings":{
"number_of_shards":3, --指定當前索引的分片數
"number_of_replicas":1 --指定當前索引的備份數
},
"mappings":{ --指定索引的數據映射定義
"properties":{ --指定索引的屬性定義
"type":{"type":"keyword"},
"name":{"type":"text"},
"country":{"type":"keyword"},
"age":{"type":"integer"},
"date":{
"type": "date",
"format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
}
}
如果_mapping爲空{}則爲非結構化索引,如果建立結構化索引可以使用以下方式建立:
在elasticsearch-head的複合查詢中或postman中請求
http://localhost:9200/book/_mappings
(http://ip:port:index_name/_mapping)
參數:
{
"properties": {
"title": {
"type": "text"
}
}
}
- 非指定文檔id插入數據
請求地址:http://127.0.0.1:9200/people/_doc/
post方式請求
請求參數:
{
"name":"重瓦力",
"country": "China",
"age": 30,
"date": "2019-11-02"
}
- 指定文檔id插入數據
請求地址:http://127.0.0.1:9200/people/_doc/1
put方式請求
請求參數:
{
"name":"瓦力",
"country": "China",
"age": 30,
"date": "2019-11-02"
}
- 指定id修改文檔
請求地址:http://127.0.0.1:9200/people/_doc/1/_update
(post方式請求,1爲數據_id值,_update爲指定操作)
參數:
{
"doc":{"name":"誰是瓦力"} -- doc爲關鍵字修改文本的name屬性
}
- 通過腳本方式修改
請求地址:http://127.0.0.1:9200/people/_doc/1/_update
(post方式)
參數:
{
"script":{"lang": "painless", --指定腳本語言
"inline": "ctx._source.age+=params.age", --指定腳本內容
"params": { -- 參數值
"age":100
}
}
}
- 通過id刪除數據
請求地址 http://127.0.0.1:9200/people/_doc/1
(刪除數據id爲1的數據,請求方式delete請求)
- 刪除索引
- 可以通過head插件進行刪除
- 使用postman進行刪除
請求地址:http://127.0.0.1:9200/people
(刪除操作非常危險,謹慎使用)
- 指定數據id查詢
請求地址: http://127.0.0.1:9200/people/_doc/1
(查詢id爲1的people索引文檔數據)
返回值:
{
"_index": "people",
"_type": "_doc",
"_id": "1",
"_version": 1,
"_seq_no": 6,
"_primary_term": 1,
"found": true,
"_source": {
"name": "重瓦力",
"country": "China",
"age": 30,
"date": "2019-11-02"
}
}
- 條件查詢
請求地址http://127.0.0.1:9200/people/_search
(_search爲內置方法操作,get方式請求)
請求參數:查詢全部數據
{
"query":{
"match_all":{} --代表查詢所有
},
"from":1, --從哪裏開始查詢(可忽略)
"size":1 --查詢多少條數據(可忽略)
}
返回結果:
{
"took": 5, --總耗時
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "people",
"_type": "_doc",
"_id": "bUqBKm4BaA3LSbA4MgPv",
"_score": 1.0,
"_source": {
"name": "重瓦力",
"country": "China",
"age": 30,
"date": "2019-11-02"
}
}
]
}
}
指定條件查詢
請求參數:
{
"query":{ --更多詳細用法可查閱資料
"match":{ -- 指定條件查詢匹配,可以匹配到條件中任何一個詞;(其它關鍵字還有match_phrase爲全詞匹配;multi_match爲多字段匹配;query_string爲語法查詢,range可對某個字段做範圍查詢,更多詳細用法可以查閱資料)
"name":"22"
}
},
"sort": [ --排序(可忽略)
{"age": {"order":"desc"}} --根據age降序排序
],
"from":1, --從哪裏開始查詢(可忽略)
"size":1 --查詢多少條數據(可忽略)
}
返回結果:
{
"took": 125,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.2876821,
"hits": [
{
"_index": "people",
"_type": "_doc",
"_id": "b0quKm4BaA3LSbA4-gNa",
"_score": 0.2876821,
"_source": {
"name": "重瓦力22",
"country": "China",
"age": 30,
"date": "2019-11-02"
}
}
]
}
}
請求參數:聚合查詢
{
"aggs":{ --aggs爲聚合查詢關鍵詞
"group_by_age":{ --自定義聚合名稱(可以定義多個作爲複合聚合)
"terms": { --聚合關鍵詞根據字段查詢,(其他關鍵字包含有stats對某個field進行結果計算總數等)
"field":"age"
}
}
}
}
返回結果:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 6,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "people",
"_type": "_doc",
"_id": "b0quKm4BaA3LSbA4-gNa",
"_score": 1.0,
"_source": {
"name": "重瓦力22",
"country": "China",
"age": 30,
"date": "2019-11-02"
}
},
...
]
},
"aggregations": {
"group_by_age": { --聚合信息
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 30, --age=30的有4個
"doc_count": 4
},
{
"key": 10,
"doc_count": 1
},
{
"key": 20,
"doc_count": 1
}
]
}
}
}
二、高級查詢
高級查詢分爲
filter用法參數
{
"query":{
"bool":{ --filter需要結合bool使用
"filter":{
"term": {
"age":20 --過濾條件
}
}
}
}
}
Query Context : 在查詢過程中,除了判斷文檔是否滿足查詢條件外,ES還會計算一個_score來標識匹配的程度,旨在判斷目標文檔和查詢條件匹配的有多好
常用查詢
- 全文本查詢:針對文本類型數據
- 字段級別查詢: 針對結構化數據,如數字,日期等
- 複合條件查詢:以一定的邏輯組合子條件查詢
指定分數用法:
固定分數查詢不支持match,只支持filter
{
"query":{
"constant_score":{ --固定分數查詢
"filter":{
"match":{
"age":10
}
},
"boost":2
}
}
}
should關鍵字用法
{
"query":{
"bool":{
"should":[ --只需滿足以下兩個條件中的其中一個即可(其它關鍵字must爲必須滿足;must_not必須不能滿足)
{
"match":{
"author":"測試okay"
}
},
{
"match":{
"title":"ElasticSearch語法"
}
}
]
}
}
}
常用查詢
- 固定分數查詢
- 布爾查詢
- …more
二、整合springboot
注意:使用版本和上面ElasticSearch7.4版本不同,下面是從新使用5.5版本進行整合以及演示
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.okay</groupId>
<artifactId>sping-boot-es</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sping-boot-es</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<elasticsearch.version>5.5.2</elasticsearch.version>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.elasticsearch.client/transport -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
添加配置類MyConf.java
@Configuration
public class MyConf {
@Bean
public TransportClient client() throws UnknownHostException {
// 這裏使用的tcp端口爲9300,默認
InetSocketTransportAddress node = new InetSocketTransportAddress(
InetAddress.getByName("localhost"),
9300
);
Settings settings = Settings.builder()
.put("cluster.name","byterun-es")
.build();
TransportClient client = new PreBuiltTransportClient(settings);
client.addTransportAddress(node);
return client;
}
}
- 添加controller請求es查詢數據
@RestController
public class ElasticSearchController {
@Autowired
private TransportClient client;
/**
* id爲es裏存在的_id值
*/
@RequestMapping("/get/book/novel")
@ResponseBody
public ResponseEntity get(@RequestParam(name = "id", defaultValue = "") String id){
if (id.isEmpty()) {
return new ResponseEntity(HttpStatus.NOT_FOUND);
}
// book爲索引,novel爲類型(5.5-6.8版本的es含有)
GetResponse result = this.client.prepareGet("book", "novel", id).get();
if (!result.isExists()) {
return new ResponseEntity(HttpStatus.NOT_FOUND);
}
return new ResponseEntity(result.getSource(), HttpStatus.OK);
}
}
啓動springboot運用即可訪問。