SearchResponse response = client.prepareSearch() .setIndices(index) .setTypes(type) .setSearchType(SearchType.QUERY_AND_FETCH) .setFetchSource(new String[]{"title"}, null) .setQuery(QueryBuilders.matchAllQuery()) .setSize(10).execute().actionGet();
public void match() { SearchRequestBuilder requestBuilder = client.prepareSearch("company").setTypes("employee") .setQuery(QueryBuilders.matchQuery("name", "neo")); System.out.println(requestBuilder.toString()); SearchResponse response = requestBuilder.get(); System.out.println(response.status()); if (response.status().getStatus() == 200) { for (SearchHit hits : response.getHits().getHits()) { System.out.println(hits.getSourceAsString()); } } }
public void matchPhrase() { SearchRequestBuilder requestBuilder = client.prepareSearch("company").setTypes("employee") .setQuery(QueryBuilders.matchPhraseQuery("name", "neo")); SearchResponse response = requestBuilder.get(); if (response.status().getStatus() == 200) { for (SearchHit hits : response.getHits().getHits()) { System.out.println(hits.getSourceAsString()); } } }
原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。