應用說明見代碼注解。
1.簡單搜索實例展示:
public void search() throws IOException { // 自定義集群結點名稱
String clusterName = "elasticsearch_pudongping"; // 獲取客戶端
Client client = ESClient.initClient(clusterName); // 創建查詢索引,參數productindex表示要查詢的索引庫為productindex
SearchRequestBuilder searchRequestBuilder = client .prepareSearch("productindex"); // 設置查詢索引類型,setTypes("productType1", "productType2","productType3"); // 用來設定在多個類型中搜索
searchRequestBuilder.setTypes("productIndex"); // 設置查詢類型 1.SearchType.DFS_QUERY_THEN_FETCH = 精確查詢 2.SearchType.SCAN = // 掃描查詢,無序
searchRequestBuilder.setSearchType(SearchType.DFS_QUERY_THEN_FETCH); // 設置查詢關鍵詞
searchRequestBuilder .setQuery(QueryBuilders.fieldQuery("title", "Acer")); // 查詢過濾器過濾價格在4000-5000內 這里范圍為[4000,5000]區間閉包含,搜索結果包含價格為4000和價格為5000的數據
searchRequestBuilder.setFilter(FilterBuilders.rangeFilter("price") .from(4000).to(5000)); // 分頁應用
searchRequestBuilder.setFrom(0).setSize(60); // 設置是否按查詢匹配度排序
searchRequestBuilder.setExplain(true); // 執行搜索,返回搜索響應信息
SearchResponse response = searchRequestBuilder.execute().actionGet(); SearchHits searchHits = response.getHits(); SearchHit[] hits = searchHits.getHits(); for (int i = 0; i < hits.length; i++) { SearchHit hit = hits[i]; Map<String, Object> result = hit.getSource(); // 打印map集合:{id=26, onSale=true, title=宏基Acer樂3, price=4009.0, // description=null, createDate=1380530123140, type=2}
System.out.println(result); } System.out.println("search success .."); }
說明:
client.prepareSearch用來創建一個SearchRequestBuilder,搜索即由SearchRequestBuilder執行。
client.prepareSearch方法有參數為一個或多個index,表現在數據庫中,即零個或多個數據庫名,你既可以使用(下面兩個都可以表示在多個索引庫中查找):
client.prepareSearch().setIndices("index1","index2","index3","index4");
或者:
client.prepareSearch("index1","index2","index3","index4");
SearchRequestBuilder常用方法說明:
(1) setIndices(String... indices):上文中描述過,參數可為一個或多個字符串,表示要進行檢索的index;
(2) setTypes(String... types):參數可為一個或多個字符串,表示要進行檢索的type,當參數為0個或者不調用此方法時,表示查詢所有的type;
setSearchType(SearchType searchType):執行檢索的類別,值為org.elasticsearch.action.search.SearchType的元素,SearchType是一個枚舉類型的類,
其值如下所示:
QUERY_THEN_FETCH:查詢是針對所有的塊執行的,但返回的是足夠的信息,而不是文檔內容(Document)。結果會被排序和分級,基於此,只有相關的塊的文檔對象會被返回。由於被取到的僅僅是這些,故而返回的hit的大小正好等於指定的size。這對於有許多塊的index來說是很便利的(返回結果不會有重復的,因為塊被分組了)
QUERY_AND_FETCH:最原始(也可能是最快的)實現就是簡單的在所有相關的shard上執行檢索並返回結果。每個shard返回一定尺寸的結果。由於每個shard已經返回了一定尺寸的hit,這種類型實際上是返回多個shard的一定尺寸的結果給調用者。
DFS_QUERY_THEN_FETCH:與QUERY_THEN_FETCH相同,預期一個初始的散射相伴用來為更准確的score計算分配了的term頻率。
DFS_QUERY_AND_FETCH:與QUERY_AND_FETCH相同,預期一個初始的散射相伴用來為更准確的score計算分配了的term頻率。
SCAN:在執行了沒有進行任何排序的檢索時執行瀏覽。此時將會自動的開始滾動結果集。
COUNT:只計算結果的數量,也會執行facet。
(4) setSearchType(String searchType),與setSearchType(SearchType searchType)類似,區別在於其值為字符串型的SearchType,值可為dfs_query_then_fetch、dfsQueryThenFetch、dfs_query_and_fetch、dfsQueryAndFetch、query_then_fetch、queryThenFetch、query_and_fetch或queryAndFetch;
(5) setScroll(Scroll scroll)、setScroll(TimeValue keepAlive)和setScroll(String keepAlive),設置滾動,參數為Scroll時,直接用new Scroll(TimeValue)構造一個Scroll,為TimeValue或String時需要將TimeValue和String轉化為Scroll;
(6) setTimeout(TimeValue timeout)和setTimeout(String timeout),設置搜索的超時時間;
(7) setQuery,設置查詢使用的Query;
(8) setFilter,設置過濾器;
(9) setMinScore,設置Score的最小數量;
(10) setFrom,從哪一個Score開始查;
(11) setSize,需要查詢出多少條結果;
檢索出結果后,通過response.getHits()可以得到所有的SearchHit,得到Hit后,便可迭代Hit取到對應的Document,轉化成為需要的實體。
2.搜索高亮顯示
SearchRequestBuilder中的addHighlightedField()方法可以定制在哪個域值的檢索結果的關鍵字上增加高亮
public void search() throws IOException { // 自定義集群結點名稱
String clusterName = "elasticsearch_pudongping"; // 獲取客戶端
Client client = ESClient.initClient(clusterName); // 創建查詢索引,參數productindex表示要查詢的索引庫為productindex
SearchRequestBuilder searchRequestBuilder = client .prepareSearch("productindex"); // 設置查詢索引類型,setTypes("productType1", "productType2","productType3"); // 用來設定在多個類型中搜索
searchRequestBuilder.setTypes("productIndex"); // 設置查詢類型 1.SearchType.DFS_QUERY_THEN_FETCH = 精確查詢 2.SearchType.SCAN = 掃描查詢,無序
searchRequestBuilder.setSearchType(SearchType.DFS_QUERY_THEN_FETCH); // 設置查詢關鍵詞
searchRequestBuilder .setQuery(QueryBuilders.fieldQuery("title", "Acer")); // 查詢過濾器過濾價格在4000-5000內 這里范圍為[4000,5000]區間閉包含,搜索結果包含價格為4000和價格為5000的數據
searchRequestBuilder.setFilter(FilterBuilders.rangeFilter("price") .from(4000).to(5000)); // 分頁應用
searchRequestBuilder.setFrom(0).setSize(60); // 設置是否按查詢匹配度排序
searchRequestBuilder.setExplain(true); //設置高亮顯示
searchRequestBuilder.addHighlightedField("title"); searchRequestBuilder.setHighlighterPreTags("<span style=\"color:red\">"); searchRequestBuilder.setHighlighterPostTags("</span>"); // 執行搜索,返回搜索響應信息
SearchResponse response = searchRequestBuilder.execute().actionGet(); //獲取搜索的文檔結果
SearchHits searchHits = response.getHits(); SearchHit[] hits = searchHits.getHits(); ObjectMapper mapper = new ObjectMapper(); for (int i = 0; i < hits.length; i++) { SearchHit hit = hits[i]; //將文檔中的每一個對象轉換json串值
String json = hit.getSourceAsString(); //將json串值轉換成對應的實體對象
Product product = mapper.readValue(json, Product.class); //獲取對應的高亮域
Map<String, HighlightField> result = hit.highlightFields(); //從設定的高亮域中取得指定域
HighlightField titleField = result.get("title"); //取得定義的高亮標簽
Text[] titleTexts = titleField.fragments(); //為title串值增加自定義的高亮標簽
String title = ""; for(Text text : titleTexts){ title += text; } //將追加了高亮標簽的串值重新填充到對應的對象
product.setTitle(title); //打印高亮標簽追加完成后的實體對象
System.out.println(product); } System.out.println("search success .."); }
程序運行結果:
[id=8,title=宏基<span style="color:red">Acer</span>,description=宏基Acer蜂鳥系列,price=5000.0,onSale=true,type=1,createDate=Mon Sep 30 13:46:41 CST 2013] [id=21,title=宏基<span style="color:red">Acer</span>,description=宏基Acer蜂鳥系列,price=5000.0,onSale=true,type=1,createDate=Mon Sep 30 13:48:17 CST 2013] [id=7,title=宏基<span style="color:red">Acer</span>,description=宏基Acer蜂鳥系列,price=5000.0,onSale=true,type=1,createDate=Mon Sep 30 11:38:50 CST 2013] [id=5,title=宏基<span style="color:red">Acer</span>樂0,description=<null>,price=4000.0,onSale=true,type=1,createDate=Mon Sep 30 16:35:23 CST 2013] [id=12,title=宏基<span style="color:red">Acer</span>樂1,description=<null>,price=4003.0,onSale=false,type=2,createDate=Mon Sep 30 16:35:23 CST 2013] [id=19,title=宏基<span style="color:red">Acer</span>樂2,description=<null>,price=4006.0,onSale=false,type=1,createDate=Mon Sep 30 16:35:23 CST 2013] [id=26,title=宏基<span style="color:red">Acer</span>樂3,description=<null>,price=4009.0,onSale=true,type=2,createDate=Mon Sep 30 16:35:23 CST 2013] [id=33,title=宏基<span style="color:red">Acer</span>樂4,description=<null>,price=4012.0,onSale=false,type=1,createDate=Mon Sep 30 16:35:23 CST 2013]
從程序執行結果中我們可以看到,我們定義的高亮標簽已經追加到指定的域上了.
當搜索索引的時候,你搜索關鍵字包含了特殊字符,那么程序就會報錯
// fieldQuery 這個必須是你的索引字段哦,不然查不到數據,這里我只設置兩個字段 id ,title
String title = "title+-&&||!(){}[]^\"~*?:\\"; title = QueryParser.escape(title);// 主要就是這一句把特殊字符都轉義,那么lucene就可以識別
searchRequestBuilder.setQuery(QueryBuilders.fieldQuery("title", title));
轉載請注明出處:[http://www.cnblogs.com/dennisit/p/3363851.html]