Elasticsearch-Java API操作(二)條件查詢QueryBuilder(5)【模糊查詢(fuzzy)】


大數據技術之Elasticsearch-Java API操作(二)條件查詢QueryBuilder

模糊查詢(fuzzy)

注意:需要加入分詞器,不然容易搜不到匹配的詞

@Test

public void fuzzy() {

 

// 1 模糊查詢

SearchResponse searchResponse = client.prepareSearch("blog").setTypes("article")

.setQuery(QueryBuilders.fuzzyQuery("title", "lucene")).get();

 

// 2 打印查詢結果

SearchHits hits = searchResponse.getHits(); // 獲取命中次數,查詢結果有多少對象

System.out.println("查詢結果有:" + hits.getTotalHits() + "條");

 

Iterator<SearchHit> iterator = hits.iterator();

 

while (iterator.hasNext()) {

SearchHit searchHit = iterator.next(); // 每個查詢對象

 

System.out.println(searchHit.getSourceAsString()); // 獲取字符串格式打印

}

 

// 3 關閉連接

client.close();

}

****自己操作****

Java代碼:

// 十二、模糊查詢-fuzzy // 注意:需要加入分詞器,不然容易搜不到匹配的詞
 @Test public void fuzzyQuery() { // 模糊查詢
        SearchResponse searchResponse = client.prepareSearch("blog").setTypes("article")  .setQuery(QueryBuilders.fuzzyQuery("content", "web")).get();  SearchHits hits = searchResponse.getHits(); System.out.println("查詢結果有:" + hits.getTotalHits() + " 條"); for (SearchHit searchHits : hits) { System.out.println(searchHits.getSourceAsString()); } client.close(); }

結果:

查詢結果有:4 條

{"id":5,"title":"基於Lucene的搜索服務器","content":"它提供了一個分布式多用戶能力的全文搜索引擎,基於RESTful web接口"}

{"id":"1","title":"基於Lucene的搜索服務器","content":"它提供了一個分布式多用戶能力的全文搜索引擎,基於RESTful web接口"}

{"id":3,"title":"基於Lucene的搜索服務器","content":"它提供了一個分布式多用戶能力的全文搜索引擎,基於RESTful web接口"}

{"id":"2","title":"基於Lucene的搜索服務器","content":"它提供了一個分布式多用戶能力的全文搜索引擎,基於RESTful web接口。大數據前景無限","createDate":"2017-8-22"}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM