最近想的比較多, 小說項目大部分功能都已經完成了。可是仍是有不少的細節要完善:node
想一想仍是頭大的 !·_·!json
今天是要介紹經過Java API來如何操做ES。 首先介紹環境,公司電腦上安裝的ES版本是5.5.2bash
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.5.2</version>
</dependency>
複製代碼
這裏version須要和本身電腦上安裝的ES版本一致,否則會出現兼容性問題app
既然須要操做ES, 固然須要先連接上ES啊elasticsearch
@Configuration
public class ESConfig {
@Bean
public TransportClient client() throws UnknownHostException {
//節點信息, 能夠添加多個
InetSocketTransportAddress node = new InetSocketTransportAddress(InetAddress.getByName("192.168.87.134"), 9300);
TransportClient client = new PreBuiltTransportClient(getSetting());
client.addTransportAddress(node);
return client;
}
private Settings getSetting() {
return Settings.builder()
.put("cluster.name", "es_books")
.build();
}
}
複製代碼
@Configuration意思是告訴Spring 這個類是一個配置類, 你們就把這個類理解成xml就能夠了ui
// 這裏的ID是ES生成的ID
GetResponse fields = client.prepareGet("books", "books", "1").get();
//getSource() 會獲取到數據
System.out.println(fields.getSource());
複製代碼
在實際操做中, 若是咱們在新安裝的ES中進行這樣的操做, 會出現 索引不存在 的問題this
[books] IndexNotFoundException[no such index]
複製代碼
IndicesExistsRequest existsRequest = new IndicesExistsRequest("books");
IndicesExistsResponse response = client.admin().indices().exists(existsRequest).actionGet();
//存在 true, 不存在 false
System.out.println(response.isExists());
複製代碼
建立名稱爲
booksadds
, 類型爲booksadd
5個分片1個副片 類型爲空spa
String type = "{" +
"\"booksadd\": {" +
"}" +
"}";
client.admin().indices().prepareCreate("booksadds")
.setSettings(Settings.builder().put("index.number_of_shards", 5).put("index.number_of_replicas", 1))
.addMapping("booksadd", type, XContentType.JSON).get();
複製代碼
特殊說明: 若是須要設置類型, 參考紅線部分日誌
XContentBuilder contentBuilder = XContentFactory.jsonBuilder()
.startObject()
.field("id", 1)
.field("booksName", "九星霸體訣")
.field("author", "123")
.endObject();
IndexResponse response = client.prepareIndex("booksadds", "booksadd", "1")
.setSource(contentBuilder)
.get();
System.out.println(response);
複製代碼
經過查看
setSource
方法, 能夠看到內部有不少方法,你們能夠不用侷限於這一種。code
XContentBuilder contentBuilder = XContentFactory.jsonBuilder()
.startObject()
.field("id", 2)
.field("booksName", "九星霸體訣")
.field("author", "123")
.endObject();
IndexRequestBuilder requestBuilder = client.prepareIndex("booksadds", "booksadd", "2")
.setSource(contentBuilder);
//批量加入
BulkRequestBuilder prepareBulk = client.prepareBulk();
prepareBulk.add(requestBuilder);
prepareBulk.execute().get();
複製代碼
UpdateRequest request = new UpdateRequest("booksadds", "booksadd", "2");
XContentBuilder contentBuilder = XContentFactory.jsonBuilder()
.startObject()
.field("booksName", "九星霸體訣11111")
.endObject();
request.doc(contentBuilder);
UpdateResponse response = client.update(request).get();
System.out.println(response);
複製代碼
一樣
request.doc()
方法也並不侷限於這一種方式
DeleteResponse requestBuilder = this.client.prepareDelete("booksadds", "booksadd", "2").get();
System.out.println(requestBuilder.getResult());
複製代碼
client.admin().indices().prepareDelete("booksadds").execute().get();
複製代碼
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//匹配booksName查詢
boolQueryBuilder.must(QueryBuilders.matchQuery("booksName", "九星"));
SearchResponse searchResponse = client.prepareSearch("books", "books")
.setQuery(boolQueryBuilder)
.addSort("id", SortOrder.DESC) //排序
.setFrom(0) //分頁
.setSize(10)
.execute()
.actionGet();
System.out.println(searchResponse.getHits());
複製代碼
到這裏 經過API操做ES完成, 算是一個拋磚引玉的過程, 期待你們更多的操做。