elasticsearch 6.6.2基本的index和document的crud操作

100 阅读1分钟

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

示例1

#判断索引twitter是否存在
HEAD twitter

#索引初始化操作,指定分片和副本数量
#设定后,分片不能修改
PUT twitter
{
  "settings": {
    "number_of_replicas": 1,
    "number_of_shards": 2
  }
}
#获取索引信息
GET twitter/_settings

#创建一个job文档,id为1的
PUT twitter/job/1
{
  "title": "python",
  "min_salary": 15000,
  "company": {
    "name": "jingdong",
    "company_addr": "beijing"
  },
  "publish_date": "2022-03-10",
  "comments": 15
}

#创建一个job文档,id为2的PUT twitter/job/2
{
  "title": "python",
  "min_salary": 15000,
  "company": {
    "name": "meituan",
    "company_addr": "tianjin"
  },
  "publish_date": "2022-03-13",
  "comments": 20
}

#根据id查询
GET twitter/job/1

#修改属性值
POST twitter/job/1/_update
{
  "doc": {
    "comments": 100
  }
}
#查询 publish_date大于2022-03-11的
GET twitter/job/_search
{
  "query": {
    "bool": {
      "filter": [
        {"range": {"publish_date":{"gte":"2022-03-11"}}}  
      ]
    }
  }
}

#删除id为1的记录
DELETE twitter/job/1
#删除索引
DELETE twitters

示例2

#新建一个index,并指定需要分词的字段
#一个index只能创建一个文档
PUT accounts
{
  "settings": {
    "number_of_replicas": 1,
    "number_of_shards": 2
  }, 
  "mappings": {
    "person":{
      "properties":{
        "user":{
          "type":"text",
          "analyzer":"ik_max_word",
          "search_analyzer":"ik_max_word"
        },
        "title":{
          "type":"text",
          "analyzer":"ik_max_word",
          "search_analyzer":"ik_max_word"
        },
        "desc":{
          "type":"text",
          "analyzer":"ik_max_word",
          "search_analyzer":"ik_max_word"
        }
      }
    }
  }
}
#新增一条记录
PUT accounts/person/1
{
  "user":"张三",
  "title":"工程师",
  "desc":"数据库管理"
}
#新增一条记录
PUT accounts/person/2
{
  "user":"李四",
  "title":"工程师",
  "desc":"系统管理"
}
#新增一条记录
PUT accounts/person/3
{
  "user":"李四2",
  "title":"工程师",
  "desc":"系统管理"
}

#返回所有记录
GET accounts/person/_search

#ES默认返回10条记录,可以通过size改变这个设置
#通过from字段指定位移
GET accounts/person/_search
{
  "query": {
    "match": {
      "desc": "管理"
    }
  },
  "from": 1,
  "size": 1
}

#执行多个关键字的and搜索,必须使用bool查询
GET accounts/person/_search
{
  "query": {
    "bool": {
      "must": [
          {"match": {"desc": "系统"}},
          {"match": {"desc": "管理"}}
      ]
    }
  }
}

#执行多个条件搜索,必须使用bool查询
GET accounts/person/_search
{
  "query": {
    "bool": {
      "filter": [
        {"term": {"user": "李四"}},
        {"term":{"desc":"系统"}}
      ]
    }
  }
}

后期再补充。版本不同,操作不同,初次学习,记录一下。

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。