1、Code -> GitHub
https://github.com/liufengji/elasticsearch_api.git
2姓言、源代碼
@Test
public void fuzzy() {
// 1 模糊查詢
SearchResponse searchResponse = client.prepareSearch("blog").setTypes("article")
.setQuery(QueryBuilders.fuzzyQuery("title", "lucene")).get();
// 2 打印查詢結(jié)果
SearchHits hits = searchResponse.getHits(); // 獲取命中次數(shù),查詢結(jié)果有多少對象
System.out.println("查詢結(jié)果有:" + hits.getTotalHits() + "條");
Iterator<SearchHit> iterator = hits.iterator();
while (iterator.hasNext()) {
SearchHit searchHit = iterator.next(); // 每個查詢對象
System.out.println(searchHit.getSourceAsString()); // 獲取字符串格式打印
}
// 3 關(guān)閉連接
client.close();
}