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

22.9. 返回 Source 欄位

下面例子查詢返回 "id", "title", "description", "image", "ctime"

		
	/*
	 * 範例:/restful/search/article/list/23/0/20.json?tags=美國
	 */
	@RequestMapping(value = "/article/list/{siteId}/{from}/{size}")
	public List<Map<String, Object>> listBySiteIdAndTags(@PathVariable String siteId, @PathVariable int from, @PathVariable int size, @RequestParam(value = "tags", required = false) String tags) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

		SearchRequestBuilder searchRequestBuilder = client.prepareSearch("information").setTypes("article").setSearchType(SearchType.DFS_QUERY_THEN_FETCH).addSort("ctime", SortOrder.DESC);

		searchRequestBuilder.setFetchSource(new String[] { "id", "title", "description", "image", "ctime" }, null);
		if (tags != null && !tags.equals("")) {
			// logger.info(tags);
			searchRequestBuilder.setQuery(QueryBuilders.matchQuery("tags", tags));
		}
		searchRequestBuilder.setPostFilter(QueryBuilders.termQuery("site_id", siteId)).setFrom(from).setSize(size).setExplain(true);

		logger.info(searchRequestBuilder.toString());
		SearchResponse response = searchRequestBuilder.get();

		for (final SearchHit hit : response.getHits().getHits()) {
			// logger.info(hit.getSourceAsString());
			hit.getSourceAsMap().remove("content");
			list.add(hit.getSourceAsMap());
		}
		logger.info(tags);
		return list;
	}