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());
}
}
}
22.6.3. match phrase 短語精準匹配
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());
}
}
}