ElasticSearch常用基本操作【日常操作】

103 阅读2分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第20天,点击查看活动详情

索引

创建索引

 对比关系型数据库,创建索引就等同创建数据库

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