以下内容基于es5.4.2版本,其他版本可能略有出入。
一,创建ES模板
PUT /_template/test_index
{
"order" : 1,
"template" : "test_index~*",
"aliases": {
"test_index": {}
},
"mappings" : {
"default" : {
"_source" : {
"excludes" : [
"*Query"
]
},
"properties": {
"noShowQuery": {
"type": "text",
"analyzer": "whitespace"
},
"ikItem": {
"type": "text",
"analyzer": "ik_max_word"
},
"fItem": {
"type": "float"
},
"kItem": {
"type": "keyword"
},
"bItem": {
"type": "boolean"
}
}
}
},
"settings" : {
"index" : {
"number_of_shards" : "30",
"number_of_replicas" : "1"
}
}
}
二,模糊查询
GET test_index/default/_search
{
"query": {
"wildcard": {
"kItem": "*abc*"
}
}
}
三,Forcemerge
curl -XPOST "http://10.10.10.10:9200/test_index~2021-07/_forcemerge?only_expunge_deletes=true"
四,IK测试
-- test _analyze
-- ik_max_word/ik_smart/standard
GET /_analyze?
{"analyzer" : "standard", "text" : "床前明月光"}
五,前缀匹配查询
curl -XGET "http://10.10.10.10:9200/test_index/default/_search" -H 'Content-Type: application/json' -d'
{
"query": {
"prefix": {
"phone": "13"
}
}
}'
六,条件查询
curl -XGET "http://10.10.10.10:9200/test_index/default/_search" -H 'Content-Type: application/json' -d'
{
"query" : {
"bool" : {
"must" : [
{
"terms" : {
"addr" : [
"some where"
],
"boost" : 1.0
}
},
{
"terms" : {
"phone" : [
"13304772122"
],
"boost" : 1.0
}
}
],
"disable_coord" : false,
"adjust_pure_negative" : true,
"boost" : 1.0
}
}
}'
七,设置字段不索引
索引index
这个参数可以控制字段应该怎样建索引,怎样查询。它有以下三个可用值:
· no: 不把此字段添加到索引中,也就是不建索引,此字段不可查询
· not_analyzed:将字段的原始值放入索引中,作为一个独立的term,它是除string字段以外的所有字段的默认值。
· analyzed:string字段的默认值,会先进行分析后,再把分析的term结果存入索引中。
那么如果不需要的值,直接在mapping中设置为no就可以了。其它信息参考:store 和 _source field
PUT /my_index
{
"mappings": {
"my_type": {
"properties": {
"status_code": {
"type": "keyword",
"index": false
}
}
}
}
八,字段求和
GET /test_index/default/_search
{
"query" : {
"bool" : {
"must" : [
{
"script" : {
"script" : {
"inline" : "doc['monthSendAvg'].value + doc['monthRecvAvg'].value >= 113.599 && doc['monthSendAvg'].value + doc['monthRecvAvg'].value <= 113.6999",
"lang" : "painless"
},
"boost" : 1.0
}
}
],
"disable_coord" : false,
"adjust_pure_negative" : true,
"boost" : 1.0
}
}
}
九,批量导入数据
POST test_index/default/_bulk
{"index":{"_id":"39B12CA2977211EA8EA9E594507029C8"}}
{"prop1":"1","prop2":"a"}
{"index":{"_id":"EE9B0C5A977211EA8EA9E594507029C8"}}
{"prop1":"2","prop2":"B"}
十,按条件删除
# 下面是删除全部,按条件删除,需自己写条件
POST test_index/default/_delete_by_query
{
"query": {
"match_all": {}
}
}
十一,删除索引
DELETE test_index