ElasticSearch 中保存的数据结构
假设有两个对象:
public class Movie {
String id;
String name;
Double doubanScore;
List<Actor> actorList;
}
public class Actor{
String id;
String name;
}
这两个对象如果放在关系型数据库保存,会被拆成 2 张表,但是 elasticsearch 是用一个 json 来表示一个 document。
所以在 ES 中是这样保存的:
{
“id”:”1”,
“name”:”operation red sea”,
“doubanScore”:”8.5”,
“actorList”:[
{“id”:”1”,”name”:”zhangyi”},
{“id”:”2”,”name”:”haiqing”},
{“id”:”3”,”name”:”zhanghanyu”}
]
}
操作 ElasticSearch 中的数据
查看 ES 中有哪些索引
GET /_cat/indices?v
结果:
![](https://img.haomeiwen.com/i12678003/0e5656662c496efb.png)
表头含义:
health green(集群完整) yellow(单点正常、集群不完整) red(单点不正常)
status 是否能使用
index 索引名
uuid 索引统一编号
pri 主节点几个
rep 从节点几个
docs.count 文档数
docs.deleted 文档被删了多少
store.size 整体占空间大小
pri.store.size 主节点占
增加索引
PUT /movie_index
![](https://img.haomeiwen.com/i12678003/46f679a73244fe41.png)
![](https://img.haomeiwen.com/i12678003/dc6579389de2635e.png)
删除索引
DELETE /movie_index
![](https://img.haomeiwen.com/i12678003/a8a218834f1609be.png)
新增文档
PUT /movie_index/movie/1
{ "id":1,
"name":"operation red sea",
"doubanScore":8.5,
"actorList":[
{"id":1,"name":"zhang yi"},
{"id":2,"name":"hai qing"},
{"id":3,"name":"zhang han yu"}
]
}
PUT /movie_index/movie/2
{
"id":2,
"name":"operation meigong river",
"doubanScore":8.0,
"actorList":[
{"id":3,"name":"zhang han yu"}
]
}
PUT /movie_index/movie/3
{
"id":3,
"name":"incident red sea",
"doubanScore":5.0,
"actorList":[
{"id":4,"name":"zhang chen"}
]
}
注意: 如果之前没建过 index 或者 type,es 会自动创建
搜索 type 全部数据
GET /movie_index/movie/_search
{
"took": 129,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "movie_index",
"_type": "movie",
"_id": "2",
"_score": 1,
"_source": {
"id": 2,
"name": "operation meigong river",
"doubanScore": 8,
"actorList": [
{
"id": 3,
"name": "zhang han yu"
}
]
}
},
{
"_index": "movie_index",
"_type": "movie",
"_id": "1",
"_score": 1,
"_source": {
"id": 1,
"name": "operation red sea",
"doubanScore": 8.5,
"actorList": [
{
"id": 1,
"name": "zhang yi"
},
{
"id": 2,
"name": "hai qing"
},
{
"id": 3,
"name": "zhang han yu"
}
]
}
},
{
"_index": "movie_index",
"_type": "movie",
"_id": "3",
"_score": 1,
"_source": {
"id": 3,
"name": "incident red sea",
"doubanScore": 5,
"actorList": [
{
"id": 4,
"name": "zhang chen"
}
]
}
}
]
}
}
查找指定 id 的 document 数据
GET /movie_index/movie/1
{
"_index": "movie_index",
"_type": "movie",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"id": 1,
"name": "operation red sea",
"doubanScore": 8.5,
"actorList": [
{
"id": 1,
"name": "zhang yi"
},
{
"id": 2,
"name": "hai qing"
},
{
"id": 3,
"name": "zhang han yu"
}
]
}
}
修改 document
修改分两种: 整体替换和只修改某个字段
整体替换
和新增文档没有区别
PUT /movie_index/movie/3 {
"id":"3",
"name":"incident red sea",
"doubanScore":"8.0",
"actorList":[
{"id":"1","name":"zhang chen"}
]
}
只修改某个字段
使用post方法
POST /movie_index/movie/3/_update {
"doc": {
"doubanScore":"8.1"
}
}
删除一个 document
DELETE /movie_index/movie/3
按条件查询(全部)
GET /movie_index/movie/_search
{
"query": {
"match_all": {}
}
}
按照字段的分词查询
GET /movie_index/movie/_search
{
"query": {
"match": {
"name": "sea"
}
}
}
按照分词子属性查询
GET /movie_index/movie/_search
{
"query": {
"match": {
"actorList.name": "zhang"
}
}
}
按照短语查询
按照短语查询的意思是指, 匹配某个 field 的整个内容, 不再利用分词技术
GET /movie_index/movie/_search
{
"query": {
"match_phrase": {
"name": "operation red"
}
}
}
说明: 把operation red作为一个整体来看待
![](https://img.haomeiwen.com/i12678003/2e6d388de63e1b5d.png)
对比:
下面的表示包含 operation 或者 red 的都会被查找出来
GET /movie_index/movie/_search
{
"query": {
"match": {
"name": "operation red"
}
}
}
模糊查询
校正匹配分词,当一个单词都无法准确匹配,es 通过一种算法对非常接近的单词也给与一定的评分,能够查询出来,但是消耗更多的性能。
GET /movie_index/movie/_search
{
"query": {
"fuzzy": {
"name": "red"
}
}
}
过滤(查询后过滤)
GET /movie_index/movie/_search
{
"query": {
"match": {
"name": "red"
}
},
"post_filter": {
"term": {
"actorList.id": "3"
}
}
}
查询前过滤(推荐使用)
GET movie_index/movie/_search
{
"query": {
"bool": {
"filter": [
{"term":
{"actorList.id": 3}
},
{
"term":
{"actorList.id": 1}
}
],
"must":
{"match": {
"name": "zhang"
}}
}
}
}
按范围过滤
GET movie_index/movie/_search
{
"query": {
"bool": {
"filter": {
"range": {
"doubanScore": {
"gt": 5,
"lt": 9
}
}
}
}
}
}
![](https://img.haomeiwen.com/i12678003/f29dbea1f0ee2735.png)
排序
GET movie_index/movie/_search
{
"query":{
"match": {"name":"red operation"}
}
, "sort": [
{
"doubanScore": {
"order": "desc"
}
}
]
}
分页查询
GET movie_index/movie/_search
{
"query": { "match_all": {} },
"from": 1,
"size": 1
}
指定查询的字段
GET movie_index/movie/_search
{
"query": { "match_all": {} },
"_source": ["name", "doubanScore"]
}
聚合
每个演员参演了多少部电影
GET movie_index/movie/_search {
"aggs": {
"groupby_actor": {
"terms": {
"field": "actorList.name.keyword"
}
}
}
}
每个演员参演电影的平均分是多少,并按评分排序
GET movie_index/movie/_search {
"aggs": {
"groupby_actor_id": {
"terms": {
"field": "actorList.name.keyword" ,
"order": {
"avg_score": "desc"
}
},
"aggs": {
"avg_score":{
"avg": {
"field": "doubanScore"
}
}
}
}
}
}
网友评论