ElasticSearch之HTTP请求操作

598 阅读9分钟

ElasticSearch之HTTP请求操作

映射操作

索引库(index)中的映射,类似于数据库(database)中的表结构(table)。创建数据库表需要设置字段名称,类型,长度,约束等;索引库也一样,需要知道这个类型下有哪些字段,每个字段有哪些约束信息,这就叫做映射(mapping)。

创建映射

字段名:任意填写

type:类型,Elasticsearch中支持的数据类型非常丰富,说几个关键的:

String类型,又分两种:

text:可分词

keyword:不可分词,数据会作为完整字段进行匹配

Numerical:数值类型,分两类

基本数据类型:long、integer、shortbytedoublefloat、half_float

浮点数的高精度类型:scaled_float

Date:日期类型

Array:数组类型

Object:对象

index:是否索引,默认为true,所有字段都会被索引

true:字段会被索引,则可以用来进行搜索

false:字段不会被索引,不能用来搜索

store:是否将数据进行独立存储,默认为false

原始的文本会存储在_source里面,默认情况下其他提取出来的字段都不是独立存储的,是从_source里面提取出来的。当然也可以独立的存储某个字段,只要设置"store": true即可,获取独立存储的字段要比从_source中解析快得多,但是也会占用更多的空间,所以要根据实际业务需求来设置。

analyzer:分词器,这里的ik_max_word即使用ik分词器

http://127.0.0.1:9200/user

Put http://127.0.0.1:9200/user/_mapping
{
	"properties": {
		"username": {
			"type": "text",
			"index": true
		},
		"sex": {
			"type": "keyword",
			"index": true
		},
		"age": {
			"type": "long",
			"index": false
		}
	}
}

查看映射

Get http://127.0.0.1:9200/user/_mapping
{
	"user": {
		"mappings": {
			"properties": {
				"age": {
					"type": "long",
					"index": false
				},
				"sex": {
					"type": "keyword"
				},
				"username": {
					"type": "text"
				}
			}
		}
	}
}

索引直接映射关联

Put http://127.0.0.1:9200/user1
{
	"settings": {},
	"mappings": {
		"properties": {
			"username": {
				"type": "text",
				"index": true
			},
			"sex": {
				"type": "keyword",
				"index": true
			},
			"age": {
				"type": "long",
				"index": false
			}
		}
	}
}
{
	"acknowledged": true,
	"shards_acknowledged": true,
	"index": "user1"
}

索引操作

创建索引

向ES服务器发PUT请求

http://127.0.0.1:9200/demo

响应:

{
	"acknowledged": true,
	"shards_acknowledged": true,
	"index": "demo"
}

查看单个索引

向ES服务器发GET请求

http://127.0.0.1:9200/demo

响应:

{
	"demo": {
		"aliases": {},
		"mappings": {},
		"settings": {
			"index": {
				"creation_date": "1632061699362",
				"number_of_shards": "1",
				"number_of_replicas": "1",
				"uuid": "PMskybxnT6WHoA2hWHFETw",
				"version": {
					"created": "7080099"
				},
				"provided_name": "demo"
			}
		}
	}
}

查看所有索引

向ES服务器发GET请求

http://127.0.0.1:9200/_cat/indices?v

响应:

health status index uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   demo  PMskybxnT6WHoA2hWHFETw   1   1          0            0       208b           208b
表头含义
health当前服务器健康状态: green: 集群完整 yellow: 单点正常、集群不完整 red: 单点不正常
status索引打开、关闭状态
index索引名
uuid索引统一编号
pri主分片数量
rep副本数量
docs.count可用文档数量
docs.deleted文档删除状态(逻辑删除)
store.size主分片和副分片整体占空间大小
pri.store.size主分片占空间大小

删除索引

向ES服务器发DELETE请求

http://127.0.0.1:9200/demo
{
	"acknowledged": true
}

文档操作

创建文档

发POST请求

http://127.0.0.1:9200/demo/_doc

请求数据:

{
    "username":"小白白",
    "age":22,
    "address":"中国上海"
}

