ElasticSearch-1 ES基础概念及命令操作
ElasticSearch-2 ES6 TransportClient + SpringBoot2 增删改查
ElasticSearch-3 ES6 TransportClient 实现全文检索
ElasticSearch-4 ES7.6.3 RestHighLevelClient + SpringBoot2 增删改查
ElasticSearch-5 ES7.6.3 RestHighLevelClient 实现仿京东搜索
基础概念
ElasticSearch是面向文档的数据库,ES里一切都是JSON!
index
:是ES的顶层管理单位,相当于一个数据库。查看所有索引的命令:GET 'http://localhost:9200/_cat/indices'
type
:字段类型:index里的虚拟的逻辑分组,类似于一个表用来过滤 Document(ES7.0被去掉)。document
: Index 里面单条的JSON
格式记录称为 Document(文档),许多条 Document 构成了一个 Index。
分片
:每个索引都有多个分片,每个分片是一个Lucene索引,所以每一个ES索引由多个Lucene索引组成备份
:复制一个分片就完成了分片的备份-
倒排索引
:Lucene索引采用倒排索引
倒排索引
基础操作
- put创建索引
put /test0
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "test0"
}
- 创建索引时指定
mappings
和settings
PUT /test1
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"man": {
"properties": {
"name": {
"type":"text"
},
"country": {
"type":"keyword"
},
"age": {
"type":"integer"
},
"date": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
},
"worman": {}
}
}
- put创建索引、文档
put可以只创建索引,也可以索引文档一起创建
post不可以只创建索引,但可以索引文档一起创建
put /test/_doc/1
{
"name":"瓦力",
"age":18
}
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 20
}
- post 创建索引、文档(post可以不指定id,让其自动生成)
post /test3/_doc/
{
"name":"卧矣",
"age":18
}
{
"_index" : "test",
"_type" : "_doc",
"_id" : "-kq5bXEBSZdLg0qiZcmg",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 20
}
- post 修改文档
post修改是修改指定字段,put修改是用给与的字段覆盖原有文档
post /test/_update/1
{
"doc":{
"name":"瓦力",
"age":18,
"hobby":"卧矣"
}
}
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"result" : "noop",
"_shards" : {
"total" : 0,
"successful" : 0,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 20
}
- get 查看索引和文档
创建缩影的时候如果不显示设置
aliases
、mappings
、settings
,系统会自动设置
get /test
{
"test" : {
"aliases" : { },
"mappings" : {
"properties" : {
"age" : {
"type" : "long"
},
"hobby" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"settings" : {
"index" : {
"creation_date" : "1586665691321",
"number_of_shards" : "5",
"number_of_replicas" : "1",
"uuid" : "YHxWx3pIQrC5lIP9_FcHoQ",
"version" : {
"created" : "7060299"
},
"provided_name" : "test"
}
}
}
}
get /test/_doc/1
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "瓦力",
"age" : 18
}
}
- delete 删除文档和索引
delete /test/_doc/1
> 类型不可执行删除操作
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"_version" : 3,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 20
}
delete /test
{
"acknowledged" : true
}
高级查询
1、子条件查询:特定字段查询所指特定值
- Query context:ES会计算一个
_score
来表示匹配的程度,旨在判断目标文档和查询条件匹配的有多好
。- 全文本查询:针对文本类型数据的查询,包含模糊匹配、短语匹配、多字段匹配、语法查询
match
:模糊查询,会经过分词器解析term
:精确查找,通过倒排索引指定的词条进行精确查找
_score
:匹配度_source
:文档的字段
- 创建测试数据
PUT /es/_create/1
{
"name":"瓦力在雨中",
"age":18,
"desc":"一顿操作猛如虎",
"tags":["暖男","执着","善良"]
}
PUT /es/_create/2
{
"name":"瓦力在太空",
"age":19,
"desc":"一看工资两千五",
"tags":["可爱","好色","疯狂"]
}
PUT /es/_create/3
{
"name":"伊娃",
"age":18,
"desc":"一看工资两千五",
"tags":["善良","可爱","美丽"]
}
PUT /es/_create/4
{
"name":"伊娃",
"age":16,
"desc":"白富美",
"tags":["白富美","女神","大长腿"]
}
- 模糊查询,并过滤输出信息的字段
get /es/_search
{
"query":{
"match": {
"name": "瓦力"
}
},
"_source": ["name","tags"]
}
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.95466316,
"hits" : [
{
"_index" : "es",
"_type" : "user",
"_id" : "1",
"_score" : 0.95466316,
"_source" : {
"name" : "瓦力在雨中",
"tags" : [
"暖男",
"执着",
"善良"
]
}
},
{
"_index" : "es",
"_type" : "user",
"_id" : "2",
"_score" : 0.95466316,
"_source" : {
"name" : "瓦力在太空",
"tags" : [
"可爱",
"好色",
"疯狂"
]
}
}
]
}
}
- 根据指定字段进行排序并进行分页显示
get /es/_search
{
"query":{
"match": {
"name": "瓦力"
}
},
"sort": [
{
"age":{
"order":"asc"
}
}
],
"from":0,
"size":1
}
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "es",
"_type" : "user",
"_id" : "1",
"_score" : null,
"_source" : {
"name" : "瓦力在雨中",
"age" : 18,
"desc" : "一顿操作猛如虎",
"tags" : [
"暖男",
"执着",
"善良"
]
},
"sort" : [
18
]
}
]
}
}
- 多条件查询,类似mysql的and
get /es/_search
{
"query":{
"bool":{
"must":[
{
"match":{
"name":"瓦力"
}
},
{
"match":{
"age":18
}
}
]
}
}
}
{
"took" : 6,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 2.1794991,
"hits" : [
{
"_index" : "es",
"_type" : "user",
"_id" : "1",
"_score" : 2.1794991,
"_source" : {
"name" : "瓦力在雨中",
"age" : 18,
"desc" : "一顿操作猛如虎",
"tags" : [
"暖男",
"执着",
"善良"
]
}
}
]
}
}
多条件查询,类似mysql的 !=
只需要将上面的must改为must_not多条件查询,类似mysql的or
只需要将上面的must改为should即可
- 过滤器:过滤年龄段
gt>
gte>=
lt<
lte<=
get /es/_search
{
"query":{
"bool":{
"must":[
{
"match":{
"name":"瓦力"
}
}
],
"filter": {
"range": {
"age": {
"gte": 10,
"lte": 20
}
}
}
}
}
}
- 匹配多个条件
tags里包男和善良的文档都会被查询出来,多个匹配关键字之间用空格隔开
get /es/_search
{
"query":{
"match":{
"tags": "男 善良"
}
}
}
精确查询
经过分词器
不经过分词器精确查询多个值
get /es/_search
{
"query":{
"bool":{
"should":[
{
"term":{
"age":"19"
}
},{
"term":{
"age":16
}
}
]
}
}
}
- 高亮查询
会在查询出来信息的字段上加上标签
get /es/_search
{
"query":{
"match": {
"name": "伊娃"
}
},
"highlight":{
"pre_tags":"",
"post_tags": ">",
"fields": {
"name":{}
}
}
}
- 其他
127.0.0.1:9200/book/_search
{
"query": {
"match":{
"author":"金庸"
}
}
}
习语/短语匹配:关键词match_phrase
127.0.0.1:9200/book/_search
{
"query": {
"match_phrase":{
"author":"java入门"
}
}
}
多字段匹配:关键词multi_match
127.0.0.1:9200/book/_search
{
"query": {
"multi_match":{
"author":"金庸",
"fields":["auth","title"]
}
}
}
语法匹配:关键词query_string
127.0.0.1:9200/book/_search
{
"query": {
"query_string":{
"query":"(java AND good) OR Python",
"fields": ["title", "author"]
}
}
}
- 字段级别查询:针对结构化数据,如数字,日期等
语法匹配:关键词term
127.0.0.1:9200/book/_search
{
"query": {
"term":{
"author":"古龙"
}
}
}
- 字段级别的范围查询
127.0.0.1:9200/book/_search
{
"query": {
"range":{
"word_count": {
"gte": 1000,
"lte": 2000
}
}
}
}
2、Filter context ,只判断是否满足条件,只有yes或no; 会对结果进行缓存,速度较快
127.0.0.1:9200/book/_search
{
"query": {
"bool":{
"filter": {
"term": {
"word_count": 1000
}
}
}
}
}
- 二、复合条件查询:以一定的逻辑组合子条件查询
索引
1 、索引名规范
- 仅限小写字母
- 不能超过255个字节(注意它是字节,因此多字节字符将计入255个限制)
2、索引的种类
- 非结构化索引
mappings为空就是非结构话索引 - 结构化索引
mappings:{}不为空的索引
3、索引配置
PUT blog
{
"settings" : {
"index" : {
"number_of_shards" : 2,
"number_of_replicas" : 2
}
}
}
或
PUT blog
{
"settings" : {
"number_of_shards" : 2,
"number_of_replicas" : 2
}
}