1.將Centos7集羣都啓動 html
2.將Centos的插件打開前端
head插件是一個用來瀏覽、與ES數據進行交互的web前端展現插件,是一個用來監視ES狀態的客戶端插件。java
3.添加日誌web
添加log4j2.xml https://www.cnblogs.com/JBLi/p/11403956.htmlapache
4.添加依賴json
<dependencies> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>5.4.3</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>5.4.3</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.25.Final</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
代碼以下服務器
package com.bw.es; import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.get.MultiGetItemResponse; import org.elasticsearch.action.get.MultiGetRequest; import org.elasticsearch.action.get.MultiGetResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.client.Requests; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.transport.client.PreBuiltTransportClient; import org.junit.Before; import org.junit.Test; import java.io.IOException; import java.net.InetAddress; import java.util.HashMap; import java.util.Iterator; public class TestES { private TransportClient client; @Before public void getClient() throws Exception { // 1 設置鏈接的集羣名稱 Settings settings = Settings.builder().put("cluster.name", "my-cluster").build(); // 2 鏈接集羣 client = new PreBuiltTransportClient(settings); client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.137.10"), 9300)); // 3 打印集羣名稱 System.out.println(client.toString()); // client.close(); } //建立索引 @Test public void createIndex_blog(){ // 1 建立索引 client.admin().indices().prepareCreate("blog4").get(); // 2 關閉鏈接 client.close(); } //刪除索引 @Test public void deleteIndex() { //刪除索引 client.admin().indices().prepareDelete("hadoop2").get(); //關閉資源 client.close(); } //建立文檔以json形式 @Test public void createIndexbyJson(){ //建立文檔的內容 String json = "{" + "\"id\":\"1\"," + "\"title\":\"基於Lucene的搜索服務器\"," + "\"content\":\"它提供了一個分佈式多用戶能力的全文搜索引擎,基於RESTful web接口\"" + "}"; //建立 IndexResponse re = client.prepareIndex("blog", "article", "1").setSource(json).execute().actionGet(); //打印返回值 System.out.println("索引="+re.getIndex()); System.out.println("類型="+re.getType()); System.out.println("id="+re.getId()); System.out.println("版本號="+re.getVersion()); System.out.println("結果="+re.getResult()); client.close(); } //建立文檔以hashmap @Test public void createIndexByMap(){ HashMap<String, Object> json = new HashMap<String, Object>(); json.put("id","2"); json.put("title","建總"); json.put("content","看片。拍片"); IndexResponse re = client.prepareIndex("blog", "article", "2").setSource(json).execute().actionGet(); //打印返回值 System.out.println("索引="+re.getIndex()); System.out.println("類型="+re.getType()); System.out.println("id="+re.getId()); System.out.println("版本號="+re.getVersion()); System.out.println("結果="+re.getResult()); client.close(); } //建立文檔以builder @Test public void createIndexByBuilder() throws IOException{ XContentBuilder b= XContentFactory.jsonBuilder() .startObject() .field("id", "4") .field("title", "偉哥") .field("content", "買藥吃藥") .endObject(); IndexResponse re = client.prepareIndex("blog", "article", "3").setSource(b).execute().actionGet(); //打印返回值 System.out.println("索引="+re.getIndex()); System.out.println("類型="+re.getType()); System.out.println("id="+re.getId()); System.out.println("版本號="+re.getVersion()); System.out.println("結果="+re.getResult()); client.close(); } //單個索引查詢 @Test public void queryIndex(){ //查詢 GetResponse re = client.prepareGet("blog", "article", "3").get(); //打印 System.out.println(re.getSourceAsString()); //關閉資源 client.close(); } //多個索引查詢 @Test public void queryMultiIndex(){ //查詢 MultiGetResponse re = client.prepareMultiGet().add("blog", "article", "3") .add("blog", "article", "2", "3") .add("blog", "article", "1").get(); for(MultiGetItemResponse mu:re){ GetResponse re1 = mu.getResponse(); //判斷是否存在 if(re1.isExists()){ System.out.println(re1.getSourceAsString()); } } } //更新文檔數據update @Test public void update() throws Exception{ UpdateRequest up = new UpdateRequest("blog", "article", "2"); up.doc(XContentFactory.jsonBuilder().startObject() .field("id","2") .field("title","建總你好") .field("content","看片 拍片 西片") .endObject()); UpdateResponse re = client.update(up).get(); //打印返回值 System.out.println("索引="+re.getIndex()); System.out.println("類型="+re.getType()); System.out.println("id="+re.getId()); System.out.println("版本號="+re.getVersion()); System.out.println("結果="+re.getResult()); client.close(); } //更新文檔數據upsert 沒有建立 有就更新 @Test public void upsert() throws Exception{ //沒有這個文檔內容就建立 IndexRequest index = new IndexRequest("blog", "article", "5"); IndexRequest re1 = index.source(XContentFactory.jsonBuilder().startObject() .field("id", 5) .field("titile", "我是最棒的") .field("content", "我是一隻魚 ,水裏的空氣") .endObject()); //有文檔內容就更新 UpdateRequest updateRequest = new UpdateRequest("blog", "article", "5"); updateRequest.doc(XContentFactory .jsonBuilder() .startObject() .field("id","5") .field("title","奮鬥是惟一的辦法") .field("content","加油加油加油加油") .endObject()); updateRequest.upsert(re1); //具體更新操做 UpdateResponse re= client.update(updateRequest).get(); //打印返回值 System.out.println("索引="+re.getIndex()); System.out.println("類型="+re.getType()); System.out.println("id="+re.getId()); System.out.println("版本號="+re.getVersion()); System.out.println("結果="+re.getResult()); client.close(); } //刪除文檔 @Test public void delete(){ client.prepareDelete("blog","article","5").get(); client.close(); } //查詢 @Test public void querymatchall(){ //查詢 SearchResponse response = client.prepareSearch("blog").setTypes("article").setQuery(QueryBuilders.matchAllQuery()).get(); //獲取查詢對象(文檔) SearchHits hits = response.getHits(); //打印查詢結果條目 System.out.println("查詢結果="+hits.getTotalHits()); //遍歷打印文檔內容 Iterator<SearchHit> iterator = hits.iterator(); while (iterator.hasNext()){ SearchHit next = iterator.next(); System.out.println(next.getSourceAsString()); } //關閉資源 client.close(); } //分詞查詢 @Test public void query(){ //查詢 SearchResponse response = client .prepareSearch("blog") .setTypes("article") .setQuery(QueryBuilders.queryStringQuery("看片")) .get(); //獲取查詢對象(文檔) SearchHits hits = response.getHits(); //打印查詢結果條目 System.out.println("查詢結果="+hits.getTotalHits()); //遍歷打印文檔內容 Iterator<SearchHit> iterator = hits.iterator(); while (iterator.hasNext()){ SearchHit next = iterator.next(); System.out.println(next.getSourceAsString()); } //關閉資源 client.close(); } //詞條查詢 @Test public void termQuery(){ //查詢 SearchResponse response = client .prepareSearch("blog") .setTypes("article") .setQuery(QueryBuilders.termQuery("content","看")) .get(); //獲取查詢對象(文檔) SearchHits hits = response.getHits(); //打印查詢結果條目 System.out.println("查詢結果="+hits.getTotalHits()); //遍歷打印文檔內容 Iterator<SearchHit> iterator = hits.iterator(); while (iterator.hasNext()){ SearchHit next = iterator.next(); System.out.println(next.getSourceAsString()); } //關閉資源 client.close(); } //通配符查詢 * 表示多個字符(任意字符) ? 表示單個字符 @Test public void wildcardQuery(){ //查詢 SearchResponse response = client .prepareSearch("blog") .setTypes("article") .setQuery(QueryBuilders.wildcardQuery("content","*看片*")) .get(); //獲取查詢對象(文檔) SearchHits hits = response.getHits(); //打印查詢結果條目 System.out.println("查詢結果="+hits.getTotalHits()); //遍歷打印文檔內容 Iterator<SearchHit> iterator = hits.iterator(); while (iterator.hasNext()){ SearchHit next = iterator.next(); System.out.println(next.getSourceAsString()); } //關閉資源 client.close(); } //模糊 @Test public void fuzzy(){ //查詢 SearchResponse response = client .prepareSearch("blog") .setTypes("article") .setQuery(QueryBuilders.fuzzyQuery("title","licene")) .get(); //獲取查詢對象(文檔) SearchHits hits = response.getHits(); //打印查詢結果條目 System.out.println("查詢結果="+hits.getTotalHits()); //遍歷打印文檔內容 Iterator<SearchHit> iterator = hits.iterator(); while (iterator.hasNext()){ SearchHit next = iterator.next(); System.out.println(next.getSourceAsString()); } //關閉資源 client.close(); } //映射相關操做 新建立一個index 沒有mapping映射才能運行 @Test public void createMapping() throws Exception { // 1設置mapping XContentBuilder builder = XContentFactory.jsonBuilder() .startObject() .startObject("article") .startObject("properties") .startObject("id1") .field("type", "string") .field("store", "yes") .endObject() .startObject("title2") .field("type", "string") .field("store", "no") .endObject() .startObject("content") .field("type", "string") .field("store", "yes") .endObject() .endObject() .endObject() .endObject(); // 2 添加mapping PutMappingRequest mapping = Requests.putMappingRequest("blog4").type("article").source(builder); client.admin().indices().putMapping(mapping).get(); // 3 關閉資源 client.close(); } }