响应:

{
	"_index": "demo",
	"_type": "_doc",
	"_id": "wwKD_nsBW1VSDLfjekOE",
	"_version": 1,
	"result": "created",
	"_shards": {
		"total": 2,
		"successful": 1,
		"failed": 0
	},
	"_seq_no": 0,
	"_primary_term": 1
}

由于没有指定数据唯一性标识(ID),默认情况下,ES服务器会随机生成一个。

自定义唯一性标识,需要在创建时指定

http://127.0.0.1:9200/demo/_doc/1

http://127.0.0.1:9200/demo/_create/2

响应:

{
	"_index": "demo",
	"_type": "_doc",
	"_id": "1",
	"_version": 1,
	"result": "created",
	"_shards": {
		"total": 2,
		"successful": 1,
		"failed": 0
	},
	"_seq_no": 1,
	"_primary_term": 1
}

注意:增加数据,明确数据主键,可以使用POST与PUT请求

查看文档

发GET请求:

http://127.0.0.1:9200/demo/_doc/1

响应:

{
	"_index": "demo",
	"_type": "_doc",
	"_id": "1",
	"_version": 1,
	"_seq_no": 1,
	"_primary_term": 1,
	"found": true,
	"_source": {
		"username": "小白2",
		"age": 22,
		"address": "中国上海2"
	}
}

修改文档

发POST请求 :

http://127.0.0.1:9200/demo/_doc/1

请求参数:

{
    "username":"修改文档",
    "age":22,
    "address":"中国上海2"
}

响应:

{
	"_index": "demo",
	"_type": "_doc",
	"_id": "1",
	"_version": 2,
	"result": "updated",
	"_shards": {
		"total": 2,
		"successful": 1,
		"failed": 0
	},
	"_seq_no": 2,
	"_primary_term": 1
}

修改字段

修改数据时只修改数据的局部信息

发POST请求 :

http://127.0.0.1:9200/demo/_update/1

请求数据:

{ 
  "doc": {
    "username":"update field"
  } 
}

响应:

{
	"_index": "demo",
	"_type": "_doc",
	"_id": "1",
	"_version": 3,
	"result": "updated",
	"_shards": {
		"total": 2,
		"successful": 1,
		"failed": 0
	},
	"_seq_no": 3,
	"_primary_term": 2
}

删除文档

删除文档不会立即从磁盘上移除,只是被标记成已删除(逻辑删除)

根据文档的唯一性标识进行删除

根据条件对多条数据进行删除

发DELETE请求 :

http://127.0.0.1:9200/demo/_doc/4

响应:

{
	"_index": "demo",
	"_type": "_doc",
	"_id": "4",
	"_version": 2,
	"result": "deleted",
	"_shards": {
		"total": 2,
		"successful": 1,
		"failed": 0
	},
	"_seq_no": 6,
	"_primary_term": 2
}

发送POST请求:

http://127.0.0.1:9200/demo/_delete_by_query

请求数据:

{
	"query": {
		"match": {
			"username": "update field"
		}
	}
}

响应:

{
	"took": 81,
	"timed_out": false,
	"total": 1,
	"deleted": 1,
	"batches": 1,
	"version_conflicts": 0,
	"noops": 0,
	"retries": {
		"bulk": 0,
		"search": 0
	},
	"throttled_millis": 0,
	"requests_per_second": -1,
	"throttled_until_millis": 0,
	"failures": []
}

查询所有

发送Get请求:

http://127.0.0.1:9200/demo/_search

响应:

{
	"took": 632,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 4,
			"relation": "eq"
		},
		"max_score": 1,
		"hits": [
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "wwKD_nsBW1VSDLfjekOE",
				"_score": 1,
				"_source": {
					"username": "小白白",
					"age": 22,
					"address": "中国上海"
				}
			},
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "1",
				"_score": 1,
				"_source": {
					"username": "update field",
					"age": 22,
					"address": "中国上海2"
				}
			}
		]
	}
}

高级查询

查询所有

