ElasticSearch Java Api(二) -檢索索引庫
阿新 • • 發佈:2018-12-26
上篇部落格記錄瞭如何用java呼叫api把資料寫入索引,這次記錄下如何搜尋。
一、準備資料
String data1 = JsonUtil.model2Json(new Blog(1, "git簡介", "2016-06-19", "SVN與Git最主要的區別...")); String data2 = JsonUtil.model2Json(new Blog(2, "Java中泛型的介紹與簡單使用", "2016-06-19", "學習目標 掌握泛型的產生意義...")); String data3 = JsonUtil.model2Json(new Blog(3, "SQL基本操作", "2016-06-19", "基本操作:CRUD ...")); String data4 = JsonUtil.model2Json(new Blog(4, "Hibernate框架基礎", "2016-06-19", "Hibernate框架基礎...")); String data5 = JsonUtil.model2Json(new Blog(5, "Git基本知識git", "2016-06-19", "Shell是什麼...")); String data6 = JsonUtil.model2Json(new Blog(6, "C++基本知識", "2016-06-19", "Shell是什麼...")); String data7 = JsonUtil.model2Json(new Blog(7, "Mysql基本知識", "2016-06-19", "git是什麼..."));
二、查詢
一次查詢可分為下面四個步驟:
-
1.建立連線ElasticSearch服務的client.
索引在ElasticSearch伺服器上,進行索引的查詢首先要和伺服器建立連線,這是第一步。Client client = TransportClient.builder().build() .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
-
2.建立QueryBuilder.
QueryBuilder可以設定單個欄位的查詢,也可以設定多個欄位的查詢.
e.g.1hibernate
關鍵字的文件:QueryBuilder qb1 = termQuery("title", "hibernate");
e.g.2: 查詢title欄位或content欄位中包含
git
關鍵字的文件:QueryBuilder qb2= QueryBuilders.multiMatchQuery("git", "title","content");
-
3.執行查詢
通過client設定查詢的index、type、query.返回一個SearchResponse物件:SearchResponse response = client.prepareSearch("blog").setTypes("article").setQuery(qb2).execute() .actionGet();
-
4.處理查詢結果
SearchResponse物件的getHits()方法獲取查詢結果,返回一個SearchHits的集合,遍歷集合獲取查詢的文件資訊:SearchHits hits = response.getHits();
三、java程式碼實現
package cn.com.bropen.es;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import static org.elasticsearch.index.query.QueryBuilders.*;
public class ElasticSearchGet {
public static void main(String[] args) {
// client startup
try {
Client client = TransportClient.builder().build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
QueryBuilder qb1 = termQuery("title", "hibernate");
QueryBuilder qb2= QueryBuilders.multiMatchQuery("git", "title","content");
SearchResponse response = client.prepareSearch("blog").setTypes("article").setQuery(qb2).execute()
.actionGet();
SearchHits hits = response.getHits();
if (hits.totalHits() > 0) {
for (SearchHit hit : hits) {
System.out.println("score:"+hit.getScore()+":\t"+hit.getSource());// .get("title")
}
} else {
System.out.println("搜到0條結果");
}
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
查詢結果:
log4j:WARN No appenders could be found for logger (org.elasticsearch.plugins).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
score:0.5: {posttime=2016-06-19, id=1, title=git簡介, content=SVN與Git最主要的區別...}
score:0.17673586: {posttime=2016-06-19, id=7, title=Mysql基本知識, content=git是什麼...}
score:0.049935166: {posttime=2016-06-19, id=5, title=Git基本知識git, content=Shell是什麼...}
參考資料: