Elasticsearch常用操作

新建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
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值