query代表一个查询对象,里面可以有不同的查询属性

Get http://127.0.0.1:9200/demo/_search

请求参数:

{
  "query": {
    "match_all": {}
  }
}

字段匹配查询

Get http://127.0.0.1:9200/demo/_search?q=age:22

响应:

{
	"took": 40,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 3,
			"relation": "eq"
		},
		"max_score": 1,
		"hits": [
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "wwKD_nsBW1VSDLfjekOE",
				"_score": 1,
				"_source": {
					"username": "小白白",
					"age": 22,
					"address": "中国上海"
				}
			},
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "1",
				"_score": 1,
				"_source": {
					"username": "update field",
					"age": 22,
					"address": "中国上海2"
				}
			}
		]
	}
}
Get http://127.0.0.1:9200/demo/_search

请求参数:

{
	"query": {
		"match": {
			"age": 22
		}
	}
}

响应:

{
	"took": 2,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 3,
			"relation": "eq"
		},
		"max_score": 1,
		"hits": [
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "wwKD_nsBW1VSDLfjekOE",
				"_score": 1,
				"_source": {
					"username": "小白白",
					"age": 22,
					"address": "中国上海"
				}
			},
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "1",
				"_score": 1,
				"_source": {
					"username": "update field",
					"age": 22,
					"address": "中国上海2"
				}
			}
		]
	}
}

multi_match与match类似,不同的是它可以在多个字段中查询

{
	"query": {
		"multi_match": {
			"query": "2",
			"fields": [
				"username",
				"address"
			]
		}
	}
}
{
	"took": 13,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 2,
			"relation": "eq"
		},
		"max_score": 0.6682933,
		"hits": [
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "2",
				"_score": 0.6682933,
				"_source": {
					"username": "小白2",
					"age": 22,
					"address": "中国上海2"
				}
			},
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "9",
				"_score": 0.6682933,
				"_source": {
					"username": "小白2",
					"age": 25,
					"address": "中国上海2"
				}
			}
		]
	}
}

精确匹配查询

term查询,精确的关键词匹配查询,不对查询条件进行分词

{
	"query": {
		"term": {
			"username": {
				"value": "小白2"
			}
		}
	}
}

多关键字精确查询

terms 查询和 term 查询一样,允许指定多值进行匹配。

如果这个字段包含了指定值中的任何一个值,那么这个文档满足条件,类似于mysql的in
{
	"query": {
		"terms": {
			"username": [
				"小白白",
				"小白"
			]
		}
	}
}

查询指定字段

默认情况下,ES在搜索的结果中,会把文档中保存在_source的所有字段都返回。如果只想获取其中的部分字段,可以添加_source的过滤

Get http://127.0.0.1:9200/demo/_search

请求数据:

{
	"query": {
		"match_all": {
			
		},
	},
		"_source":['username']
}

响应:

{
	"took": 3,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 3,
			"relation": "eq"
		},
		"max_score": 1,
		"hits": [
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "wwKD_nsBW1VSDLfjekOE",
				"_score": 1,
				"_source": {
					"username": "小白白"
				}
			},
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "2",
				"_score": 1,
				"_source": {
					"username": "小白2"
				}
			}
		]
	}
}

字段过滤

includes:来指定想要显示的字段

excludes:来指定不想要显示的字段
Get http://127.0.0.1:9200/demo/_search

请求数据:

{
  "_source": {
    "includes": ["age","username"]
  },  
  "query": {
    "match_all": {
			
		},
  }
}

分页查询

from:当前页的起始索引,默认从0开始。 from = (pageNum - 1) * size

size:每页显示多少条
Get http://127.0.0.1:9200/demo/_search

请求参数:

{
	"query": {
		"match_all": {
			
		},
	},
		"from":0,
		"size":2
}
{
	"took": 1,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 3,
			"relation": "eq"
		},
		"max_score": 1,
		"hits": [
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "wwKD_nsBW1VSDLfjekOE",
				"_score": 1,
				"_source": {
					"username": "小白白",
					"age": 22,
					"address": "中国上海"
				}
			},
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "1",
				"_score": 1,
				"_source": {
					"username": "update field",
					"age": 22,
					"address": "中国上海2"
				}
			}
		]
	}
}

