ES是一款基于倒排索引的NoSQL数据库,传统数据库对于模糊查询存在性能瓶颈,而ES更擅长与大数据量的模糊查询
ES在存储数据的时候会先将数据进行分词,将分词的结果作为索引存入数据库中;当进行查询时也会将查询的参数进行分词,根据分词结果去ES中查询索引,根据索引查找到匹配的文档,从而将文档返回
二、ES请求方式 1、ES与MySQL- MySQL:数据存储在表中,表存储在数据库中
- ES:document存储在Type中,type存储在index中
- index:相当于数据库
- type:相当于表(ES7之前可以创建多个,ES7之后只能创建一个,且默认创建,默认名为_doc)
- docment:相当于数据库中的一条数据
ES的 *** 作与MySQL类似,都是从数据库→数据表→数据,ES为从index→type→document
1、索引(表) 1.1创建索引PUT index11.2查询索引
GET index1 GET index1/_settings GET index1/_mapping GET index1/_alias
查询结果为:
{ "index1" : { "aliases" : { }, "mappings" : { }, "settings" : { "index" : { "creation_date" : "1640858096289", "number_of_shards" : "1", "number_of_replicas" : "1", "uuid" : "sh4a0xy9TbuJ-HkosiqIxg", "version" : { "created" : "7060299" }, "provided_name" : "index1" } } } }
其中,mappings为字段的映射,settings为索引的属性,aliases为索引别名
在settings中,number_of_shards字段为分片数量,number_of_replices字段为备份数量
可以在创建索引时设置索引的属性与映射
PUT index1 { "mappings":{ "properties": { "bookId":{ "type": "long" }, "bookName":{ "type": "text" }, "bookPrice":{ "type": "float" }, "bookAuthor":{ "type": "keyworkd" } } }, "settings":{ "number_of_shards":5, "number_of_replicas":2 } }1.3删除索引
DELETe index12、document(数据)
对数据的 *** 作需要先找到Index再找到type再找到 文档编号,其中文档编号类似于MySQL中的主键
2.1添加数据POST index1/_doc/101 { "bookId":1, "bookName":"Java", "bookPrice":"15.2", "bookAuthor":"勒布朗·詹姆斯" }2.2查询数据
GET index1/_doc/1012.3修改数据
修改数据与添加数据相同,若不存在给条数据则会创建一条数据
POST index1/_doc/101 { "bookId":1, "bookName":"Java", "bookPrice":"1.25", "bookAuthor":"勒布朗·詹姆斯" }
若不存在数据则不会进行创建
POST index1/_doc/102/_update { "doc":{ "bookId":1, "bookName":"Java从入门到放弃", "bookPrice":"1.25", "bookAuthor":"终南山" } }2.4删除数据
DELETE index1/_doc/101四、复杂查询 1、精确查询 1.1 term
不会对关键字进行分词,对关键词进行全匹配
GET /index1/_search { "query":{ "term":{ "bookId": "1" } } }1.2 terms
多关键词全匹配
GET /index1/_search { "query":{ "terms":{ "bookId": ["1","2","3"] } } }2、模糊查询 2.1 match
对关键词进行拆词匹配
GET /index1/_search { "query":{ "match":{ "bookName": "入门" } } }2.2 match_all
GET /index1/_search { "query":{ "match_all":{ } } }2.3 multi_match
多属性匹配
GET /index1/_search { "query":{ "multi_match":{ "query": "语言", "fields": ["bookName","bookAuthor"] } } }3、分页查询
GET /index1/_search { "query":{ "match":{ "bookName": "入门" } }, "from":0, "size":3 }4、排序
asc升序,desc降序
GET /index1/_search { "query":{ "match":{ "bookName": "入门" } }, "from":0, "size":3, "sort": [ { "bookId": { "order": "asc" } } ] }5、范围查询
GET /index1/_search { "query":{ "range": { "bookPrice": { "gte": 10, "lte": 20 } } } }6、模糊查询
不是分词的模糊查询,是全模糊查询,类似与MySQL中的 like
GET /index1/_search { "query":{ "fuzzy": { "bookName": { "value": "nu" } } } }四、复合查询
1、多条件查询must → and
should → or
must_not → not
GET index1/_search { "query": { "bool":{ "must": [ { "match": { "bookName": "放弃" } } ], "should": [ { "match": { "bookAuthor": "陈奕迅" } } ], "must_not": [ { "match": { "bookPrice": "10.39" } } ] } }, "sort": [ { "bookId": { "order": "asc" } } ], "from": 0, "size": 5 }2、高亮显示
highlight字段用于高亮显示
pre_tags 为前缀
post_tags 为后缀
GET index1/_search { "query": { "bool":{ "must": [ { "match": { "bookName": "放弃" } } ] } }, "sort": [ { "bookId": { "order": "asc" } } ], "from": 0, "size": 5, "highlight": { "fields": { "bookName": {} }, "pre_tags": "aaaa", "post_tags": "ddddd" } }3、显示字段
每一个query后面都可以跟 分页查询、高亮显示、排序、显示字段
显示字段用 “_sources” 字段
GET index1/_search { "query": { "bool":{ "must": [ { "match": { "bookName": "放弃" } } ] } }, "sort": [ { "bookId": { "order": "asc" } } ], "_source": ["bookId","bookName"], "from": 0, "size": 5, "highlight": { "fields": { "bookName": {} }, "pre_tags": "aaaa", "post_tags": "ddddd" } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)