ElasticSearch基本操作

文档来源
es下载地址
IK分词器下载地址
Elasticsearch 权威指南(中文版)

索引

  • 创建索引
    对比关系型数据库,创建索引就等同创建数据库
      PUT请求
      http://127.0.0.1:9200/shopping
  • 查询索引
      GET请求
      http://127.0.0.1:9200/shopping
  • 查询所有索引
      GET请求
      http://127.0.0.1:9200/_cat/indices?v
  • 删除索引
      DELETE请求
      http://127.0.0.1:9200/shopping

文档

索引已经创建好了,接下来我们创建文档,并添加数据。这里的文档可以类比为关系型数据库中的表数据,添加的数据格式为JSON格式

  • 创建文档

      POST请求
      http://127.0.0.1:9200/shopping/_doc #写法一
      http://127.0.0.1:9200/shopping/_create # 写法二
      {"name":"商品"}
      PUT请求,主键必须幂等性
      http://127.0.0.1:9200/shopping/_doc/1001 #写法一
      http://127.0.0.1:9200/shopping/_create/1002 # 写法二
      {"name":"商品"}
      POST请求 ,创建自定义id
      http://127.0.0.1:9200/shopping/_doc/1001
  • 主键查询

      GET请求
      http://127.0.0.1:9200/shopping/_doc/1001
  • 全查询

      GET请求
      http://127.0.0.1:9200/shopping/_search
  • 全量修改

      PUT请求
      http://127.0.0.1:9200/shopping/_doc/1001
      {"name":"商品"}
  • 局部修改

      POST请求
      http://127.0.0.1:9200/shopping/_update/1001
      {"doc":{"name":"局部修改商品"}}
  • 删除

      DELETE请求
      http://127.0.0.1:9200/shopping/_doc/1001

    查询

  • 条件查询

      GET请求,方法一
      http://127.0.0.1:9200/shopping/_search?q=category:小米
      http://127.0.0.1:9200/shopping/_search?q=name:商品
      GET请求,方法二(推荐)
      http://127.0.0.1:9200/shopping/_search
      {
          "query":{
              "match":{
                  "category":"小米"
              }
          }
      }
  • 全量查询

      GET请求
      http://127.0.0.1:9200/shopping/_search
      {
          "query":{
              "match_all":{
              }
          }
      }
  • 分页查询(from,size)

      GET请求
      http://127.0.0.1:9200/shopping/_search
      {
          "query":{
              "match_all":{
              }
          },
          "from":0,#起始位置/偏移量 ,公式:(页码-1* 每页数据条数
          "size":10,#每页查询10}
  • 指定field分页查询 (_source)

      GET请求
      http://127.0.0.1:9200/shopping/_search
      {
          "query":{
              "match_all":{
              }
          },
          "from":0,#起始位置/偏移量 ,公式:(页码-1* 每页数据条数
          "size":10,#每页查询10"_source":["title"]
      }
  • 查询排序(sort)

      GET请求
      http://127.0.0.1:9200/shopping/_search
      {
          "query":{
              "match_all":{
              }
          },
          "from":0,#起始位置/偏移量 ,公式:(页码-1* 每页数据条数
          "size":10,#每页查询10"_source":["title"],
          "sort":{
              "price":{
                  "order":"desc"
              }
          }
      }

    多条件查询

  • and查询(must)

      GET请求
      http://127.0.0.1:9200/shopping/_search
      {
          "query":{
              "bool":{
                  "must":[ 
                      {
                          "match":{
                              "category":"小米"
                          }
                      },
                      {
                          "match":{
                              "price":1999.00
                          }
                      }
                  ]
              }
          }
      }
  • or查询(should)

      GET请求
      http://127.0.0.1:9200/shopping/_search
      {
          "query":{
              "bool":{
                  "should":[ 
                      {
                          "match":{
                              "category":"小米"
                          }
                      },
                      {
                          "match":{
                              "price":1999.00
                          }
                      }
                  ]
              }
          }
      }
  • 范围查询(filter,range)

      GET请求
      http://127.0.0.1:9200/shopping/_search
      {
          "query":{
              "bool":{
                  "should":[
                      {
                          "match":{
                              "category":"小米"
                          }
                      },
                      {
                          "match":{
                              "price":1999.00
                          }
                      }
                  ],
                  "filter":{
                      "range":{
                          "price":{
                              "gt":5000
                          }
                      }
                  }
              }
          }
      }
  • 全文检索匹配(分词)(match)

      GET请求
      http://127.0.0.1:9200/shopping/_search
      {
          "query":{
              "match":{
                  "category": "小华"
              }
          }
      }
  • 完全匹配(match_phrase)

      GET请求
      http://127.0.0.1:9200/shopping/_search
      {
          "query":{
              "match_phrase":{
                  "category": "小华"
              }
          }
      }
  • 高亮查询 (hightlight,对结果加html标签)

      GET请求
      http://127.0.0.1:9200/shopping/_search
      {
          "query":{
              "match_phrase":{
                  "category": "小华"
              }
          },
          "hightlight":{
              "fields":{
                  "category":{}
              }
          }
      }

    聚合查询

  • 返回统计数据和原始数据

    
      GET请求
      http://127.0.0.1:9200/shopping/_search
      { 
          "aggs":{ #聚合操作
              "price_group":{ #名称,随意起名
                  "terms":{ #分组
                      "field":"price" #分组字段
                  }
              }
          }}
    
  • 关闭原始数据(size)

      GET请求
      http://127.0.0.1:9200/shopping/_search
    
          { 
          "aggs":{ #聚合操作
              "price_group":{ #名称,随意起名
                  "terms":{ #分组
                      "field":"price" #分组字段
                  }
              }
          }"size":0
      }
  • 平均值

      GET请求
      http://127.0.0.1:9200/shopping/_search
    
          { 
          "aggs":{ #聚合操作
              "price_avg":{ #名称,随意起名
                  "age":{ #平均值
                      "field":"price" #分组字段
                  }
              }
          }"size":0
      }

    映射关系

  • 创建映射

      PUT请求
      http://127.0.0.1:9200/user/_mapping
      { 
          "properties":{
              "name":{
                  "type":"text", #全文检索分词查询
                  "index":true
              },
              "sex":{
                  "type":"keyword",#完全查询
                  "index":true
              },
              "tel":{
                  "type":"keyword",#不能查询
                  "index":false
              }
          }
      }
  • 查询映射

      GET请求
      http://127.0.0.1:9200/user/_mapping
  • 增加数据

      PUT请求
      http://127.0.0.1:9200/user/_create/1001
      {
          name:"小米",
          sex:"男的",
          tel:"10010"
      }
  • 查询数据

      GET请求
      http://127.0.0.1:9200/user/_search
      {
          "query":{
              "match": {
                  name:"小"
              }
          }
      }
      GET请求
      http://127.0.0.1:9200/user/_search
      {
          "query":{
              "match": {
                  sex:"男" #查询不到,必须输入男的
              }
          }
      }
      #不支持查询
      GET请求
      http://127.0.0.1:9200/user/_search
      {
          "query":{
              "match": {
                  tel:"10010" 
              }
          }
      }
本作品采用《CC 协议》,转载必须注明作者和本文链接
本帖由 working 于 1年前 加精
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 7
╰ゝSakura

Seven yyds

1年前 评论
my38778570 (楼主) 1年前

666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666

1年前 评论
my38778570 (楼主) 1年前

大哥 6666666666666666666 刷火箭~

1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!