elasticsearch的基本使用实操

elasticsearch中的索引指的是indices索引库

1.先创建索引库,indices
概念

存储数据到 Elasticsearch 的行为叫做 索引 (indexing)

关于数据的概念

Relational DB -> Databases 数据库 -> Tables 表 -> Rows 行 -> Columns 列
Elasticsearch -> Indices 索引库 -> Types 类型 -> Documents 文档 -> Fields 字段/属性
一个 Elasticsearch 集群可以 包含多个 索引 (indices 数据库),相应的每个索引可以包含多个 类型(type 表) 。 这些不同的类型存储着多个 文档(document 数据行) ,每个文档又有 多个 属性 (field 列)。

curl -X PUT 127.0.0.1:9200/{indices,索引}/{_mapping,类型}

curl 127.0.0.1:9200/_cluster/health # 查询状态


```python
curl -X PUT 127.0.0.1:9200/articles -H 'Content-Type:application/json' -d '
    {
        "settings":{
			"index":{
				"number_of_shards":3,
                "number_of_replicas":1,
                
            }
        
        }
    }
    # number_of_shards是主分片,
    # number_of_replicas是副分片,注意设置一次后,只可以重新设置副分片
2.进行类型映射 _mapping
curl -X PUT 127.0.0.1:9200/articles/_mapping/article -H 'Content-type: application/json' -d '
    {
       
        "_all":{
			"analyzer":"ik_max_word"		
        },
        "properties":{
			"article_id":{
                "type":"long",
                "include_in_all":"flase"
            },
            "user_id":{
                "type":"long",
                "include_in_all":"flase"
            },
            "title":{
                "type":"text",
                "include_in_all":"true",
                "analyzer":"ik_max_word",
                "boost":2
            },
            "content":{
                "type":"text",
                "include_in_all":"true",
                "analyzer":"ik_max_word",
                "store":"flase"
            },
            "status":{
                "type":"integer",
                "include_in_all":"flase"
            },
            "create_time":{
                "type":"date",
                "include_in_all":"flase"
            }
        }
    }'
     # _all字段是把所有其它字段中的值,以空格为分隔符组成一个大字符串,然后被分析和索引,但是不存储,也就是说它能被查询,但不能被取回显示。_all允许在不知道要查找的内容是属于哪个具体字段的情况下进行搜索。
     # properties是用来放置字段的
    # analyzer 是用来定义分析方法的,默认是standard
    # include_in_all 控制_all中要查询的字段,默认是true,
    # boost是权重分数,分数越大越靠前,分数一般不大于十
    
   # 查询类型映射使用,curl 127.0.0.1:9200/articels/_mapping/article?pretty
映射修改

一个类型映射创建好后,可以为类型增加新的字段映射

curl -X PUT 127.0.0.1:9200/articles/_mapping/article -H 'Content-Type:application/json' -d '
{
  "properties": {
    "new_tag": {
      "type": "text"
    }
  }
}
'

但是不能修改已有字段的类型映射,原因在于elasticsearch已按照原有字段映射生成了反向索引数据,类型映射改变意味着需要重新构建反向索引数据,所以并不能再原有基础上修改,只能新建索引库,然后创建类型映射后重新构建反向索引数据。

例如,将status字段类型由integer改为byte会报错

curl -X PUT 127.0.0.1:9200/articles/_mapping/article -H 'Content-Type:application/json' -d '
{
  "properties": {
    "status": {
      "type": "byte"
    }
  }
}
'

需要从新建立索引

curl -X PUT 127.0.0.1:9200/articles_v2 -H 'Content-Type: application/json' -d'
{
   "settings" : {
      "index": {
          "number_of_shards" : 3,
          "number_of_replicas" : 1
       }
   }
}
'

curl -X PUT 127.0.0.1:9200/articles_v2/_mapping/article -H 'Content-Type: application/json' -d'
{
     "_all": {
          "analyzer": "ik_max_word"
      },
      "properties": {
          "article_id": {
              "type": "long",
              "include_in_all": "false"
          },
          "user_id": {
               "type": "long",
              "include_in_all": "false"
          },
          "title": {
              "type": "text",
              "analyzer": "ik_max_word",
              "include_in_all": "true",
              "boost": 2
          },
          "content": {
              "type": "text",
              "analyzer": "ik_max_word",
              "include_in_all": "true"
          },
          "status": {
              "type": "byte",
              "include_in_all": "false"
          },
          "create_time": {
              "type": "date",
              "include_in_all": "false"
          }
      }
}
'
重新索引数据
curl -X POST 127.0.0.1:9200/_reindex -H 'Content-Type:application/json' -d '
{
  "source": {
    "index": "articles"
  },
  "dest": {
    "index": "articles_v2"
  }
}
'
为索引起别名

为索引起别名,让新建的索引具有原索引的名字,可以让应用程序零停机。

curl -X DELETE 127.0.0.1:9200/articles
curl -X PUT 127.0.0.1:9200/articles_v2/_alias/articles

查询索引别名

# 查看别名指向哪个索引
curl 127.0.0.1:9200/*/_alias/articles

# 查看哪些别名指向这个索引
curl 127.0.0.1:9200/articles_v2/_alias/*

文档

一个文档的实例

{
    "name":         "John Smith",
    "age":          42,
    "confirmed":    true,
    "join_date":    "2014-06-01",
    "home": {
        "lat":      51.5,
        "lon":      0.1
    },
    "accounts": [
        {
            "type": "facebook",
            "id":   "johnsmith"
        },
        {
            "type": "twitter",
            "id":   "johnsmith"
        }
    ]
}

一个文档不仅仅包含它的数据 ,也包含 元数据(metadata) —— 有关文档的信息。 三个必须的元数据元素如下:

  • _index

    文档在哪存放

  • _type

    文档表示的对象类别

  • _id

    文档唯一标识

索引文档(保存文档数据)
自定义文档id
PUT /{索引库indices}/{类型_mapping,type}/{id}
curl -X PUT 127.0.0.1:9200/articles/article/150000 -H 'Content-Type:application/json' -d'
{
	"field":"values",
	"article_id": 150000,
  	"user_id": 1,
  	"title": "python是世界上最好的语言",
  	"content": "确实如此",
    "status": 2,
  	"create_time": "2019-04-03"
}'

自动生成文档id

PUT /{index}/{type}
{
  "field": "value",
  ...
}

获取指定文档

curl 127.0.0.1:9200/articles/article/150000?pretty

# 获取一部分
curl 127.0.0.1:9200/articles/article/150000?_source=title,content\&pretty

注意:_version 每次修改文档数据,版本都会增加,可以当作乐观锁的依赖(判断标准)使用

判断文档是否存在

curl -i -X HEAD 127.0.0.1:9200/articles/article/150000
  • 存在 200状态码
  • 不存在 404状态码

更新文档

在 Elasticsearch 中文档是 不可改变 的,不能修改它们。 相反,如果想要更新现有的文档,需要 重建索引或者进行替换。我们可以使用相同的 index API 进行实现。

例如修改title字段的内容,不可进行以下操作(仅传递title字段内容)

curl -X PUT 127.0.0.1:9200/articles/article/150000 -H 'Content-Type:application/json' -d '
{
  "title": "python必须是世界上最好的语言"
}'

而是要索引完整文档内容

curl -X PUT 127.0.0.1:9200/articles/article/150000 -H 'Content-Type:application/json' -d '
{
  "article_id": 150000,
  "user_id": 1,
  "title": "python必须是世界上最好的语言",
  "content": "确实如此",
  "status": 2,
  "create_time": "2019-04-03"
}'

注意返回值_version的变化

删除文档

curl -X DELETE 127.0.0.1:9200/articles/article/150000
取回多个文档
curl -X GET 127.0.0.1:9200/_mget -d '
{
  "docs": [
    {
      "_index": "articles",
      "_type": "article",
      "_id": 150000
    },
    {
      "_index": "articles",
      "_type": "article",
      "_id": 150001
    }
  ]
}'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值