python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Elasticsearch文档索引增删改查

Elasticsearch文档索引基本操作增删改查示例

作者:Jeff的技术栈

这篇文章主要为答案及介绍了Elasticsearch文档索引基本操作增删改查示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

接口幂等性

倒排索引

1.es介绍10个点

2.安装

-jdk :java开发环境

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文档索引增删改查的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文