Elasticsearch Java Rest client 分成两部分:
官网地址
本文基于Elasticsearch 6.5.4版本操作
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.5.4</version>
</dependency>
防止某些api不兼容,导入对应的客户端版本
@Configuration
public class ElasticsearchClientConfig {
@Bean(autowire = Autowire.BY_NAME, name = "restHighLevelClient")
public RestHighLevelClient restClient() {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http")));
return client;
}
//main方法测试连接是否成功
public static void main(String[] args) throws IOException{
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http")));
Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put("user", "kimchy");
jsonMap.put("postDate", new Date());
jsonMap.put("message", "trying out Elasticsearch");
IndexRequest indexRequest = new IndexRequest("posts")
.index("test")
.type("test")
.id("1").source(jsonMap);
IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
client.close();
}
}
@Resource(name="restHighLevelClient")//通过指定对应的名称注入
private RestHighLevelClient highLevelClient;
RestHighLevelClient
提供了三种传入数据的格式操作对应的API,接下来我们基于创建索引展示
@Test
public void createIndex1() throws IOException {
String jsonString = "{" +
"\"user\":\"kimchy\"," +
"\"postDate\":\"2013-01-30\"," +
"\"message\":\"trying out Elasticsearch\"" +
"}";
IndexRequest indexRequest = new IndexRequest("posts")
.index("test")
.type("test")
.id("1").source(jsonString,XContentType.JSON);
IndexResponse indexResponse = highLevelClient.index(indexRequest, RequestOptions.DEFAULT);
}
@Test
public void createIndex() throws IOException {
Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put("user", "kimchy");
jsonMap.put("postDate", new Date());
jsonMap.put("message", "trying out Elasticsearch");
IndexRequest indexRequest = new IndexRequest("posts")
.index("test")
.type("test")
.id("1").source(jsonMap);
IndexResponse indexResponse = highLevelClient.index(indexRequest, RequestOptions.DEFAULT);
}
@Test
public void createIndex2() throws IOException {
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
{
builder.field("user", "kimchy");
builder.timeField("postDate", new Date());
builder.field("message", "trying out Elasticsearch");
}
builder.endObject();
IndexRequest indexRequest = new IndexRequest("posts")
.index("test")
.type("test")
.id("1").source(builder);
IndexResponse indexResponse = highLevelClient.index(indexRequest, RequestOptions.DEFAULT);//
}
@After
public void after() throws IOException {
highLevelClient.close();
}
以上方式我们可以根据自己的实际情况选择一种即可,更多详细信息可以参考官网提供的Api,非常详细
官网API
本文后面全部使用json字符串的方式操作
@Test
public void addTest() {
IndexRequest indexRequest = new IndexRequest("megacorp", "employee");
MegacorpBean megacorpBean = new MegacorpBean();
megacorpBean.setDesc("中国产小型无人机的“对手”来了,俄微型拦截导弹便宜量又多");
megacorpBean.setName("戴小小南");
megacorpBean.setDate(new Date());
megacorpBean.setContent("数据库管理");
String jsonStr = JSONObject.toJSONString(megacorpBean);
indexRequest.source(jsonStr, XContentType.JSON);
try {
highLevelClient.index(indexRequest,RequestOptions.DEFAULT);
} catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace();
}
}
MegacorpBean对象
@Data
public class MegacorpBean {
private String content;
private String desc;
private String message;
private String name;
private Date date;
}
@Test
public void updateDoc() {
UpdateRequest updateRequest = new UpdateRequest("megacorp", "employee", "1");
MegacorpBean megacorpBean = new MegacorpBean();
megacorpBean.setName("戴小二小南");
String jsonStr = JSONObject.toJSONString(megacorpBean);
updateRequest.doc(jsonStr, XContentType.JSON);
try {
highLevelClient.update(updateRequest,RequestOptions.DEFAULT);
} catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace();
}
}
@Test
public void matchQuery() throws Exception {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
SearchRequest searchRequest = new SearchRequest();
QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("desc", "数据库管理")
.fuzziness(Fuzziness.AUTO)
.prefixLength(3)
.maxExpansions(10);
searchSourceBuilder.query(matchQueryBuilder);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = highLevelClient.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(searchResponse);
}
本文主要介绍基于Spring Boot使用Java High Level REST Client
,支持的三种数据格式,以及展示一些基本的api相关操作,更多api 操作还是要读者自己去查阅官方提供的api文档。