Home | 簡體中文 | 繁體中文 | 雜文 | 打賞(Donations) | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 知乎專欄 | Search | Email

24.6. Query 查詢

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

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

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