【elasticsearch学习笔记】

简介

文档和词条:一条数据对应一个文档,对文档中内容进行分词,得出的词语就是词条
倒排索引:对词条创建索引,并记录词条所在文档的信息。查询时现根据词条查到文档id,再获取文档。
概念对比:

MysqlES说明
TableIndex索引就是文档的集合
RowDocument文档是一条条的数据,文档是JSON格式
ColumnFieldJSON文档中的字段
SchemaMapping映射是索引中文档的约束,例如字段类型约束
SQLDSLDSL是ES提供的JSON风格的请求语句

ES,kibana,IK分词器 安装

  • windows下安装ES过程中报错
1. Error response from daemon: Ports are not available: listen tcp 0.0.0.0:9300: bind: An attempt was made to access a socket in a way forbidden by its access permissions.
通过如下命令查看hyper-v保留的部分tcp端口,把映射到本地的9300端口改成9201
netsh interface ipv4 show excludedportrange protocol=tcp
2. es8默认会开启安全默认,外部无法访问9200,启动时加`-e xpack.security.enabled=false` 关闭安全默认
-- ES
docker network create elastic

docker pull docker.elastic.co/elasticsearch/elasticsearch:8.4.3

docker run -d --name elasticsearch --net elastic -p 9200:9200 -p 9201:9300 -e "ES_JAVA_OPTS=-Xms512m -Xmx512m" -e "discovery.type=single-node" -e xpack.security.enabled=false -v /usr/share/elasticsearch/data:/d/elasticsearch/data -v /usr/share/elasticsearch/plugins:/d/elasticsearch/plugins --privileged -t docker.elastic.co/elasticsearch/elasticsearch:8.4.3
复制配置文件到本地
docker cp elasticsearch:/usr/share/elasticsearch/data D:\elasticsearch
docker cp elasticsearch:/usr/share/elasticsearch/plugins D:\elasticsearch
重启es
验证:http://localhost:9200
-- kibana
docker pull docker.elastic.co/kibana/kibana:8.4.3
docker run -d --name kibana --net elastic -p 5601:5601 docker.elastic.co/kibana/kibana:8.4.3
验证: http://localhost:5601
-- IK分词器
1. 下载zip包https://github.com/medcl/elasticsearch-analysis-ik/releases/tag/v8.4.3
2. 解压到your-es-root/plugins/ik下
3. 重启es
4. 验证
POST /_analyze
{
  "text": "美国留给伊拉克的是个烂摊子吗"
}

基础操作

索引
mapping属性

mapping是对索引库中文档的约束,常见的mapping属性有:

  • type 字段数据类型,常见类型有:
    字符串:text(可分词的文本),keyword(精确值,如:人名,国家)
    数值:long、integer、short、byte、double、float
    布尔: boolean
    日期:date
    对象:object
  • index:是否创建索引,默认true
  • analyzer:使用哪种分词器
  • properties:该字段的子字段
创建索引库
PUT /school
{
  "mappings": {
    "properties": {
      "name": {
        "properties": {
          "firstName": {
            "type": "keyword"
          }
        }
      },
      "email": {
        "type":"keyword",
        "index": false
      }
    }
  }
}
查看、删除索引库

GET /索引库名称
DELETE /索引库名称

修改索引库(添加新字段)

索引库和mapping一旦创建无法修改,但是可以添加新的字段

PUT /索引库名/_mapping
{
	"properties": {
		"新字段名": {
			"type": "integer"
		}
	}
}
文档
插入文档
POST /索引库/_doc/文档id
{
	 json文档
}
查看、删除文档

GET /索引库/_doc/文档id
DELETE /索引库/_doc/文档id

修改文档
  • 全量修改(文档id不存在时会新增,存在时先删除再新增)
PUT /索引库/_doc/文档id
{
	 json文档
}
  • 局部修改(修改字段)
POST /索引库/_update/文档id
{
    "doc": {
		"xxx": "yyy"
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值