javaAPI操作Elasticsearch_elasticsearch 修改字段 java api

    "type": "keyword",
    "index": false
  },
  "price": {
    "type": "integer"
  },
  "score": {
    "type": "integer"
  },
  "brand": {
    "type": "keyword",
    "copy\_to": "all"
  },
  "city": {
    "type": "keyword"
  },
  "starName": {
    "type": "keyword"
  },
  "business": {
    "type": "keyword",
    "copy\_to": "all"
  },
  "location": {
    "type": "geo\_point"
  },
  "pic": {
    "type": "keyword",
    "index": false
  },
  "all": {
    "type": "text",
    "analyzer": "ik\_max\_word"
  }
}

}
}


基于elasticsearch的规则, id用keyword


* 操作索引库



import com.zyw.elasticsearchdemo.constants.HotelConstants;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;

public class ElasticsearchDemoApplicationTests {

private RestHighLevelClient client;


/\*\*

* 删除索引库
*/
@Test
void deleteHotelIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest(“hotel”);
client.indices().delete(request, RequestOptions.DEFAULT);
}

/\*\*

* 判断索引库是否存在
*/
@Test
void existHotelIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest(“hotel”);
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists ? “索引库已经存在” : “索引库不存在”);
}

/\*\*

* 创建索引库
*/
@Test
void createHotelIndex() throws IOException {
// 1.创建request对象
CreateIndexRequest request = new CreateIndexRequest(“hotel”);
// 2.准备请求的参数, DSL语句
request.source(HotelConstants.MAPPING_TEMPLATE, XContentType.JSON);
// 3. 发送请求
client.indices().create(request, RequestOptions.DEFAULT);
}

@BeforeEach
void setUp() {
    this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://82.114.174.50:9200")));
}

@AfterEach
void tearDown() throws IOException {
    client.close();
}

}


## RestClient操作文档




---



import cn.hutool.json.JSONUtil;
import com.zyw.elasticsearchdemo.mapper.HotelMapper;
import com.zyw.elasticsearchdemo.pojo.Hotel;
import com.zyw.elasticsearchdemo.pojo.HotelDoc;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType

  • 16
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值