原生elasticsearch測試用例編寫
阿新 • • 發佈:2018-12-01
1.座標
<dependencies>
<dependency>
<groupId>org.</groupId>
<artifactId>elasticsearch</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId >junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.1</version>
</dependency >
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId >
<artifactId>jackson-annotations</artifactId>
<version>2.8.1</version>
</dependency>
</dependencies>
2.實體類編寫
package cn.itcast.elasticsearch.domain;
public class Article {
private Integer id;
private String title;
private String content;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "Article [id=" + id + ", title=" + title + ", content="
+ content + "]";
}
}
3.編寫測試類
package cn.itcast.elasticsearch.test;
import java.io.IOException;
import java.net.InetAddress;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.highlight.HighlightField;
import org.junit.Test;
import cn.itcast.elasticsearch.domain.Article;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.net.InetAddresses;
// ElasticSearch 測試程式
public class ElasticSearchTest {
// 直接在ElasticSearch中建立文件,自動建立索引
@Test
public void demo1() throws IOException {
// 建立連線搜尋伺服器物件
Client client = TransportClient
.builder()
.build()
.addTransportAddress(
new InetSocketTransportAddress(InetAddress.getByName("192.168.38.113"), 9300));
// 描述json 資料
/*
* {id:xxx, title:xxx, content:xxx}
*/
XContentBuilder builder = XContentFactory
.jsonBuilder()
.startObject()
.field("id66", 6)
.field("title", "ElasticSearch是一個基於Lucene的搜尋伺服器")
.field("content",
"它提供了一個分散式多使用者能力的全文搜尋引擎,基於RESTful web介面。Elasticsearch是用Java開發的,並作為Apache許可條款下的開放原始碼釋出,是當前流行的企業級搜尋引擎。設計用於雲端計算中,能夠達到實時搜尋,穩定,可靠,快速,安裝使用方便。")
.endObject();
XContentBuilder builder2 = XContentFactory
.jsonBuilder()
.startObject()
.field("id2", 9)
.field("title2", "ElasticSearch是一個基於Lucene的搜尋伺服器")
.field("content2",
"它提供了一個分散式多使用者能力的全文搜尋引擎,基於RESTful web介面。Elasticsearch是用Java開發的,並作為Apache許可條款下的開放原始碼釋出,是當前流行的企業級搜尋引擎。設計用於雲端計算中,能夠達到實時搜尋,穩定,可靠,快速,安裝使用方便。")
.endObject();
// 建立文件物件
client.prepareIndex("blog1", "article", "4").setSource(builder).get();
client.prepareIndex("blog1", "comment", "8").setSource(builder2).get();
// 關閉連線
client.close();
}
// boolQuery() 布林查詢,可以用來組合多個查詢條件
// fuzzyQuery() 相似度查詢
// matchAllQuery() 查詢所有資料
// regexQuery() 正則表示式查詢
// termQuery() 詞條查詢
// wildcardQuery() 模糊查詢
// 搜尋在elasticSearch中建立文件物件
@Test
public void demo2() throws IOException {
// 建立連線搜尋伺服器物件
Client client = TransportClient
.builder()
.build()
.addTransportAddress(
new InetSocketTransportAddress(InetAddress
.getByName("127.0.0.1"), 9300));
// 搜尋資料
// get() 相當於 execute().actionGet()
SearchResponse searchResponse = client.prepareSearch("blog1")
.setTypes("comment").setQuery(QueryBuilders.matchAllQuery())
.get();
printSearchResponse(searchResponse);
// 關閉連線
client.close();
}
private void printSearchResponse(SearchResponse searchResponse) {
SearchHits hits = searchResponse.getHits(); // 獲取命中次數,查詢結果有多少物件
System.out.println("查詢結果有:" +hits.getHits().length + "條"+" 總條數="+hits.getTotalHits());
Iterator<SearchHit> iterator = hits.iterator();
while (iterator.hasNext()) {
SearchHit searchHit = iterator.next(); // 每個查詢物件
System.out.println(searchHit.getSourceAsString()); // 獲取字串格式列印
System.out.println("title2:" + searchHit.getSource().get("title2"));
}
}
// 各種查詢使用
@Test
public void demo3() throws IOException {
// 建立連線搜尋伺服器物件
Client client = TransportClient
.builder()
.build()
.addTransportAddress(
new InetSocketTransportAddress(InetAddress
.getByName("127.0.0.1"), 9300));
// 搜尋資料
// get() === execute().actionGet()
// SearchResponse searchResponse = client.prepareSearch("blog1")
// .setTypes("comment")
// .setQuery(QueryBuilders.queryStringQuery("分花草")).get();
SearchResponse searchResponse = client.prepareSearch("blog2")
.setTypes("article")
.setQuery(QueryBuilders.wildcardQuery("content", "*服器*")).get();
// SearchResponse searchResponse = client.prepareSearch("blog2")
// .setTypes("article")
// .setQuery(QueryBuilders.termQuery("content", "方案")).get();
printSearchResponse(searchResponse);
// 關閉連線
client.close();
}
// 索引操作
@Test
public void demo4() throws IOException {
// 建立連線搜尋伺服器物件
Client client = TransportClient
.builder()
.build()
.addTransportAddress(
new InetSocketTransportAddress(InetAddress
.getByName("127.0.0.1"), 9300));
// 建立索引
client.admin().indices().prepareCreate("blog2").get();
// 刪除索引
// client.admin().indices().prepareDelete("blog2").get();
// 關閉連線
client.close();
}
// 對映操作
@Test
public void demo5() throws IOException, InterruptedException,
ExecutionException {
// 建立連線搜尋伺服器物件
Client client = TransportClient
.builder()
.build()
.addTransportAddress(
new InetSocketTransportAddress(InetAddress
.getByName("127.0.0.1"), 9300));
// 新增對映
XContentBuilder builder = XContentFactory.jsonBuilder()
.startObject()
.startObject("article")
.startObject("properties")
.startObject("id")
.field("type", "integer")
.field("store", "yes")
.endObject()
.startObject("title")
.field("type", "string")
.field("store", "yes")
.field("analyzer", "ik")
.endObject()
.startObject("content")
.field("type", "string")
.field("store", "yes")
.field("analyzer", "ik")
.endObject()
.endObject()
.endObject()
.endObject();
PutMappingRequest mapping = Requests.putMappingRequest("blog2")
.type("article").source(builder);
client.admin().indices().putMapping(mapping).get();
// 關閉連線
client.close();
}
// 文件相關操作
@Test
public void demo6() throws IOException, InterruptedException,
ExecutionException {
// 建立連線搜尋伺服器物件
Client client = TransportClient
.builder()
.build()
.addTransportAddress(
new InetSocketTransportAddress(InetAddress
.getByName("127.0.0.1"), 9300));
// 描述json 資料
/*
* {id:xxx, title:xxx, content:xxx}
*/
Article article = new Article();
article.setId(2);
article.setTitle("開發工作更加快樂喲");
article.setContent("我們希望我們的搜尋解決方案要快,我們希望有一個零配置和一個完全免費的搜尋模式,"
+ "我們希望能夠簡單地使用JSON通過HTTP的索引資料,我們希望我們的搜尋伺服器始終可用,"
+ "我們希望能夠一臺開始並擴充套件到數百,我們要實時搜尋,我們要簡單的多租戶,"
+ "我們希望建立一個雲的解決方案。Elasticsearch旨在解決所有這些問題和更多的問題。");
ObjectMapper objectMapper = new ObjectMapper();
// 建立文件
// client.prepareIndex("blog2", "article", article.getId().toString())
// .setSource(objectMapper.writeValueAsString(article)).get();
// 修改文件
client.prepareUpdate("blog2", "article", article.getId().toString())
.setDoc(objectMapper.writeValueAsString(article)).get();
// 修改文件
// client.update(
// new UpdateRequest("blog2", "article", article.getId().toString())
// .doc(objectMapper.writeValueAsString(article))).get();
// 刪除文件
// client.prepareDelete("blog2", "article", article.getId().toString())
// .get();
// 刪除文件
// client.delete(
// new DeleteRequest("blog2", "article", article.getId()
// .toString())).get();
// 關閉連線
client.close();
}
// 批量插入100條記錄
@Test
public void demo7() throws IOException, InterruptedException,
ExecutionException {
// 建立連線搜尋伺服器物件
Client client = TransportClient
.builder()
.build()
.addTransportAddress(
new InetSocketTransportAddress(InetAddress
.getByName("127.0.0.1"), 9300));
ObjectMapper objectMapper = new ObjectMapper();
for (int i = 1; i <= 100; i++) {
// 描述json 資料
Article article = new Article();
article.setId(i);
article.setTitle(i + "搜尋工作其實很快樂");
article.setContent(i
+ "我們希望我們的搜尋解決方案要快,我們希望有一個零配置和一個完全免費的搜尋模式,我們希望能夠簡單地使用JSON通過HTTP的索引資料,我們希望我們的搜尋伺服器始終可用,我們希望能夠一臺開始並擴充套件到數百,我們要實時搜尋,我們要簡單的多租戶,我們希望建立一個雲的解決方案。Elasticsearch旨在解決所有這些問題和更多的問題。");
// 建立文件
client.prepareIndex("blog2", "article", article.getId().toString())
.setSource(objectMapper.writeValueAsString(article)).get();
}
// 關閉連線
client.close();
}
// 分頁搜尋
@Test
public void demo8() throws IOException {
// 建立連線搜尋伺服器物件
Client client = TransportClient
.builder()
.build()
.addTransportAddress(
new InetSocketTransportAddress(InetAddress
.getByName("127.0.0.1"), 9300));
// SearchResponse searchResponse = client.prepareSearch("blog2")
// .setTypes("article")
// .setQuery(QueryBuilders.queryStringQuery("文")).get();
// 搜尋資料
// get() === execute().actionGet()
SearchRequestBuilder searchRequestBuilder = client
.prepareSearch("blog2").setTypes("article")
.setQuery(QueryBuilders.matchAllQuery());
// 查詢第1頁資料,每頁20條
searchRequestBuilder.setFrom(0).setSize(20);
SearchResponse searchResponse = searchRequestBuilder.get();
printSearchResponse(searchResponse);
// 關閉連線
client.close();
}
// 高亮查詢結果 處理 搜尋
@Test
public void demo9() throws IOException {
// 建立連線搜尋伺服器物件
Client client = TransportClient
.builder()
.build()
.addTransportAddress(
new InetSocketTransportAddress(InetAddress
.getByName("127.0.0.1"), 9300));
ObjectMapper objectMapper = new ObjectMapper();
// 搜尋資料
SearchRequestBuilder searchRequestBuilder = client
.prepareSearch("blog2").setTypes("article")
.setQuery(QueryBuilders.termQuery("content", "我們"));
// 高亮定義
searchRequestBuilder.addHighlightedField("content"); // 對content欄位進行高亮
searchRequestBuilder.setHighlighterPreTags("<em>"); // 前置元素
searchRequestBuilder.setHighlighterPostTags("</em>");// 後置元素
SearchResponse searchResponse = searchRequestBuilder.get();
SearchHits hits = searchResponse.getHits(); // 獲取命中次數,查詢結果有多少物件
System.out.println("查詢結果有:" + hits.getTotalHits() + "條");
Iterator<SearchHit> iterator = hits.iterator();
int i=0;
while (iterator.hasNext()) {
if(i>0) return;
i++;
SearchHit searchHit = iterator.next(); // 每個查詢物件
// 將高亮處理後內容,替換原有內容 (原有內容,可能會出現顯示不全 )
Map<String, HighlightField> highlightFields = searchHit
.getHighlightFields();
// HighlightField titleField = highlightFields.get("title");
HighlightField contentField = highlightFields.get("content");
// 獲取到原有內容中 每個高亮顯示 集中位置 fragment 就是高亮片段
// Text[] fragments = titleField.fragments();
// String title = "";
// for (Text text : fragments) {
// title += text;
// System.out.println("片段裡的內容="+title);
// }
Text[] contentFragments = contentField.fragments();
String content = "";
int j=0;
for (Text text : contentFragments) {
System.out.println("i="+i + " j="+j++ +"片段裡的內容text="+text+"\n");
content += text;
}
System.out.println(i+"片段裡的內容content="+content);
// 將查詢結果轉換為物件
Article article = objectMapper.readValue(
searchHit.getSourceAsString(), Article.class);
// 用高亮後內容,替換原有內容
// article.setTitle(title);
article.setContent(content);
// System.out.println(article);
}
// 關閉連線
client.close();
}
}