ElasticSearch-javaAPI-索引与文档操作
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.查询文档
/**
* 查询文档
*/
@Test
void getDoc() throws IOException {
GetRequest getRequest=new GetRequest("mmm","1");
GetResponse response = client.get(getRequest, RequestOptions.DEFAULT);
System.out.println(response.getSourceAsString());
}
9.删除文档
/**
* 删除文档
*/
@Test
void deleteDoc() throws IOException {
DeleteRequest deleteRequest =new DeleteRequest("mmm","1");
DeleteResponse delete = client.delete(deleteRequest, RequestOptions.DEFAULT);
}