单字段排序

通过order指定排序的方式。desc降序,asc升序。
Get http://127.0.0.1:9200/demo/_search

请求数据:

{
	"query": {
		"match_all": {
			
		},
	},
		"from":0,
		"size":2,
		"_source":['username'],
		"sort":{
		    "age":{
		        "order":"desc"
		    }
		}
}

响应:

{
	"took": 469,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 4,
			"relation": "eq"
		},
		"max_score": null,
		"hits": [
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "9",
				"_score": null,
				"_source": {
					"username": "小白2"
				},
				"sort": [
					25
				]
			},
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "wwKD_nsBW1VSDLfjekOE",
				"_score": null,
				"_source": {
					"username": "小白白"
				},
				"sort": [
					22
				]
			}
		]
	}
}

多字段排序

Get http://127.0.0.1:9200/demo/_search

请求数据:

{
	"query": {
		"match_all": {
			
		},
	},
		"from":0,
		"size":2,
		"_source":['username'],
		"sort":[
		    {
		    "age":{
		        "order":"desc"
		    }
		},
		{
      "_score":{
        "order": "desc"
      }
    }
		    ]
}

响应:

{
	"took": 4,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 3,
			"relation": "eq"
		},
		"max_score": null,
		"hits": [
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "9",
				"_score": 1,
				"_source": {
					"username": "小白2"
				},
				"sort": [
					25,
					1
				]
			},
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "wwKD_nsBW1VSDLfjekOE",
				"_score": 1,
				"_source": {
					"username": "小白白"
				},
				"sort": [
					22,
					1
				]
			}
		]
	}
}

模糊查询

返回包含与搜索字词相似的字词的文档。

编辑距离是将一个术语转换为另一个术语所需的一个字符更改的次数。这些更改可以包括:

更改字符(box → fox)

删除字符(black → lack)

插入字符(sic → sick)

