一、概述
- Index(索引)
相当于Mysql中的Database - Type(类型)
在index(索引)中,可以定义一个或多个类型
类似于MySQL中的Table,每一种类型的数据放在一起 - Dcoument(文档)
保存在某个索引(Index)下,某种类型(Type)的一个数据(Document),文档是JSON格式的,Docment就像是MySQL中的某个Table里面的内容
根据规划,Elastic 6.x 版只允许每个 Index 包含一个 Type,7.x 版将会彻底移除 Type,默认type为_doc
二、安装
kibana是elasticsearch的可视化界面
1.下载镜像
docker pull elasticsearch:7.13.0
docker pull kibana:7.13.0
2.docker 方式启动
docker run -d
-p 9200:9200
-p 9300:9300
-e ES_JAVA_POTS="-Xms1280m -Xmx128m"
-e "discovery.type=single-node"
--name elasticsearch_9200
elasticsearch:7.13.0
docker run -d
--name kibana_5601
--link elasticsearch_9200:elasticsearch
-p 5601:5601
kibana:7.13.0
elasticsearch修改配置,解决跨域访问问题
修改文件/user/share/elasticsearch/config/elasticsearch.yml
末尾加上
http.cors.enabled: true
http.cors.allow-origin: "*"
请求http://localhost:9200
,查看是否启动成功
3.docker compose 方式启动
下方有 es_data 目录映射,需要先建 es_data 目录,否则 es 可能会报 AccessDeniedException
version: '3.8'
services:
elasticsearch:
container_name: elasticsearch_9200
image: elasticsearch:7.13.0
environment:
- discovery.type=single-node
- ELASTIC_PASSWORD='123456'
volumes:
- ./es_data:/usr/share/elasticsearch/data
ports:
- 9200:9200
- 9300:9300
kibana:
container_name: kibana_5601
image: kibana:7.13.0
depends_on:
- elasticsearch
environment:
- ELASTICSEARCH_HOSTS=http://elasticsearch:9200
- ELASTICSEARCH_USERNAME=elastic
- ELASTICSEARCH_PASSWORD='123456'
ports:
- 5601:5601
4._cat
GET /_cat/nodes
查看所有节点
GET /_cat/health
查看es健康状况
GET /_cat/master
查看主节点
GET /_cat/indices
查看所有索引
GET方式请求http://localhost:9200/_cat/indices?v
,查看所有的索引
三、操作索引
7.x 版,索引相当于库名表名,mapping相当于字段名称和类型
可以新增mapping的字段,不可以修改mapping字段的类型
1.创建索引和mapping
kibana命令:
PUT /person
{
"mappings" : {
"properties" : {
"name" : {
"type": "keyword"
},
"age": {
"type": "integer"
},
"sex": {
"type": "keyword"
},
"desc": {
"type":"text",
"analyzer": "standard"
}
}
}
}
如果是使用curl命令:
curl -X PUT http://localhost:9200/person/?pretty -H 'content-Type:application/json' -d '
{
"mappings" : {
"properties" : {
"name" : {
"type": "keyword"
},
"age": {
"type": "integer"
},
"sex": {
"type": "keyword"
},
"desc": {
"type":"text",
"analyzer": "standard"
}
}
}
}
'
2.创建索引
PUT /person
3.新增mapping的字段
POST /person/_mappings
{
"properties" : {
"name" : {
"type": "keyword"
},
"age": {
"type": "integer"
},
"sex": {
"type": "keyword"
},
"desc": {
"type":"text",
"analyzer": "standard"
}
}
}
4.查看索引和mapping
GET /person
GET /person/_mapping
5.删除索引
DELETE /person
四、操作文档
1.新增文档
PUT和POST请求是可以新增修改
PUT必须带id
POST可带可不带id,指定id,如果不指定id,会自动生成一个id序列
如果id不存在则新增;如果id存在,底层删除以前的文档,然后新增文档。
PUT /person/_doc/1
{
"name": "zhangsan",
"age": 21
}
POST /person/_doc/1
{
"name": "zhangsan",
"age": 21
}
使用_create
,如果id存在,则报错
POST /person/_create/1
{
"name": "zhangsan",
"age": 21
}
批量新增文档
每一行包含描述操作说明的 JSON 对象,第二行为 JSON 对象本身
POST /person/_bulk
{"index": {"_id": "1"}}
{"name": "zhang san", "age": 21, "desc": "zhang san desc"}
{"create": {"_id": "2"}}
{"name": "li si", "age": 22, "desc": "li si desc"}
{"create": {"_id": "3"}}
{"name": "zhang si", "age": 23, "desc": "zhang si desc"}
2.修改文档
使用_update
,用于只会修改指定字段
底层从旧文档构建 JSON,更改该 JSON,删除旧文档,索引一个新文档
POST /person/_update/1
{
"doc": {
"age": 22
}
}
3.查询文档
GET /person/_doc/1
4.删除文档
DELETE /person/_doc/1
五、检索文档
1.term
term是代表完全匹配,即不进行分词器分析,文档中必须包含整个搜索的词汇
GET /person/_search
{
"query": {
"term": {
"name": "zhang san"
}
}
}
terms
精确匹配多个值,相当于in
GET /person/_search
{
"query": {
"terms": {
"name": ["zhang san", "li si"]
}
}
}
2.match
GET /person/_search
{
"query": {
"match": {
"desc": {
"query": "zhang san"
}
}
}
}
operator,默认是or,可以指定and
GET /person/_search
{
"query": {
"match": {
"desc": {
"query": "zhang san",
"operator": "and"
}
}
}
}
match_phrase
称为短语搜索,要求所有的分词必须同时出现在文档中,同时位置必须紧邻一致
GET /person/_search
{
"query": {
"match_phrase": {
"desc": "zhang san"
}
}
}
match_all
查询所有数据
GET /person/_search
{
"query": {
"match_all": {}
}
}
3.range
gt (大于),gte(大于等于),lt(小于),lte(小于等于)
GET /person/_search
{
"query": {
"range": {
"age": {
"gt": 21,
"lt": 23
}
}
}
}
4.bool
bool查询包含四种操作符,分别是must,should,must_not,filter
-
must: 必须匹配,与and等价。贡献算分
-
must_not:必须不匹配,与not等价,常过滤子句用,但不贡献算分
-
should: 选择性匹配,至少满足一条,与 OR 等价。贡献算分
-
filter: 过滤子句,必须匹配,但不贡献算分
filter会对查询结果缓存,不会计算相关度,避免计算分值,执行速度非常快
must
GET /person/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"name": "zhang san"
}
},
{
"term": {
"age": 21
}
}
]
}
}
}
must_not
GET /person/_search
{
"query": {
"bool": {
"must_not": [
{
"term": {
"name": "zhang san"
}
},
{
"term": {
"age": 21
}
}
]
}
}
}
should
GET /person/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"name": "zhang san"
}
},
{
"term": {
"age": 22
}
}
]
}
}
}
filter
GET /person/_search
{
"query": {
"bool": {
"filter": [
{
"term": {
"name": "zhang san"
}
},
{
"term": {
"age": 21
}
}
]
}
}
}
六、排序、高亮、去重
1.sort
GET /person/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"age": { "order": "desc"}
}
]
}
2.highlight
GET /person/_search
{
"query": {
"match": {
"desc": {
"query": "zhang san"
}
}
},
"highlight": {
"fields": {
"desc": {}
}
}
}
highlight指定标签
GET /person/_search
{
"query": {
"match": {
"desc": {
"query": "zhang san"
}
}
},
"highlight": {
"pre_tags": "<font color='red'>",
"post_tags": "</font>",
"fields": {
"desc": {}
}
}
}
3.collapse
collapse去重
GET /person/_search
{
"query": {
"match_all": {}
},
"collapse": {
"field": "name"
}
}