美文网首页
ES Elastic Restfull Api

ES Elastic Restfull Api

作者: 无来无去_A | 来源:发表于2020-09-19 10:50 被阅读0次

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

结果:


image.png

表头含义:
health green(集群完整) yellow(单点正常、集群不完整) red(单点不正常)
status 是否能使用
index 索引名
uuid 索引统一编号
pri 主节点几个
rep 从节点几个
docs.count 文档数
docs.deleted 文档被删了多少
store.size 整体占空间大小
pri.store.size 主节点占

增加索引

PUT /movie_index
image.png image.png

删除索引

DELETE /movie_index
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作为一个整体来看待


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
          }
        }
      }
    }
  }
}
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"
  }
  }
  }
  }
  }
}

相关文章

  • ES Elastic Restfull Api

    ElasticSearch 中保存的数据结构假设有两个对象: 这两个对象如果放在关系型数据库保存,会被拆成 2 张...

  • Restfull API 示例

    什么是Restfull API Restfull API 从字面就可以知道,他是rest式的接口,所以就要先了解什...

  • 环信Restfull API dotnetSDK

    Easemob.Restfull4Net 环信Restfull API dotnet的封装 支持的.Net Fra...

  • RESTfull API

    介绍 现在的网络应用程序,分为前端和后端两个部分, 采用客户端/服务器 模式。 后端建立在分布式体系上,通过互联网...

  • RESTfull API

    API 基本格式 在命令行访问 集群的 API 的格式如下: 简单解释一下命令的各个部分: VERB : 请求的方...

  • docker、gateway、nginx网络性能损耗对比测试

    一、测试准备 1.程序准备 1、java api程序,springboot restfull api; 2、ope...

  • 小程序笔记篇

    小程序当中的api使用 服务器api调用的类型: RESTFull API 返回的是 json SOAP XML ...

  • Elasticsearch Restfull API

    本文发布链接地址: https://www.jianshu.com/p/0ccadf8eb3c5 Elastics...

  • Elasticsearch Restfull API

    API 基本格式 在命令行访问 集群的 API 的格式如下: 简单解释一下命令的各个部分: VERB : 请求的方...

  • 50. RESTful API的简单实现

    RESTfull API是现在很流行的 API 设计风格。众所周知的 HTTP 1.1规范正是基于 REST 架构...

网友评论

      本文标题:ES Elastic Restfull Api

      本文链接:https://www.haomeiwen.com/subject/wzkdyktx.html