方法 / 步骤
一: 前置工作
1.1 导入数据
POST /staff/_bulk
{"index":{"_id":1}}
{"name":"zs","realname":"张三","age":10,"birthday":"2018-12-27","salary":1000.0,"address":"北京市北海公园"}
{"index":{"_id":2}}
{"name":"ls","realname":"李四","age":20,"birthday":"2017-10-20","salary":2000.0,"address":"北京市京东大峡谷"}
{"index":{"_id":3}}
{"name":"ww","realname":"王五","age":30,"birthday":"2016-03-15","salary":3000.0,"address":"北京市陶然亭"}
{"index":{"_id":4}}
{"name":"zl","realname":"赵六","age":40,"birthday":"2003-04-19","salary":4000.0,"address":"北京市玉渊潭"}
{"index":{"_id":5}}
{"name":"tq","realname":"田七","age":50,"birthday":"2001-08-11","salary":5000.0,"address":"北京市圆明园"}
1.2 常用DSL
# 查看集群的健康值:
GET _cat/health
# 测试分词器
POST /_analyze
{
"text":"工程师",
"analyzer":"ik_max_word"
}
# 查看某个索引属性
GET {index_name}
#查看指定索引文档总数
GET {index_name}/_count
#按照文档数量排序索引
GET _cat/indices?v&s=docs.count:desc
# 根据ID便捷查询索引文件
GET {index_name}/_doc/{index_id}
#创建索引库
PUT page
#删除索引
DELETE /page
二: 创建相关
2.1 创建索引,指定mapping(映射关系)
索引库一旦mapping创建后无法修改和删除,但是可以添加新的字段
为什么不能修改一个字段的type?原因是一个字段的类型修改以后,那么该字段的所有数据都需要重新索引。Elasticsearch底层使用的是lucene库,字段类型修改以后索引和搜索要涉及分词方式等操作。
PUT /store
{
"mappings": {
"properties": {
"id":{"type": "integer"},
"en_name":{"type": "keyword"},
"zn_name":{"type": "text"},
"creat_time":{"type": "date"},
"state_owned":{"type": "boolean"},
"address":{"type": "text","analyzer": "ik_max_word","search_analyzer": "ik_smart"},
"employees":{"type": "integer_range"}
}
}
}
2.2 新增Mapping映射
POST /{index_name}/_mapping
{
"properties": {
"phone":{
"type": "keyword"
}
}
}
三: 查询操作
3.1 query 查询
query 下面的参数有 match /
// match分词匹配,必须使用分词 text字段
GET {index_name}/_search
{
"query": {
"match": {
"name": "operator red sea"
}
}
}
// 查询某个具体值
GET {index_name}/_search
{
"query": {
"match": {
"actorList.name": "zhang han yu"
}
}
}
// 过滤查询
// term 值等匹配 必须用keyword类型字段
// term 中的词是不会被分词的,但es默认会将数据中的词进行分词所以除非是分词内的,否则term中查不到该分词
GET {index_name}/_search
{
"query": {
"term": {
"user.id": {
"value": "kimchy",
"boost": 1.0
}
}
}
}
// 短语匹配 类似于 like '%xxxx%'
GET {index_name}/_search
{
"query": {
"match_phrase": {
"actorList.name": "zhang han yu"
}
}
}
// fuzzy查询,模糊匹配(容错匹配)
GET {index_name}/_search
{
"query": {
"fuzzy": {
"name": "rad"
}
}
}
3.2 范围查询
// 范围过滤
GET {index_name}/_search
{
"query": {
"range": {
"doubanScore": {
"gte": 6,
"lte": 9
}
}
}
}
3.3 混合查询
// 混合查询方式 1、先匹配,再过滤 2、同时匹配加过滤
//1.先匹配,再过滤
GET {index_name}/_search
{
"query": {
"bool": {
"filter": [
{ "term": { "color": "red" }},
{ "term": { "brand": "gucci" }}
]
}
}
}
//或者
//1.先匹配,再过滤
GET {index_name}/_search
{
"query": {
"match": {
"name": "red"
}
},
"post_filter": {
"term": {
"actorList.id.keyword": "ygc"
}
}
}
//2.同时匹配加过滤
GET {index_name}/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "red"
}
}
],
"filter": {
"term": {
"actorList.id.keyword": "ygc"
}
}
}
}
}
3.4 分页查询
// 分页查询,类似于limit
// from = (pageNo-1)*size
// 指定的查询字段"_source":
// 高亮highlight
GET {index_name}/_search
{
"query": {
"match": {
"name": "red"
}
},
"highlight": {
"fields": {
"name": "name": {"pre_tags": "<span color='red'>","post_tags": "</span>"}
}
},
"_source": ["name","doubanScore"],
"from": 0,
"size": 20
}
四: 更新操作
- 覆盖方式更新(会把旧数据替换掉)
PUT / {index_name}/{index_id}?refresh=true
{
"properties":{
"age":{
"type":"integer",
"index": false
}
}
}
- 部分更新
# 如果数据不存在,会报错
POST {index_name}/_update/{index_id}?refresh=true
{
"doc": {
"name": "胡八一"
}
}
# 如果我们想要这样的效果:如果数据不存在,则作为新数据添加
POST {index_name}/_update/{index_id}?refresh=true
{
"doc": {
"name": "胡八一"
},
"doc_as_upsert": true
}
五: 删除操作
- 直接删除索引
DELETE index-name
DELETE entry_record
六: 聚合查询
- 聚合查询可以查看下面的文章
Elasticsearch 聚合查询一: Metric/指标聚合
Elasticsearch 聚合查询二: Bucketing/桶聚合
参考资料 & 致谢
[1] 官方文档