菜单

Es相关操作记录

duckflew
发布于 2021-08-07 / 227 阅读
0
0

Es相关操作记录

Es相关操作记录

DELETE /bms
GET /ems
# 新建一个bms索引并且指定一个book类型以及确定mapping映射 映射就是对字段的约束 6.x版本开始就只能一个索引新建一个类型 但是兼容多个类型 也就是不能新建但是可以兼容  7.x以后彻底删除
PUT /bms
{
  "mappings":{
    "book":{
     "properties":{
       "id":{
         "type":"integer"
       },
       "name":{
         "type":"text"
         , "analyzer": "ik_max_word"
         , "search_analyzer": "ik_smart"
       },
       "author":{
         "type": "text"
          , "analyzer": "ik_max_word"
         , "search_analyzer": "ik_smart"
       }
     }
    }
  }
}
#查询索引信息
GET /bms/_mapping/book

# 插入一条文档
PUT  /bms/book/2
{
   "id": "2",
    "name": "时间简史",
    "author": "霍金"
}
# 插入一条文档
PUT  /bms/book/1
{
   "id": "1",
    "name": "测试书籍",
    "author": "黄金唯"
}
# 插入一条文档
PUT  /bms/book/3
{
   "id": "3",
    "name": "黄金时代",
    "author": "王小波"
}
# 插入一条文档
PUT  /bms/book/4
{
   "id": "4",
    "name": "杠精的修养",
    "author": "碰瓷"
}
# 插入一条文档
PUT  /bms/book/5
{
   "id": "5",
    "name": "论中国信息产业发展",
    "author": "江泽民"
}
# 更新一条文档 并且只更新部分字段
POST /bms/book/1/_update
{
  "doc":{
   "id":"1"  
  }
}
# 查询所有文档
GET /bms/book/_search
# 删除一条文档
DELETE /bms/book/36
# 查询并排序
GET /bms/book/_search?q=*&sort=id:desc
GET /bms/book/_search?q=*&sort=id

# 匹配字段  并且分页 from从0开始 source指定查询的哪些字段  match会将查询的值进行分词
GET /bms/book/_search 
{
  "query": {
    "match": {
      "name": "黄金时代"
    }
  },
  "size": 1,
  "from": 0,
  "_source": ["name","author"]
}
GET /bms/book/_search 
{
  "query": {
    "term": {
      "name":{
        "value": "黄金"
      }
    }
  },
  "size": 1,
  "from": 0,
  "_source": ["name","author"]
}
# 范围查询
GET bms/book/_search
{
  "query": {
    "range": {
      "id": {
        "gte": 35,
        "lte": 100
      }
    }    
  }
}
GET bms/book/_search
{
  "query": {
    "prefix": {
      "name": {
        "value": "黄金"
      }
    }
  }
}
#前缀查询
略
#  ?表示一个分词   *表示通配
GET bms/book/_search
{
  "query": {
    "wildcard": {
      "name": {
        "value": "黄金??"
      }
    }
  }
}
GET bms/book/_search
{
  "query": {
    "wildcard": {
      "name": {
        "value": "黄金*"
      }
    }
  }
}
# 多ID查询
GET bms/book/_search
{
  "query": {
    "ids": {
      "values": ["1","35","36"]
    }
  }
}
# 模糊查询 
GET bms/book/_search
{
  "query": {
    "fuzzy": {
      "name": "黄"
    }
  }
}
# Bool查询 must mustnot should should表示满足一个条件即可
# must 表示要满足所有条件
GET bms/book/_search
{
  "query": 
  {
    "bool": 
    {
      "must_not": 
      [
        {
          "term": 
          {
            "id": 
            {
              "value": "34"
            }
          }
        },
        {
          "term": {
            "id": {
              "value": "36"
            }
          }
        }
      ]
    }
  }
}
# 关键词高亮
GET bms/book/_search
{
  "query": {
    "term": {
      "name": {
        "value": "黄金时代"
      }
    }
  }
  , "highlight": {
    "fields": {
      "*":{}
    },
    "pre_tags": {
     
    }, 
    "post_tags": {}
  }
}
# 多字段分词查询
GET bms/book/_search
{
  "query": {
    "multi_match": {
      "query": "杠精",
      "fields": [
        "name",
        "author"
        ]
    }
  }
}
# 多字段分词查询 query_string
GET bms/book/_search
{
  "query": {
    "query_string": {
      "default_field": "name",
      "query": "黄金"
    }
  }
}
GET _analyze
{
  "text": "江泽民",
  "analyzer": "ik_smart"
}
# 扩展词查询  (term不是分词查询 所以查询不到 索引库没有"杠精"的索引 )
#配置完了扩展词要删除所有的数据重新启动Es当然也许是BUG 理论上这是不合理的 实际生产考虑别# 的办法吧
GET /bms/book/_search
{
  "query": {
    "term": {
      "author": {
        "value": "碰瓷"
      }
    }
  }
}
GET /bms/book/_search
{
  "query": {
    "term": {
      "author": {
        "value": "江泽民"
      }
    }
  }
}


评论