• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
毛逢芳
博客园    首页    新随笔    联系   管理    订阅  订阅

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);
}
posted @ 2020-12-27 13:31  那棵tree看起来生气了  阅读(190)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3