文章目录
前言
包含内容
- 索引创建
- mapping 添加修改
- 集群健康度
GET /_cluster/health?pretty
{
"cluster_name" : "test-es-cluster",
"status" : "green", #green 健康 yellow 亚健康 red 病了
"timed_out" : false,
"number_of_nodes" : 3, # 节点数
"number_of_data_nodes" : 3, # 数据节点
"active_primary_shards" : 264,
"active_shards" : 525,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 0,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 100.0
}
- 查看所有索引状态
GET /_cat/indices
yellow open test_index_1 MzjlWDJMR3ytnwERULaWsA 5 1 0 0 1kb 1kb
green open test_index UNQgrksETpi7rPjGKv-WDA 3 0 0 0 624b 624b
- 创建索引
PUT /test_index
{
"settings":{
"index":{
"number_of_shards": 5,
"number_of_replicas": 1
}
}
}
numner_of_shards: 分片数量,默认时1
number_of_replicas: 副本数量一般与节点数保持一致,默认时1
- 查看创建索引
GET /test_index
- 创建索引并添加mapping
PUT /test_m1
{
"settings": {
"index": {
"number_of_shards": 3,
"number_of_replicas": 0
}
},
"mappings": {
"properties": {
"username": {
"type": "text",
"index": true
},
"password": {
"type": "keyword",
"index": false
}
}
}
}
index:false 不进行索引
- 创建mapping,不指定index
PUT /test_m2
{
"mappings": {
"properties": {
"username": {
"type": "text",
"index": true
},
"password": {
"type": "keyword",
"index": false
}
}
}
}
新增mappings 时不指定索引分片副本,默认是1副本1分片
- 给新建索引添加mapping
PUT /test_m3
{
"mappings":{
"properties":{
"username":{
"type":"text",
"index":true
},
"keyword":{
"type":"keyword",
"index":false
}
}
}
}
#另一种方式
PUT /test_m3/_mapping
{
"properties":{
"username":{
"type":"text",
"index":true
},
"keyword":{
"type":"keyword",
"index":false
}
}
}
小结
mappings 一旦创建成功后,不能修改,只剩查询、删除
添加文档
- 新增文档数据
POST /test_m1/_doc/1
{
"id":1,
"username":"貂蝉",
“password":"123456",
"age":18,
"gender":"女",
"desc":"四大美女之一",
"date_time":"2010-02-23"
}
- 更新,部分更新
POST /test_m1/_doc/1
{
"doc":{
"desc": "三国美女"
}
}
- 整体更新
POST /test_m1/_doc/1/_update
{
"id":1,
"username":"貂蝉1111",
“password":"123456",
"age":18,
"gender":"女",
"desc":"四大美女之一",
"date_time":"2010-02-23"
}
- 删除
DELETE /test_m1/_doc/1
- 查询,根据id查询
GET /test_m1/_doc/1
- 查询全部
GET /test_m1/_doc/_search
分词
- text
username 类型是text,默认分词器使用空格隔开
GET /test_m1/_analyze
{
"field": "username",
"text": "this is a good idea"
}
- kerword
password 类型是keyword,作为一个整体不会进行切分
GET /test_m1/_analyze
{
"field": "password",
"text": "this is a good idea"
}
- 测试任意分词
GET /test_m1/_analyze
{
"analyzer": "standard",
"text": "THIS is a Good Ideas"
}
使用standard
对text 进行分词
es 内置了很多分词器,如下
standard: 默认分词,单词会被拆分,大写转小写
simple:按照字母分词,大写转小写
whitespace:按照空格分词,大写转小写
stop: 去掉停用词,大写转小写
keyword:不分词,当成一个整体
中文分词
GET /test_m1/_analyze
{
"analyzer": ”ik_max_word“,
"text": "燃烧我的卡路里"
}
查询
- QueryString
GET http://192.168.75.206:9200/test_index/_search?q=desc:爱学习&q=gender:男
DSL
- 判断是否存在,并展示存在该字段的数据
GET http://192.168.75.206:9200/test_index/_search
{
"query":{
"existis":{
"fiels":"username"
}
}
}
单个字段查询
- match 必须匹配
GET /test_m1/_analyze
{
"query": {
"match": {
"nickname": {
"query": "tom",
"analyzer": "standrad"
}
}
}
}
match
先对要查询的词进行分词进行全文检索,再查询出符合分词的结果匹配具体的某一个字段,可以先对查询的词分词看线,分此后结果。
analyzer
指定查询时使用的分词器,必须与存储时使用的分词器一致,存储时分词器,可通过查看mapping
查看
- match or/and
GET /test_index/_search
# and 必须满足所有条件
{
“query”:{
"match":{
"desc":"学习 java",
"operator": "and"
}
}
}
# or 必须满足一个条件即可
{
“query”:{
"match":{
"desc":"学习 java",
"operator": "or"
}
}
}
- match - minimum_should_match
GET /test_index/_search
{
"query":{
"match":{
"desc":{
"query":"我在学习elasticsearch日志分析",
"minimum_should_match":"80%"
}
}
}
}
minimum_should_match
最小精度匹配,一句话分词后匹配的词的个数. 分词后的词语个数X百分比,得出的数值,就是匹配的精度。举个栗子:一句话分词后又10个词组,如果百分比为50%, 10*50%=5,至少有5个词组匹配就能检索出。
minimum_should_match
可选值,数字或者百分比
-
multi_match
在多个字段中匹配,类似于 or
GET /test_index/_search
{
"query":{
"multi_match":{
"query":"骑着毛驴去旅行",
"fields":["username","desc"]
}
}
}
# 查询的词先进行分词
GET /_analyzer
{
"text": "骑着毛驴去旅行",
"analyzer": "ik_max_word"
}
multi_match
查询username、desc
两个字段,满足骑着毛驴去旅行
分词后词语,多个条件是or
连接
- 指定字段和分页
GET /test_index/_search
{
"query":{
"match_all": {}
},
"_source":["username","nickname","city"],
"from":0,
"size":3
}
match_all
:查询所有,_source
: 显示那些字段
- term 精确匹配
GET /test_index/_search
{
"query":{
"term":{
"username":"骑着毛驴去旅行"
}
}
}
term 查询按查询词 骑着毛驴去旅行
当作一个整体去查询,如果搜索的词username
mapping 类型为非keyword
查询不到。
原因很简单,存储时username 按照默认分词存储,会把骑着毛驴去旅行
进行分词,查询时(trem
)又是按照一个整体去查, 肯定检索不到。
解决:1. 查询时按照username.keyword
去查 2. 设置username 类型为keyword
- terms 匹配多个词
GET /test_index/_search
{
"query":{
"terms":{
"username":["骑着毛驴去旅行","他大舅他二舅都是他舅"]
}
},
"_source":["username", "age", "city"],
"from":0,
"size":5
}
- match_phare短语查询
GET /test_index/_search
{
"query":{
"match_phare":{
"desc":{
"query":"学习 es",
”slop": 2
}
}
}
}
match_phare
先分词再查询,只要查询的目标文档中包含学习 es
并且两个词之间的间隔为2即可命中查询
- 主键查询
GET/POST /test_index/_search
{
"query":{
"ids":{
"values":["1","3"]
}
},
"_source":["username", "gender", "city"]
}
-
权重boost
权重越高,文档的相关性越大通过
^
设置权重
GET /test_index/_search
{
"query":{
"multi_match":{
"query":"骑着毛驴去旅行"
"fields":["desc", "username^5"]
}
}
}
多个字段查询
must 查询条件必须全部匹配,等同于 and
- bool 同时查询多个字段
GET /test_index/_search
{
"query":{
"bool":{
"must":[
{
"match":{
"desc":"学习"
}
},
{
"term":{
"age":18
}
}
]
}
}
}
- should 查询条件满足1个以上,类似or
GET /test_index/_search
# 类似 select desc, age from person where desc like '%学习%' or age = 18
{
"query":{
"bool":{
"should":[
{
"match":{
"desc":"学习"
}
},
{
"term":{
"age": 18
}
}
]
}
}
}
must、should、must_not虽然在语法上可以并列写,但must和should组合,should就无效
must和must_not组合时,must筛选后,会将符合must_not条件的剔除
嵌套查询
must于must_not同时使用时,如果要同时生效,可使用如下方法
- must 与should 同时使用
- “minimum_should_match”:1 should条件就生效了,1 的意思是至少满足should中1个条件
GET /test_index/_search
{
"query": {
"bool":{
"must":[
{
"match":{
"desc":"学习"
}
}
],
"should":[
{
"match":{
"nickname":"太阳"
}
},
{
"term":{
"gender":"男
}
}
],
"minimum_should_match":1
}
}
}
- should嵌套 must
- where (desc=‘学习’ and nicknanme=‘日出’) or (desc=‘学习’ and username=‘jacktian’)
GET /test_index/_search
{
"query":{
"bool":{
"should":[
{
"bool":{
"must":[
{
"match":{
"desc":"学习"
}
},
{
"match":{
"nickname":"日出"
}
}
]
}
},
{
"bool":{
"must":[
{
"match":{
"desc":"学习"
}
},
{
"term":{
"username":"jacktian"
}
}
]
}
}
]
}
}
}
规则
bool 包好多个查询条件,
must、should、must_not:[] 形式,包含多个不同字段。类似于sql(username=‘张三’ and/or password=‘123456’
match、term、multi_match:具体某一个字段的查询条件类似于sql(username=‘张三’)
- must嵌套should
- where desc=‘学习’ and (nicknanme=‘日出’ or username=‘jacktian’)
GET /test_index/_search
{
"query":{
"bool":{
"must":[
{
"match":{
"desc":"学习"
}
},
{
"bool":{
"should":[
{
"match":{
"nickname":"日出"
}
},
{
"term":{
"username":"jacktian"
}
}
]
}
}
]
}
}
}
- range
GET /test_index/_search
{
"query":{
"range":{
"price":{
"gte": 1000,
"lte": 5000
}
}
}
}
查询价格再[1000, 5000]之间数据
批量操作
- mapping
PUT /bulk_index
{
"settings": {
"index": {
"number_of_shards": 3,
"number_of_replicas": 0
}
},
"mappings": {
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "keyword"
},
"nickname": {
"type": "text",
"analyzer": "ik_max_word"
},
"age": {
"type": "integer"
},
"gender": {
"type": "text"
},
"city": {
"type": "keyword"
},
"desc": {
"type": "text",
"analyzer": "ik_max_word"
},
"consume": {
"type": "long"
}
}
}
}
POST /_bulk
{"create":{"_index":"bulk_index","_type":"_doc","_id":1}}
{"id":1,"name":"诸葛亮","nickname":"骑着毛驴去旅行","age":23,"gender":"男","city":"北京","desc":"我在学习elsticsearch,了解了很多新知识,进步很大","consume":2000.36}
{"create":{"_index":"bulk_index","_type":"_doc","_id":2}}
{"id":2,"name":"张飞","nickname":"一只小蜗牛","age":26,"gender":"男","city":"上海","desc":"一起来学习java,共同交流","consume":6239.12}
{"create":{"_index":"bulk_index","_type":"_doc","_id":3}}
{"id":3,"name":"貂蝉","nickname":"采蘑菇的小姑娘","age":18,"gender":"女","city":"郑州","desc":"喜欢舞蹈、唱歌,撸串,在学习吉他","consume":5000.75}
{"create":{"_index":"bulk_index","_type":"_doc","_id":2}}
{"id":4,"name":"武则天","nickname":"女王陛下","age":32,"gender":"女","city":"西安","desc":"我在学习mysql高级引用,认识了很多新同学,以后想成为一名老师","consume":8000.25}
{"create":{"_index":"bulk_index","_type":"_doc","_id":5}}
{"id":5,"name":"秦始皇","nickname":"飞上太空和太阳肩并肩","age":40,"gender":"男","city":"深圳","desc":"技术宅,喜欢编程,喜欢撸串、炸鸡、啤酒、可乐、矿泉水","consume":8000.25}
- index
POST /bulk_index/_doc/_bulk
{"index":{"_id":1}}
{"id":1,"name":"诸葛亮","nickname":"骑着毛驴去旅行","age":23,"gender":"男","city":"北京","desc":"我在学习elsticsearch,了解了很多新知识,进步很大","consume":2000.36}
{"index":{"_id":2}}
{"id":2,"name":"张飞","nickname":"一只小蜗牛","age":26,"gender":"男","city":"上海","desc":"一起来学习java,共同交流","consume":6239.12}
{"index":{"_id":3}}
{"id":3,"name":"貂蝉","nickname":"采蘑菇的小姑娘","age":18,"gender":"女","city":"郑州","desc":"喜欢舞蹈、唱歌,撸串,在学习吉他","consume":5000.75}
- update
POST /bulk_index/_doc/_bulk
{"update":{"_id":1}}
{"doc":{"name":"诸葛亮111"}}
{"update":{"_id":2}}
{"doc":{"desc":"java学完了,开始学习golang"}}
- 删除
POST /bulk_index/_doc/_bulk
{"delete":{"_id":1}}
{"delete":{"_id":2}}
模板
POST /_scripts/tpdesc
{
"script": {
"lang": "mustache",
"source": {
"query": {
"match": {
"desc": {
"query": "{{desc_value}}",
"analyzer": "{{desc_analyzer}}"
}
}
}
}
}
}
# 查询
GET /test_index/_search/tpdesc
{
"id":"tpdesc",
"params":{
"desc_value":"学习",
"desc_analyzer":"ik_max_word"
}
}
- update_by_word
{
"query":{
"match":{
"nickanme":"骑着毛驴去旅行"
}
}
}
- 创建索引
PUT /update_index_test
{
"settings": {
"index": {
"number_of_shards": 5,
"number_of_replicas": 0
}
},
"mappings": {
"properties": {
"username": {
"type": "text",
"analyzer": "ik_max_word"
},
"nickname": {
"type": "text",
"analyzer": "ik_max_word",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}
# 写数据
POST /update_index_test/_doc/_bulk
{"index":{"_id":1}}
{"id":1,"username":"诸葛亮","nickname":"骑着毛驴去旅行"}
{"index":{"_id":2}}
{"id":2,"username":"张飞","nickname":"一只小蜗牛"}
{"index":{"_id":3}}
{"id":3,"username":"貂蝉","nickname":"采蘑菇的小姑娘"}