ElasticSearch搜索实例含高亮显示及搜索的特殊字符过滤
?
應(yīng)用說(shuō)明見(jiàn)代碼注解。
1.簡(jiǎn)單搜索實(shí)例展示:
說(shuō)明:
client.prepareSearch用來(lái)創(chuàng)建一個(gè)SearchRequestBuilder,搜索即由SearchRequestBuilder執(zhí)行。
client.prepareSearch方法有參數(shù)為一個(gè)或多個(gè)index,表現(xiàn)在數(shù)據(jù)庫(kù)中,即零個(gè)或多個(gè)數(shù)據(jù)庫(kù)名,你既可以使用(下面兩個(gè)都可以表示在多個(gè)索引庫(kù)中查找):
client.prepareSearch().setIndices("index1","index2","index3","index4");或者:
client.prepareSearch("index1","index2","index3","index4");
SearchRequestBuilder常用方法說(shuō)明:
檢索出結(jié)果后,通過(guò)response.getHits()可以得到所有的SearchHit,得到Hit后,便可迭代Hit取到對(duì)應(yīng)的Document,轉(zhuǎn)化成為需要的實(shí)體。
2.搜索高亮顯示
org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder
org.springframework.data.elasticsearch.core.SearchResultMapper
org.springframework.data.domain.PageImpl
org.elasticsearch.action.search.SearchResponse
org.elasticsearch.search.SearchHit
org.elasticsearch.search.highlight.HighlightField
?
?
@Test public void shouldReturnHighlightedFieldsForGivenQueryAndFields() {//givenString documentId = randomNumeric(5);String actualMessage = "some test message";String highlightedMessage = "some <em>test</em> message";SampleEntity sampleEntity = SampleEntity.builder().id(documentId).message(actualMessage).version(System.currentTimeMillis()).build();IndexQuery indexQuery = getIndexQuery(sampleEntity);elasticsearchTemplate.index(indexQuery);elasticsearchTemplate.refresh(SampleEntity.class);SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(termQuery("message", "test")).withHighlightFields(new HighlightBuilder.Field("message")).build();Page<SampleEntity> sampleEntities = elasticsearchTemplate.queryForPage(searchQuery, SampleEntity.class, new SearchResultMapper() {@Overridepublic <T> Page<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {List<SampleEntity> chunk = new ArrayList<SampleEntity>();for (SearchHit searchHit : response.getHits()) {if (response.getHits().getHits().length <= 0) {return null;}SampleEntity user = new SampleEntity();user.setId(searchHit.getId());user.setMessage((String) searchHit.getSource().get("message"));user.setHighlightedMessage(searchHit.getHighlightFields().get("message").fragments()[0].toString());chunk.add(user);}if (chunk.size() > 0) {return new PageImpl<T>((List<T>) chunk);}return null;}});assertThat(sampleEntities.getContent().get(0).getHighlightedMessage(), is(highlightedMessage)); }http://stackoverflow.com/questions/37049764/how-to-provide-highlighting-with-spring-data-elasticsearch
SearchRequestBuilder中的addHighlightedField()方法可以定制在哪個(gè)域值的檢索結(jié)果的關(guān)鍵字上增加高亮
public void search() throws IOException {// 自定義集群結(jié)點(diǎn)名稱String clusterName = "elasticsearch_pudongping";// 獲取客戶端Client client = ESClient.initClient(clusterName); // 創(chuàng)建查詢索引,參數(shù)productindex表示要查詢的索引庫(kù)為productindexSearchRequestBuilder searchRequestBuilder = client.prepareSearch("productindex");// 設(shè)置查詢索引類型,setTypes("productType1", "productType2","productType3");// 用來(lái)設(shè)定在多個(gè)類型中搜索searchRequestBuilder.setTypes("productIndex");// 設(shè)置查詢類型 1.SearchType.DFS_QUERY_THEN_FETCH = 精確查詢 2.SearchType.SCAN = 掃描查詢,無(wú)序searchRequestBuilder.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);// 設(shè)置查詢關(guān)鍵詞searchRequestBuilder.setQuery(QueryBuilders.fieldQuery("title", "Acer"));// 查詢過(guò)濾器過(guò)濾價(jià)格在4000-5000內(nèi) 這里范圍為[4000,5000]區(qū)間閉包含,搜索結(jié)果包含價(jià)格為4000和價(jià)格為5000的數(shù)據(jù)searchRequestBuilder.setFilter(FilterBuilders.rangeFilter("price").from(4000).to(5000));// 分頁(yè)應(yīng)用searchRequestBuilder.setFrom(0).setSize(60);// 設(shè)置是否按查詢匹配度排序searchRequestBuilder.setExplain(true);//設(shè)置高亮顯示searchRequestBuilder.addHighlightedField("title");searchRequestBuilder.setHighlighterPreTags("<span style=\"color:red\">");searchRequestBuilder.setHighlighterPostTags("</span>");// 執(zhí)行搜索,返回搜索響應(yīng)信息SearchResponse response = searchRequestBuilder.execute().actionGet();//獲取搜索的文檔結(jié)果SearchHits searchHits = response.getHits();SearchHit[] hits = searchHits.getHits();ObjectMapper mapper = new ObjectMapper();for (int i = 0; i < hits.length; i++) {SearchHit hit = hits[i];//將文檔中的每一個(gè)對(duì)象轉(zhuǎn)換json串值String json = hit.getSourceAsString();//將json串值轉(zhuǎn)換成對(duì)應(yīng)的實(shí)體對(duì)象Product product = mapper.readValue(json, Product.class); //獲取對(duì)應(yīng)的高亮域Map<String, HighlightField> result = hit.highlightFields(); //從設(shè)定的高亮域中取得指定域HighlightField titleField = result.get("title"); //取得定義的高亮標(biāo)簽Text[] titleTexts = titleField.fragments(); //為title串值增加自定義的高亮標(biāo)簽String title = ""; for(Text text : titleTexts){ title += text; }//將追加了高亮標(biāo)簽的串值重新填充到對(duì)應(yīng)的對(duì)象product.setTitle(title);//打印高亮標(biāo)簽追加完成后的實(shí)體對(duì)象System.out.println(product);}System.out.println("search success ..");}?
?
程序運(yùn)行結(jié)果:
[id=8,title=宏基<span style="color:red">Acer</span>,description=宏基Acer蜂鳥(niǎo)系列,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蜂鳥(niǎo)系列,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蜂鳥(niǎo)系列,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>樂(lè)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>樂(lè)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>樂(lè)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>樂(lè)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>樂(lè)4,description=<null>,price=4012.0,onSale=false,type=1,createDate=Mon Sep 30 16:35:23 CST 2013]從程序執(zhí)行結(jié)果中我們可以看到,我們定義的高亮標(biāo)簽已經(jīng)追加到指定的域上了.
當(dāng)搜索索引的時(shí)候,你搜索關(guān)鍵字包含了特殊字符,那么程序就會(huì)報(bào)錯(cuò)
// fieldQuery 這個(gè)必須是你的索引字段哦,不然查不到數(shù)據(jù),這里我只設(shè)置兩個(gè)字段 id ,title String title = "title+-&&||!(){}[]^\"~*?:\\"; title = QueryParser.escape(title);// 主要就是這一句把特殊字符都轉(zhuǎn)義,那么lucene就可以識(shí)別 searchRequestBuilder.setQuery(QueryBuilders.fieldQuery("title", title));
轉(zhuǎn)載請(qǐng)注明出處:[http://www.cnblogs.com/dennisit/p/3363851.html]
?
轉(zhuǎn)載于:https://www.cnblogs.com/softidea/p/6119329.html
總結(jié)
以上是生活随笔為你收集整理的ElasticSearch搜索实例含高亮显示及搜索的特殊字符过滤的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 虚拟机ping不通主机,但是主机可以pi
- 下一篇: 使用ajax预加载图片