Home | 简体中文 | 繁体中文 | 杂文 | 打赏(Donations) | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 知乎专栏 | Search | About

21.6. Query 查询

21.6.1. match all 匹配所有数据

			
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();		
			
			

21.6.2. match 匹配查询

			
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());
		}
	}
}
			
			

21.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());
		}
	}
}