ES搜索引擎 RESTful API 相关操作

在Elasticsearch中,提供了功能丰富的RESTful API的操作,包括基本的CRUD、创建索引、删除索引等操作

1.创建非结构化索引

PUT http://{IP地址}:{端口}/{索引名}
{
"settings": {
    "index": {
     	#分片数
        "number_of_shards": "2",
         #副本数
        "number_of_replicas": "0"
   			 }
		  }
}

2.#删除索引

DELETE http://{IP地址}:{端口}/{索引名}
{ "acknowledged": true }

3.插入数据

POST http://172.16.55.185:9200/{索引}/{类型}/{_id}
#数据 
{
    "id": 1001,
    "name": "张三",
    "age": 20,
    "sex": "男"
}

4. 更新数据

PUT http://172.16.55.185:9200/{索引}/{类型}/{_id}
#数据 
{
    "id": 1001,
    "name": "张三",
    "age": 20,
    "sex": "男"
}

// 局部更新 (注:内部实现 :1. 从旧文档中检索JSON 2. 修改它 3. 删除旧文档 4. 索引新文档)
POST http://172.16.55.185:9200/haoke/user/1001/_update
{
    "doc": {
        "age": 23
    }
}

5. 删除数据

http://192.168.80.128:9200/haoke/user/{_id}

6.搜索数据

根据id搜索数据
http://192.168.80.128:9200/haoke/user/{_id}
搜索全部数据 (默认:10条数据)
http://192.168.80.128:9200/haoke/user/_search
等于查询
http://192.168.80.128:9200/test/user/_search?q=age:20

DSL搜索

	Elasticsearch提供丰富且灵活的查询语言叫做DSL查询(Query DSL),它允许你构建更加复杂、强大的查询
	DSL(Domain Specific Language特定领域语言)以JSON请求体的形式出现。

实现:查询年龄大于20岁的男性用户。

http://192.168.80.128:9200/haoke/user/_search
{
    "query": {  // 说明查询
        "bool":{ 
            "must":{  // 配置
                "match":{
                    "sex":"男"
                }
            },
            "filter":{ // 过滤
                "range":{
                    "age":{
                        "gt":20
                    }
                }
            }
        }
    }
}

查询响应设置

pretty 格式化数据

GET http://172.16.55.185:9200/test/user/1005?pretty

指定响应字段

GET http://172.16.55.185:9200/test/user/1005?_source=id,name

判断文档是否存在

HEAD http://172.16.55.185:9200/test/user/1005

批量操作

批量查询: 
POST http://172.16.55.185:9200/test/user/_mget

 { "ids" : [ "1001", "1003" ] }
 

_bulk操作
在Elasticsearch中,支持批量的插入、修改、删除操作,都是通过_bulk的api完成的。

{ action: { metadata }}\n 
{ request body }\n 
{ action: { metadata }}\n 
{ request body }\n

批量插入数据:
{"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": 20,"sex": "男"} 
{"create":{"_index":"haoke","_type":"user","_id":2003}} 
{"id":2003,"name":"name3","age": 20,"sex": "男"}
注意最后一行的回车


批量删除:
{"delete":{"_index":"haoke","_type":"user","_id":2001}} 
{"delete":{"_index":"haoke","_type":"user","_id":2002}}
{"delete":{"_index":"haoke","_type":"user","_id":2003}}

分页

size: 结果数,默认10 
from: 跳过开始的结果数,默认0
		注 ;返回数据控制在 1000个依赖
		
GET http://172.16.55.185:9200/haoke/user/_search?size=1&from=2

结构化查询

term查询

term 主要用于精确匹配哪些值,比如数字,日期,布尔值或 not_analyzed 的字符串(未经分析的文本数据类型):
示例:

POST http://172.16.55.185:9200/itcast/person/_search 

{ "query" : { "term" : { "age" : 20 } } }

terms查询

terms 跟 term 有点类似,但 terms 允许指定多个匹配条件。 如果某个字段指定了多个值,那么文档需要一起去做匹配:

实例:

POST http://172.16.55.185:9200/itcast/person/_search 

{ "query" : { "terms" : { "age" : [20,21] } } }

range查询

range 过滤允许我们按照指定范围查找一批数据:

范围操作符包含:
gt :: 大于
gte :: 大于等于
lt :: 小于
lte :: 小于等于
示例:
POST http://172.16.55.185:9200/itcast/person/_search

 { "query": { "range": { "age": { "gte": 20, "lte": 22 } } } }

exists 查询

exists 查询可以用于查找文档中是否包含指定字段或没有某个字段,类似于SQL语句中的 IS_NULL 条件	
示例:
POST http://172.16.55.185:9200/haoke/user/_search 
{ "query": { "exists": { #必须包含 "field": "card" } } }

match查询

match 查询是一个标准查询,不管你需要全文本查询还是精确查询基本上都要用到它

bool查询

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 }} ] } }

过滤查询

前面讲过结构化查询,Elasticsearch也支持过滤查询,如term、range、match等。	
示例:查询年龄为20岁的用户。
POST http://172.16.55.185:9200/itcast/person/_search
{ "query": { "bool": { "filter": { "term": { "age": 20 } } } } }

分词

内置分词

Standard 标准分词,按单词切分,并且会转化成小写
	POST http://172.16.55.185:9200/_analyze 
	{ "analyzer": "standard", "text": "A man becomes learned by asking questions." }
Simple分词器,按照非单词切分,并且做小写处理
	POST http://172.16.55.185:9200/_analyze 
	{ "analyzer": "simple", "text": "If the document doesn't already exist" }
	```

Whitespace是按照空格切分。
POST http://172.16.55.185:9200/_analyze
{ "analyzer": "whitespace", "text": "If the document doesn't already exist" }


Stop分词器,是去除Stop Word语气助词,如the、an等。
POST http://172.16.55.185:9200/_analyze 
{ "analyzer": "stop", "text": "If the document doesn't already exist" }
	Keyword分词器,意思是传入就是关键词,不做分词处理。
	POST http://172.16.55.185:9200/_analyze { "analyzer": "keyword", "text": "If the document doesn't already exist" }

中文分词

参考地址:https://github.com/medcl/elasticsearch-analysis-ik

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值