Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About
知乎專欄多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者”

22.6. Query 查詢

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

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

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