Rest风格是一种软件架构风格,而不是标准,只是提供了一组设计原则和约束条件。它主要用于客户端和服务器交互类的软件。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。
method | url地址 | 描述 |
---|---|---|
PUT | localhost:9200/索引名称/类型名称/文档id | 创建文档(指定文档id) |
POST | localhost:9200/索引名称/类型名称 | 创建文档(随机文档id) |
POST | localhost:9200/索引名称/类型名称/文档id/_update | 修改文档 |
DELETE | localhost:9200/索引名称/类型名称/文档id | 删除文档 |
GET | localhost:9200/索引名称/类型名称/文档id | 查询文档通过文档id |
POST | localhost:9200/索引名称/类型名称/_search | 查询所有数据 |
创建文档:
PUT /test1/type/1
{
"name":"京东",
"age":23
}
查询所有数据
POST /test1/type/_search
创建具体数据类型映射的索引
PUT /test2
{
"mappings": {
"properties": {
"name":{
"type": "text"
},
"age":{
"type": "long"
},
"birthday":{
"type": "date"
}
}
}
}
删除:
DELETE /test2
更新:
PUT方法会覆盖原有数据,若新数据为空同样覆盖,建议使用POST /…/_update不会覆盖原有数据
PUT /test2/_doc/1
{
"name":"狂神",
"age":23,
"birthday":"1997-12-01"
}
POST /test2/_doc/1/_update
{
"doc":{
"name":"法外狂徒"
}
}
注意:hits标签中包含所有查询出来的具体信息,无论是kibana中还是java api操作,都是对hits中标签的查询(注意:score可以用来判断谁更符合查询结果,从而进行排序等操作)
POST /test1/type/_search
GET /mengshen/user/_search
{
"query": {
"match": {
"name": "神"
}
}
}
GET /mengshen/user/_search
{
"query": {
"match": {
"name": "神"
}
},
"_source": ["name","desc"]
}
GET /mengshen/user/_search
{
"query": {
"match": {
"name": "神"
}
},
"sort": [
{
"age": {
"order": "asc"
}
}
],
"from": 0,
"size": 1
}
GET /mengshen/user/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "狂"
}
},
{
"match": {
"age": "23"
}
}
]
}
}
should相对于or
GET /mengshen/user/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"name": "狂"
}
},
{
"match": {
"age": "22"
}
}
]
}
}
}
must_not相对于not
GET /mengshen/user/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
"name": "狂"
}
}
]
}
}
}
GET /mengshen/user/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "神"
}
}
],
"filter": {
"range": {
"age": {
"gte": 20
}
}
}
}
}
}
GET /mengshen/user/_search
{
"query": {
"match": {
"tags": "男 无"
}
}
}
GET _analyze
{
"analyzer": "keyword",
"text":"狂神说"
}
GET _analyze
{
"analyzer": "standard",
"text":"狂神说"
}
GET /mengshen/user/_search
{
"query": {
"match": {
"name":"神"
}
},
"highlight": {
"pre_tags": ""
,
"post_tags": "",
"fields": {
"name":{
}
}
}
}
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.6.2</version>
</dependency>
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http")));
return client;