创建索引
PUT course
{
"settings": {
"number_of_shards": 2,
"number_of_replicas": 1,
"analysis": {
"analyzer": {
"main_analyzer": {
"char_filter": [
"html_strip",
"abbr"
],
"tokenizer": "standard",
"stopwords": [
"a",
"an",
"the",
"的",
"乃",
"呢",
"么",
"呀",
"啊",
"嗯",
"哦"
],
"filter": [
"lowercase"
]
},
"pinyin_analyzer": {
"tokenizer": "pinyin_tokenizer"
}
},
"char_filter": {
"abbr": {
"type": "mapping",
"mappings": [
"Elasticsearch=>es",
"Golang=>go"
]
}
},
"tokenizer": {
"pinyin_tokenizer": {
"type": "pinyin",
"keep_separate_first_letter": true,
"keep_full_pinyin": true,
"keep_original": true,
"limit_first_letter_length": 16,
"lowercase": true
}
}
}
},
"mappings": {
"properties": {
"id": {
"type": "keyword",
"doc_values": false
},
"course_type": {
"type": "keyword",
"doc_values": false
},
"teacher_id": {
"type": "keyword",
"doc_values": false
},
"title": {
"type": "text",
"analyzer": "main_analyzer",
"search_analyzer": "main_analyzer",
"fields": {
"pinyin": {
"type": "text",
"analyzer": "pinyin_analyzer",
"search_analyzer": "pinyin_analyzer"
}
}
},
"tag": {
"type": "text",
"analyzer": "main_analyzer",
"search_analyzer": "main_analyzer",
"fields": {
"keyword": {
"type": "keyword",
"doc_values": false
}
}
},
"weight": {
"type": "short",
"doc_values": true
},
"desc": {
"type": "text",
"analyzer": "main_analyzer",
"search_analyzer": "main_analyzer"
},
"mtime": {
"type": "long",
"doc_values": true
},
"ctime": {
"type": "long",
"doc_values": false
}
}
}
}
创建
单条写
POST course/_doc/1
{
"id": 1,
"teacher_id":1,
"course_type":1,
"title": "Golang + Elasticsearch仿慕课网实现企业级搜索实战",
"tag": [
"go",
"es"
],
"weight": 10,
"desc": "go and es 实战课程",
"mtime": 1639728191,
"ctime": 1639728191
}
批量写bulk
POST course/_bulk
{"create":{"_index":"course","_id":2}}
{"id":2,"teacher_id":1,"course_type":2,"title":"go&es","tag":["java","es"],"weight":10,"desc":"go and es 实战课程","mtime":1639728191,"ctime":1639728191}
批量覆盖写
POST course/_bulk
{"index":{"_index":"course","_id":2}}
{"id":2,"teacher_id":1,"course_type":2,"title":"go&es","tag":["java","es"],"weight":10,"desc":"go and es 实战课程","mtime":1639728191,"ctime":1639728191}
修改
单条修改
#更新文档方式1,注意:此种方式必须传递文档的所有参数
POST course/_doc/1
{
"id": 1,
"teacher_id":1,
"course_type":1,
"title": "Golang + Elasticsearch仿慕课网实现企业级搜索实战",
"tag": [
"go",
"es"
],
"weight": 10,
"desc": "go and es 实战课程",
"mtime": 1639728191,
"ctime": 1639728191
}
#更新文档2,此种方式只需要传递待更新的字段
POST course/_update/1
{
"doc": {
"title": "Golang+Elasticsearch实战课程"
}
}
# update_by_query
POST course/_update_by_query
{
"script": {
"source": "ctx._source.title='通过_update_by_query修改'",
"lang": "painless"
},
"query": {
"term": {
"id": 1
}
}
}
通过bulk修改
POST _bulk
{ "update" : {"_id" : "1", "_index" : "course"} }
{ "doc" : {"title" : "通过bulk修改"} }
通过script方式修改
POST course/_update/1
{
"script": {
"source": "ctx._source.title=params.title",
"lang": "painless",
"params": {
"title": "通过script修改"
}
}
}
查询
直接通过文档id查询
查询单条结果
GET course/_doc/1
查询多条结果
GET /_mget
{
"docs": [
{
"_index": "course",
"_id": "1"
},
{
"_index": "course",
"_id": "2"
}
]
}
复合查询
GET course/_search
{
"from": 0,
"highlight": {
"fields": {
"desc": {},
"tag": {},
"title": {},
"title.pinyin": {
}
}
},
"query": {
"bool": {
"minimum_should_match": "1",
"must": {
"term": {
"course_type": 1
}
},
"should": [{
"match": {
"title": {
"boost": 10,
"operator": "and",
"query": "shizhan"
}
}
}, {
"match_phrase": {
"title.pinyin": {
"boost": 5,
"slop": 0,
"query": "shizhan"
}
}
}]
}
},
"size": 20
}