转置两个相邻字符(act → cat

为了找到相似的术语,fuzzy查询会在指定的编辑距离内创建一组搜索词的所有可能的变体或扩展。然后查询返回每个扩展的完全匹配。

通过fuzziness修改编辑距离。一般使用默认值AUTO,根据术语的长度生成编辑距离。

Get http://127.0.0.1:9200/demo/_search
{
  "query": {
    "fuzzy": {
      "username": {
        "value": "白",
        "fuzziness": 2
      }
    }
  }
}

组合查询

bool把各种其它查询通过must(必须 )、must_not(必须不)、should`(应该)的方式进行组合

Get http://127.0.0.1:9200/demo/_search

请求数据:

{
	"query": {
		"bool": {
			"must": [
				{
					"match": {
						"age": 22
					}
				},
				{
					"match": {
						"username": "小白"
					}
				}
			]
		}
	}
}

响应数据:

{
	"took": 5,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 2,
			"relation": "eq"
		},
		"max_score": 1.8220872,
		"hits": [
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "wwKD_nsBW1VSDLfjekOE",
				"_score": 1.8220872,
				"_source": {
					"username": "小白白",
					"age": 22,
					"address": "中国上海"
				}
			},
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "2",
				"_score": 1.6877716,
				"_source": {
					"username": "小白2",
					"age": 22,
					"address": "中国上海2"
				}
			}
		]
	}
}

或者查询

{
	"query": {
		"bool": {
			"should": [
				{
					"match": {
						"age": 22
					}
				},
				{
					"match": {
						"username": "123"
					}
				}
			]
		}
	}
}

范围查询

range查询找出那些落在指定区间内的数字或者时间,支持一下字符

操作符说明符号
gt大于>
gte大于等于>=
lt小于<
lte小于等于<=
Get http://127.0.0.1:9200/demo/_search

请求数据:

{
	"query": {
		"bool": {
			"should": [
				{
					"match": {
						"age": 22
					}
				},
				{
					"match": {
						"username": "123"
					}
				}
			],
			"filter":{
			    "range":{
			        "age":{
			            "gt":23
			        }
			    }
			}
		}
	}
}

响应:

{
	"took": 5,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 1,
			"relation": "eq"
		},
		"max_score": 0,
		"hits": [
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "9",
				"_score": 0,
				"_source": {
					"username": "小白2",
					"age": 25,
					"address": "中国上海2"
				}
			}
		]
	}
}

完全匹配查询

Get   http://127.0.0.1:9200/demo/_search

请求数据:


{
   "query":{
       "match_phrase":{
           "username":"小白白"
       }
   }
}

响应:

{
	"took": 20,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 1,
			"relation": "eq"
		},
		"max_score": 1.0316575,
		"hits": [
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "wwKD_nsBW1VSDLfjekOE",
				"_score": 1.0316575,
				"_source": {
					"username": "小白白",
					"age": 22,
					"address": "中国上海"
				}
			}
		]
	}
}

高亮查询

Get  http://127.0.0.1:9200/demo/_search
{
	"query": {
		"match_phrase": {
			"username": "小白白"
		}
	},
	"highlight": {
		"pre_tags": "<font color='red'>",
		"post_tags": "</font>",
		"fields": {
			"username": {}
		}
	}
}

响应:

{
	"took": 5,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 1,
			"relation": "eq"
		},
		"max_score": 1.0316575,
		"hits": [
			{
				"_index": "demo",
				"_type": "_doc",
				"_id": "wwKD_nsBW1VSDLfjekOE",
				"_score": 1.0316575,
				"_source": {
					"username": "小白白",
					"age": 22,
					"address": "中国上海"
				},
				"highlight": {
					"username": [
						"<font color='red'>小</font><font color='red'>白</font><font color='red'>白</font>"
					]
				}
			}
		]
	}
}

聚合查询

Get http://127.0.0.1:9200/demo/_search

对某个字段取最大值max

{
    "aggs":{
      "max_age":{
        "max":{"field":"age"}
      }
    },
    "size":0
}

对某个字段取最大值min

{
    "aggs":{
      "min_age":{
        "min":{"field":"age"}
      }
    },
    "size":0
}

对某个字段求和sum

{
    "aggs":{
      "sum_age":{
        "sum":{"field":"age"}
      }
    },
    "size":0
}

对某个字段取平均值avg

{
    "aggs":{
      "avg_age":{
        "avg":{"field":"age"}
      }
    },
    "size":0
}

对某个字段的值进行去重之后再取总数

{
    "aggs":{
      "distinct_age":{
        "cardinality":{"field":"age"}
      }
    },
    "size":0
}

桶聚合查询

桶聚和相当于sql中的group by语句,terms聚合,分组统计

Get http://127.0.0.1:9200/demo/_search

请求数据:

{
	"aggs": {
		"group_age": {
			"terms": {
				"field": "age"
			}
		}
	},
	"size": 0
}

响应:

{
	"took": 165,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 3,
			"relation": "eq"
		},
		"max_score": null,
		"hits": []
	},
	"aggregations": {
		"group_age": {
			"doc_count_error_upper_bound": 0,
			"sum_other_doc_count": 0,
			"buckets": [
				{
					"key": 22,
					"doc_count": 2
				},
				{
					"key": 25,
					"doc_count": 1
				}
			]
		}
	}
}

Stats聚合

stats聚合,对某个字段一次性返回count,max,min,avg和sum五个指标

Get http://127.0.0.1:9200/demo/_search

请求数据:

{
	"aggs": {
		"stats_age": {
			"stats": {
				"field": "age"
			}
		}
	},
	"size": 0
}

响应:

{
	"took": 8,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 3,
			"relation": "eq"
		},
		"max_score": null,
		"hits": []
	},
	"aggregations": {
		"stats_age": {
			"count": 3,
			"min": 22,
			"max": 25,
			"avg": 23,
			"sum": 69
		}
	}
}