1.创建非结构化索引
put /haoke
{
"settings":{
"index":{
"number_of_shards":"2",
"number_of_replicas":"0"
}
}
}
2.删除索引
delete/haoke
3.mapping创建结构化索引
put /itcast
{
"settings":{
"index":{
"number_of_shards":"1",
"number_of_replicas":"0"
}
},
"mapping":{
"person":{
"properties":{
"name":{
"type":"text"},
"age":{
"type":"integer"},
"mail":{
"type":"keyword"},
"hobby":{
"type":"text",
"analyzer":"ik_max_word"}
}
}
}
}
查看索引
get/itcast/_mapping
4.组合搜索
post/itcast/person/_search
{
"query":{
"bool":{
"must":{
"match":{
"hobby":"篮球"
}
},
"must_not":{
"match":{
"hobby":"音乐"}
},
"should":{
"match":{
"hobby":"游泳"
}
}
}
},
"highlight":{
"fields":{
"hobby":{}
}
}
}
5.向索引库中插入数据
唯一标识(/haoke/user/1001),插入数据
post/haoke/user/1001
{
"id":1001,
"name":"张三",
"age":20,
"sex":"男"
}
6.更新数据
///
POST: 请求服务器接受所指定的文档作为对所标识的URI的新的从属实体。
PUT: 从客户端向服务器传送的数据取代指定的文档的内容。
对资源的增,删,改,查操作,其实都可以通过GET/POST完成,不需要用到PUT和DELETE
///
通过覆盖的方式更新
全局覆盖
put/haoke/user/1001
{
"id":1001,
"name":"张三",
"age":21,
"sex":"女"
}
局部覆盖
post/haoke/user/1001/_update
{
"doc":{
"age":23
}
}
6.删除数据
delete/haoke/user/1001
7.搜索数据
1)通过唯一标识搜索数据
get/haoke/user/1001
2)搜索类型中的全部数据
get/haoke/user/_search
3)通过关键字搜索数据,age=20
get/haoke/user/_search?q=age:20
8.DSL搜索数据
post/haoke/user/_search
{
"query":{
"bool":{
"filter":{
"range":{
"age":{
"gt":3o
}
}
},
"must":{
"match":{
"sex":"男"
}
}
}
}
}
9.高亮显示
{
"query":{
"match":{
"name":"张三 李四"}
},
"highlight":{
"fields":{
"name":{}
}
}
}
10.聚合操作
post/haoke/user/_search
{
"aggs":{
"all_interests":{
"terms":{
"field":"age"}
}
}
}
11.查询相应
get/haoke/user/1005?_source=id,name
12.判断当前状态文档是否存在
head/haoke/user/1005
存在:200ok
不存在:404notfound
13.批量操作 _mget/_bulk
1)_mget
post/haoke/user/_mget
{
"ids":["1325","1345"] ##输入多个唯一标识
}
2)_bulk
批量的插入修改删除操作都是通过_bulk的api完成的
post/haoke/user/_bulk
#批量插入数据,第一行是操作行,第二行是请求体
{"create":{"index":"haoke","_type":"user","id":2001}}
{"id":2001,"name":"name1","age":20,"sex":"男"}
{"create":{"index":"haoke","_type":"user","id":2002}}
{"id":2002,"name":"name2","age":30,"sex":"男"}
{"create":{"index":"haoke","_type":"user","id":2003}}
{"id":2003,"name":"name3","age":40,"sex":"女"}
#以换行符结尾
14.分页
搜索时默认只获取10条数据,要获取能多的数据,需要用分页
size:结果数,默认返回10
from:跳过开始的结果数,默认0
例子:每页返回5个数据,页码从1到3
get/_search?size=5
get/_search?size=5&from=5
get/_search?size=5&from=10
15.结构化查询
1)term查询:主要用于精确匹配,比如数字,日期,布尔值或可分词的字符串。
post/itcast/person/_search
{
"query":{
"term":{
"age":20
}
}
}
post/itcast/person/_search
{
"query":{
"term":{
"date":"2013-12-12"
}
}
}
2)terms查询
terms相对于term允许指定多个匹配条件。
{
"query":{
"term":{
"age":[20,21,22]
}
}
}
3)range 范围查询
**range过滤允许我们按照指定范围查找一批数据**:
gt大于,lt小于,gte大于等于,lte小于等于
大于等于20小于30
filter 查出价格高于5000
{
"query":{
"bool":{
"should":[
{
"match":{
"category":"小米"
}
},
{
"match":{
"category":"华为"
}
}
,
"filter":{
"range":{
"price":{
"gt":5000
}
}
}
}
}
}
4)exists查询
查询所有文档中,某个字段有值的数据,类似与sql中的is_null
{
"exists":{
"field":"title"
}
}
5)match查询
无论是分词还是不分词,都可以用match查询
{
"match":{
"tweet":"abour search"}
}
6)bool查询
用来合并多个条件查询结果的布尔逻辑
must:多个查询条件的完全匹配,相当于and
must_not:多个查询条件的相反匹配,相当于not
should:至少有一个查询条件匹配,相当于or
{
"bool":{
"must":{"term":{"folder":"inbox"}},
"must_not":{"term":{"tag":"spam"}},
"should":[
{"term":{"starred":true}},
{"term":{"unread":true}}
]
}
}
elastic search小白学习代码(1)
最新推荐文章于 2024-06-13 14:26:00 发布