Restful:
GET请求:
GET index 查询索引信息
GET index/type/doc_id 查询指定的文档信息
例子:
1.GET /megacorp/employee/_search 搜索所有雇员
2.GET /megacorp/employee/_search?q=last_name:Smith 搜索姓氏为 Smith 的雇员
3.使用查询表达式搜索:DSL,使用Json构造一个请求:
GET /megacorp/employee/_search
{
"query" : {
"match" : {
"last_name" : "Smith"
}
}
}
4.更复杂的查询: 同样搜索姓氏为 Smith 的员工,但这次我们只需要年龄大于 30 的。查询需要稍作调整,使用过滤器 filter ,它支持高效地执行一个结构化查询。
GET /megacorp/employee/_search
{
"query" : {
"bool": {
"must": {
"match" : {
"last_name" : "smith"
}
},
"filter": {
"range" : {
"age" : { "gt" : 30 }
}
}
}
}
}
5.短语查询:
GET /megacorp/employee/_search
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
}
}
6.高亮搜索:
GET /megacorp/employee/_search
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
},
"highlight": {
"fields" : {
"about" : {}
}
}
}
POST请求:
POST index/type 创建文档
POST index/type/_search 查询文档
POST index/type/_update 修改文档
PUT请求:
PUT index 创建索引
PUT index/type/doc_id 创建文档
PUT index/type/_mappings 创建索引时,指定索引文档存储的属性信息
例子:
PUT /megacorp/employee/1
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
路径 /megacorp/employee/1 包含了三部分的信息:
megacorp ---- 索引名称
employee ---- 类型名称
1 ---- 特定雇员的ID
DELETE请求:
DELETE index 删除索引
DELETE index/type/doc_id 删除文档
ES中filed可以指定的类型:
1.string类型:
text:用于全文检索
keyword:当前field不会被分词。
2.数值类型:
long/integer/short/byte/double/float(32位)/half_float(16位)/scaled_float
3.时间类型:
date:(1)yyyy-MM-dd (2)yyyy-MM-dd HH:mm:ss (3)epoch_millis
4.布尔类型:
true/false
5.二进制类型:
binary类型暂时支持Base64 encodestring
6.范围类型:
long_range:gt/lt/gte/lte
float_range:
integer_range
double_range
date_range
ip_range
7.经纬度类型:
geo_point:用来存储经纬度
8.IP类型:
ip: IPV4/IPV6
索引操作:
PUT /book
{
"settings": {
"number_of_shards": 5, --索引分片数
"number_of_replicas": 1 --索引备份数
},
"mappings": { --指定数据结构
"novel":{ --声明类型名,现在不用指定类型名了
"properties":{ --field
"name":{
"type":"text",
"analyzer":"ik_max_word", --指定分词器
"index":true, --当前field可以作为查询的条件
"store":false --是否需要额外存储
},
"author":{
"type":"keyword"
},
"count":{
"type":"long"
},
"onsale":{
"type":"date",
"format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" --指定格式化方式
},
"desc":{
"type":"text",
"analyzer":"ik_max_word"
}
}
}
}
}
文档操作:
文档在ES服务中的唯一标志,_index, _type, _id 三个内容为组合,来锁定一个文档,操作是添加或是修改。
1.新建文档
自动生成id
POST /book/_doc
{
"name": "西游记",
"authoor": "刘明",
"onsale": "2020-12-11"
}
手动指定id:(推荐)
POST /book/_doc/1
{
"name": "三国演义",
"author": "小明",
"onsale": "2020-02-11",
"desc":"sdafewqfzxcvzxvxzcv"
}
2.修改文档:
覆盖式修改:未指定的field会清空
POST /book/_doc/1
{
...
}
基于doc(推荐)
POST /book/novel/1/_update
{
"doc": {
"name": "极品家丁"
}
}
#先锁定文档,_update 修改需要的字段即可
3.删除文档
DELETE /book/_doc/1
https://github.com/DW-Zhou/ES-6.5.4/blob/master/ElasticSearch%E7%9F%A5%E8%AF%86%E7%82%B9.md