新建index
PUT /index_demo
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings": {
"properties": {
"name":{
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
修改索引
PUT /index_demo/_settings
{
"number_of_replicas": 1
}
删除索引
DELETE /index_demo
DELETE /index_1,index_2
DELETE /index_demo*
DELETE /_all
查询索引
GET /index_demo/_settings
GET /index_demo/_mapping
新增/修改document
GET /index_demo/_search
POST index_demo/_doc/001
{
"title": "好评不错",
"tag": "精彩",
"content": "这里必须有一些内容,来表示这个评论是好评",
"score": 90,
"time": 1596441469000
}
POST index_demo/_doc
{
"title": "好评不错",
"tag": "精彩",
"content": "这里必须有一些内容,来表示这个评论是好评",
"score": 90,
"time": 1596441469000
}
PUT index_demo/_doc/001
{
"name": "好评不错"
}
语法, 使用POST或者PUT都可以,存在则更新否则创建;
区别在于没有加ID值时(没有ID会自动生成),只能用POST表示创建;
需要注意的是使用PUT做更新时,其实是直接覆盖,因此需要带上所有的数据;
查询数据
# 查询所有
GET /索引名称/_search
# 根据ID查询
GET /索引名称/_doc/数据的id值
删除数据
#删除使用的逻辑删除,之后会统一进行物理删除
DELETE /索引名称/_doc/数据的id值
query string search
GET /index_demo/_search?q=title:好评&_source=title,tag,time&sort=score:desc&from=0&size=1
注意:
排序的字段必须是设置 fielddata=true,否则分词不能用于排序
query DSL
GET /index_demo/_search
{
"query": {
"bool": {
"must": [{
"match": {
"tag": "精彩"
}
}],
"filter": [{
"range": {
"score": {
"gt": 30
}
}
}]
}
},
"_source": ["name", "title","tag","content","score"],
"sort": [
{
"score": {
"order": "desc"
}
}
],
"from": 0,
"size": 20
}
注意
filter中的过滤字段不参与评分
多字段+高亮+标准分词
{
"query": {
"multi_match": {
"query": "精彩 好评 内容",
"fields": ["title", "content"],
"analyzer": "standard"
}
},
"_source": ["name", "title", "tag", "content", "score"],
"sort": [{
"score": {
"order": "desc"
}
}],
"highlight": {
"pre_tags": [
"<span style=color:green>"
],
"post_tags": [
"</span>"
],
"fields": {
"content": {},
"title": {}
}
},
"from": 0,
"size": 20
}