ElasticSearch-javaAPI-索引与文档操作
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了ElasticSearch-javaAPI-索引与文档操作,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3467字,纯文字阅读大概需要5分钟。
内容图文
1.引入elasticsearch与json依赖
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.4.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.4.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.4.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
2.编写ElasticSearchConfig
@Configuration
@ConfigurationProperties(prefix = "elasticsearch")
public class ElasticSearchConfig {
private String host;
private int port;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
@Bean
public RestHighLevelClient client(){
return new RestHighLevelClient(RestClient.builder(new HttpHost(host,port,"http")));
}
}
3.配置application.yml
elasticsearch:
host: 127.0.0.1
port: 9200
4.注入RestHighLevelClient ,添加索引与映射
@Autowired
private RestHighLevelClient client;
/**
* 创建索引与映射
* @throws IOException
*/
@Test
void addIndexAndMapping() throws IOException {
//获取操作索引的对象
IndicesClient indices = client.indices();
//设置索引名称与mapping
CreateIndexRequest createIndexRequest=new CreateIndexRequest("mmm");
String mapping="{\n" +
" \"properties\" : {\n" +
" \"address\" : {\n" +
" \"type\" : \"text\",\n" +
" \"fields\" : {\n" +
" \"keyword\" : {\n" +
" \"type\" : \"keyword\",\n" +
" \"ignore_above\" : 256\n" +
" }\n" +
" }\n" +
" },\n" +
" \"age\" : {\n" +
" \"type\" : \"integer\"\n" +
" },\n" +
" \"name\" : {\n" +
" \"type\" : \"keyword\"\n" +
" }\n" +
" }\n" +
" }";
createIndexRequest.mapping(mapping,XContentType.JSON);
//创建索引,获取返回值
CreateIndexResponse createIndexResponse = indices.create(createIndexRequest, RequestOptions.DEFAULT);
System.out.println(createIndexResponse);
}
5.删除索引
/**
* 删除索引
*/
@Test
void deleteIndex() throws IOException {
//获取索引操作对象
IndicesClient indices = client.indices();
//设置删除索引名称
DeleteIndexRequest deleteIndexRequest=new DeleteIndexRequest("myx");
//删除索引
AcknowledgedResponse delete = indices.delete(deleteIndexRequest, RequestOptions.DEFAULT);
System.out.println(delete);
}
6.判断索引是否存在
/**
* 判断索引是否存在
*/
@Test
void existIndex() throws IOException {
IndicesClient indices = client.indices();
GetIndexRequest getIndexRequest=new GetIndexRequest("mmm");
boolean exists = indices.exists(getIndexRequest, RequestOptions.DEFAULT);
System.out.println(exists);
}
7.添加或修改文档
/**
* 添加或修改文档
*/
@Test
void addDoc() throws IOException {
//数据源
/*Map data=new HashMap();
data.put("address","湖南长沙");
data.put("age",33);
data.put("name","张三");
IndexRequest indexRequest=new IndexRequest("mmm").id("1").source(data);*/
Person p=new Person();
p.setAddress("湖南株洲");
p.setId("2");
p.setAge(30);
p.setName("李四");
String data = JSON.toJSONString(p);
IndexRequest indexRequest=new IndexRequest("mmm").id(p.getId()).source(data,XContentType.JSON);
IndexResponse index = client.index(indexRequest, RequestOptions.DEFAULT);
System.out.println(index.getId());
}
8.查询文档
9.删除文档
原文:https://www.cnblogs.com/Pudge-FreshMeat/p/14197005.html
内容总结
以上是互联网集市为您收集整理的ElasticSearch-javaAPI-索引与文档操作全部内容,希望文章能够帮你解决ElasticSearch-javaAPI-索引与文档操作所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。