Elasticsearch文档索引基本操作增删改查示例
来源:脚本之家    时间:2022-04-19 14:00:47

接口幂等性

0 接口幂等性:数学概念,多次请求,相当于一次请求get,put,delete都是幂等性的接口post 存在幂等性的问题前端速度很快,点了两次,会生成两个订单用户在访问新增页面时(提交订单)---》接口返回一个唯一id,提交订单,携带唯一id过来,后端判断这个唯一id是否被用过---》没用过,创建订单你在项目中碰到的问题和如何解决(项目收获)下订单,经常重复订单,点得快,幂等性问题,如何解决的

倒排索引

1.es介绍10个点

2.安装

-jdk :java开发环境

官网下载es相应的版本,解压,到bin目录下启动elasticsearch.bat两个客户端(kibana:官方,发送请求,数据统计,展示,elasticsearch-head:第三方用node写的)kibana跟es版本对应,bin路径下,直接启动elasticsearch-head需要安装node环境,npm install ---》npm run startkibana配置文件配置(copy一下,kibana监听的地址端口,随便名一个名,连接es的地址)修改es配置:允许跨域(浏览器的同源策略),cors:跨域资源共享,实现跨域es的倒排索引(扩展阅读.md)把文章进行分词,对每个词建立索引

3 索引操作

# 索引操作---》数据库操作---》新增,删除,修改,查询
# 1 新增索引:
PUT lqz2     # lqz2就是索引名字
{
  "settings": {
    "index":{
      "number_of_shards":5,
      "number_of_replicas":1
    }
  }
}
# 2 查询索引
GET lqz2/_settings
返回结果
{
  "lqz2" : {
    "settings" : {
      "index" : {
        "creation_date" : "1588822389842",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "NBXIeVdHQ26vCuPn8_6uew",
        "version" : {
          "created" : "7050099"
        },
        "provided_name" : "lqz2"
      }
    }
  }
}
# 3 更新索引
PUT lqz2/_settings
{
  "number_of_replicas": 2
}
#4 删除lqz2索引
DELETE lqz2

4 文档基本增删查改

#1  新增文档
POST lqz2/_doc/1
{
  "title":"红楼梦",
  "price":12,
  "publish_addr":{
    "province":"黑龙江",
    "city":"鹤岗"
  },
  "publish_date":"2013-11-11",
  "read_num":199,
  "tag":["古典","名著"]
}
POST lqz2/_doc/2
{
  "title":"西游记",
  "price":22,
  "publish_addr":{
    "province":"上海",
    "city":"上海"
  },
  "publish_date":"2013-11-11",
  "read_num":66,
  "tag":["古典","小说"]
}
# 2 查询文档(更详细的查找,后面讲)
GET lqz2/_doc/2
# 3 修改文档
-覆盖式(原来的字段就没有了)
PUT lqz/_doc/1
{
  "title":"xxxx",
  "price":333,
  "publish_addr":{
    "province":"黑龙江",
    "city":"福州"
  }
}
-增量式(只修改某个字段)一定要注意包在doc中
POST lqz2/_doc/1/_update
{
  "doc":{
    "title":"xx"
  }
}
# 4 删除文档
DELETE lqz/_doc/1
# 5 批量获取
GET _mget
{
  "docs":[
    {
      "_index":"lqz2",
      "_type":"_doc",
      "_id":2
    },
    {
      "_index":"lqz2",
      "_type":"_doc",
      "_id":1
    }
    ]
}
# 6 批量插入
PUT test/_doc/2/_create
{
  "field1" : "value22"
}
POST _bulk
{ "index" : { "_index" : "test", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_id" : "2" } }
{ "create" : { "_index" : "test", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }

5 文档查询

# 测试数据
PUT lqz/doc/1
{
  "name":"顾老二",
  "age":30,
  "from": "gu",
  "desc": "皮肤黑、武器长、性格直",
  "tags": ["黑", "长", "直"]
}
PUT lqz/doc/2
{
  "name":"大娘子",
  "age":18,
  "from":"sheng",
  "desc":"肤白貌美,娇憨可爱",
  "tags":["白", "富","美"]
}
PUT lqz/doc/3
{
  "name":"龙套偏房",
  "age":22,
  "from":"gu",
  "desc":"mmp,没怎么看,不知道怎么形容",
  "tags":["造数据", "真","难"]
}
# 两种方式
	-第一种(字符串查询)
  	GET lqz/doc/_search?q=from:gu
    GET lqz/doc/_search?q=age:22
  -查询的第二种方式(结构化)
  	GET lqz/doc/_search
    {
      "query": {
        "match": {
          "from": "gu"
        }
      }
    }
    GET lqz/doc/_search
    {
      "query": {
        "match": {
          "age": 22
        }
      }
    }
   GET lqz/doc/_search
    {
      "query": {
        "match": {
          "desc": "形容"
        }
      }
    }
GET lqz/doc/_search
{
  "query": {
    "match": {
      "tags": "造数据"
    }
  }
}

以上就是Elasticsearch文档索引基本操作增删改查示例的详细内容,更多关于Elasticsearch文档索引增删改查的资料请关注脚本之家其它相关文章!

关键词: 文档索引 基本操作 倒排索引 相关文章 资源共享

X 关闭

X 关闭