码迷,mamicode.com
首页 > 编程语言 > 详细

ElasticSearch-javaAPI-索引与文档操作

时间:2020-12-31 12:57:55      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:ash   this   dma   top   创建索引   eve   返回值   yml   efault   

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.删除文档

ElasticSearch-javaAPI-索引与文档操作

标签:ash   this   dma   top   创建索引   eve   返回值   yml   efault   

原文地址:https://www.cnblogs.com/Pudge-FreshMeat/p/14197005.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!