es rest api php,ES Elastic Restfull Api

ElasticSearch 中保存的数据结构

假设有两个对象:

public class Movie {

String id;

String name;

Double doubanScore;

List 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

结果:

bd35bbd2c9af

image.png

表头含义:

health green(集群完整) yellow(单点正常、集群不完整) red(单点不正常)

status 是否能使用

index 索引名

uuid 索引统一编号

pri 主节点几个

rep 从节点几个

docs.count 文档数

docs.deleted 文档被删了多少

store.size 整体占空间大小

pri.store.size 主节点占

增加索引

PUT /movie_index

bd35bbd2c9af

image.png

bd35bbd2c9af

image.png

删除索引

DELETE /movie_index

bd35bbd2c9af

image.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作为一个整体来看待

bd35bbd2c9af

image.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

}

}

}

}

}

}

bd35bbd2c9af

image.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"

}

}

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值