java es_Elasticsearch入门学习(四):使用javaAPI学习ES

一、Maven依赖

org.elasticsearch

elasticsearch

7.1.0

org.elasticsearch.client

elasticsearch-rest-high-level-client

7.1.0

二、开始之前的准备

/**

* 连接ES

* @return

*/

public RestHighLevelClient start() {

RestHighLevelClient restHighLevelClient = new RestHighLevelClient(

RestClient.builder(

new HttpHost("192.168.100.151", 9201, "http"),

new HttpHost("192.168.100.151", 9202, "http"),

new HttpHost("192.168.100.151", 9203, "http")));

return restHighLevelClient;

}

/**

* 操作所用到的实体类

*/

@Data

class Article{

private long id;

private String title;

public Article(long id, String title) {

this.id = id;

this.title = title;

}

}

三、关于索引的操作

新增索引

public void createIndex(RestHighLevelClient client) {

//索引名称

CreateIndexRequest request = new CreateIndexRequest("hello");

//分片副本

request.settings(Settings.builder().put("index.number_of_shards", 5)

.put("index.number_of_replicas", 1));

//内容

Map id = new HashMap <>();

id.put("type","text");

id.put("store",true);

Map title = new HashMap <>();

title.put("type","text");

title.put("store",true);

title.put("index",true);

title.put("analyzer", "standard");

Map properties = new HashMap <>();

properties.put("id",id);

properties.put("title",title);

Map mapping = new HashMap <>();

mapping.put("properties",properties);

request.mapping(mapping);

try {

CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);

System.out.println(response.toString());

} catch (IOException e) {

e.printStackTrace();

}

}

查询指定索引

public void getIndex(RestHighLevelClient client) throws IOException {

//索引名称

GetIndexRequest request = new GetIndexRequest("hello");

try {

boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);

System.out.println(exists);

} catch (IOException e) {

e.printStackTrace();

}

}

删除索引

public void delIndex(RestHighLevelClient client){

DeleteIndexRequest request = new DeleteIndexRequest("hello");

try {

AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);

System.out.println(delete.toString());

} catch (IOException e) {

e.printStackTrace();

}

}

四、关于文档的操作

创建文档

public void createDocument(RestHighLevelClient client){

//索引名称

IndexRequest indexRequest = new IndexRequest("hello");

ObjectMapper mapper = new ObjectMapper();

Article article = new Article(3L, "web前端");

byte[] json = new byte[0];

try {

json = mapper.writeValueAsBytes(article);

//可以设置文章ID

indexRequest.id("5");

indexRequest.source(json, XContentType.JSON);

IndexResponse index = client.index(indexRequest, RequestOptions.DEFAULT);

} catch (Exception e) {

e.printStackTrace();

}

}

根据文档ID查询文档

public void getDocument(RestHighLevelClient client){

GetRequest getRequest = new GetRequest("hello", "1");

GetResponse documentFields = null;

try {

documentFields = client.get(getRequest, RequestOptions.DEFAULT);

System.out.println(documentFields.toString());

} catch (IOException e) {

e.printStackTrace();

}

}

更新文档

public void updateDocument(RestHighLevelClient client){

UpdateRequest updateRequest = new UpdateRequest("hello", "1");

Article article = new Article(2L, "java入门到放弃");

ObjectMapper mapper = new ObjectMapper();

byte[] json = new byte[0];

try {

json = mapper.writeValueAsBytes(article);

IndexRequest indexRequest = new IndexRequest("hello");

indexRequest.source(json, XContentType.JSON);

updateRequest.doc(indexRequest);

UpdateResponse updateResponse = client.update(

updateRequest, RequestOptions.DEFAULT);

System.out.println(updateResponse);

} catch (Exception e) {

e.printStackTrace();

}

}

删除文档

public void delDocument(RestHighLevelClient client){

DeleteRequest request = new DeleteRequest("hello", "1");

try {

DeleteResponse deleteResponse = client.delete(

request, RequestOptions.DEFAULT);

System.out.println(deleteResponse);

} catch (IOException e) {

e.printStackTrace();

}

}

查询文档

public void searchDocument(RestHighLevelClient client){

SearchRequest searchRequest = new SearchRequest();

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

searchSourceBuilder.query(QueryBuilders.matchAllQuery());

searchRequest.source(searchSourceBuilder);

SearchResponse searchResponse = null;

try {

searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

System.out.println(searchResponse.toString());

} catch (IOException e) {

e.printStackTrace();

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值