# 索引员工(放一个员工数据到ES中),id=1
PUT /megacorp/employee/1
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
# 同上
PUT /megacorp/employee/2
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 35,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
# 检索id为1的人
GET /megacorp/employee/1
# 检索文档,能查出全部
GET /megacorp/employee/_search
# 查姓氏为smith的人,大小写不敏感
GET /megacorp/employee/_search?q=last_name:smith
# 使用查询表达式搜索,等同于上句的效果
GET /megacorp/employee/_search
{
"query" : {
"match" : {
"last_name" : "Smith"
}
}
}
#条件查询,查姓氏为smith,并且年龄大于30的
GET /megacorp/employee/_search?pretty
{
"query" : {
"bool": {
"must": {
"match" : {
"last_name" : "smith"
}
},
"filter": {
"range" : {
"age" : { "gt" : 30 }
}
}
}
}
}
#精确匹配,仅匹配同时包含 “rock” 和 “climbing”,并且二者以短语 “rock climbing” 的形式紧挨着的
GET /megacorp/employee/_search
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
}
}
# 高亮搜索
GET /megacorp/employee/_search?pretty
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
},
"highlight": {
"fields" : {
"about" : {}
}
}
}
操作映射
#创建索引 --> 数据库
PUT person
#查询索引
GET person
#添加映射
PUT /person/_mapping
{
"properties":{
"name":{
"type":"text"
},
"age":{
"type":"integer"
}
}
}
创建索引并添加映射
#创建索引并添加映射(推荐)
PUT /person1
{
"mappings": {
"properties": {
"name": {
"type": "text"
},
"age": {
"type": "integer"
}
}
}
}
#查询映射
GET person1/_mapping
添加字段
#添加字段
PUT /person1/_mapping
{
"properties": {
"name": {
"type": "text"
},
"age": {
"type": "integer"
}
}
}
操作文档
•添加文档,指定id
POST /person/_doc/1
{
"name":"张三",
"age":18,
"address":"北京"
}
GET /person/_doc/1
•添加文档,不指定id("_version" : 会变,但是数据量不会变)
#添加文档,不指定id
POST /person1/_doc/
{
"name":"张三",
"age":18,
"address":"北京"
}
#查询所有文档
GET /person/_search
#删除指定id文档
DELETE /person/_doc/1