@Override public List<Article> query(String queryString, int size) { Assert.hasText(queryString, "queryString must not be empty!"); SearchResponse searchResponse = transportClient .prepareSearch(INDICE) .setTypes(TYPE) .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) .setQuery(QueryBuilders.queryString(queryString)) .setFrom(0) .setSize(size) .setExplain(true) .execute() .actionGet(); SearchHits hits = searchResponse.getHits(); long total = hits.getTotalHits(); logger.debug("search articles result total:{}", total); List<Article> list = new ArrayList<Article>(); Article article = null; for (SearchHit hit : hits) { Long id = (Long) hit.getSource().get("id"); String title = (String) hit.getSource().get(TITLE); String summary = (String) hit.getSource().get(SUMMARY); logger.debug("id:{},title:{},summary:{}", id, title, summary); article = new Article(); article.setId(id); article.setTitle(title); article.setSummary(summary); list.add(article); } return list; }
@Override public void index(List<Article> list) { if (list.size() > 0) { try { BulkRequestBuilder bulkRequest = transportClient.prepareBulk(); for (Article article : list) { XContentBuilder builder = XContentFactory.jsonBuilder() .startObject() .field(TITLE, article.getTitle()) .field(SUMMARY, HtmlHelper.filterHtml(article.getSummary())) .endObject(); bulkRequest.add( transportClient .prepareIndex(INDICE, TYPE, String.valueOf(article.getId())) .setSource(builder)); } BulkResponse bulkResponse = bulkRequest.execute().actionGet(); if (bulkResponse.hasFailures()) { logger.debug("bulkResponse.hasFailures()"); } else { logger.debug("bulk index ok!"); } } catch (IOException e) { e.printStackTrace(); } } }
@Override public void index(Article entity) { try { XContentBuilder builder = XContentFactory.jsonBuilder() .startObject() .field(TITLE, entity.getTitle()) .field(SUMMARY, HtmlHelper.filterHtml(entity.getSummary())) .endObject(); transportClient .prepareIndex(INDICE, TYPE, String.valueOf(entity.getId())) .setSource(builder) .execute() .actionGet(); logger.debug("index entity ok!"); } catch (Exception e) { e.printStackTrace(); } }
@Override public Article get(long id) { GetResponse getResponse = transportClient.prepareGet(INDICE, TYPE, String.valueOf(id)).execute().actionGet(); if (getResponse.isExists()) { Map<String, Object> map = getResponse.getSource(); String title = (String) map.get(TITLE); String summary = (String) map.get(SUMMARY); String idStr = getResponse.getId(); logger.debug("get id : {}", idStr); Article article = new Article(); article.setId(Long.parseLong(idStr)); article.setTitle(title); article.setSummary(summary); return article; } else { return null; } }
@Override public Page<Article> queryPage(Page<Article> page, String queryString) { Assert.notNull(page, "page must not null"); Assert.hasText(queryString, "queryString must not be empty!"); SearchResponse searchResponse = transportClient .prepareSearch(INDICE) .setTypes(TYPE) .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) .setQuery(QueryBuilders.queryString(queryString)) .setFrom(page.getFirst() - 1) .setSize(page.getPageSize()) .setExplain(true) .execute() .actionGet(); SearchHits hits = searchResponse.getHits(); long total = hits.getTotalHits(); logger.debug("search articles result total:{}", total); List<Article> list = new ArrayList<Article>(); Article article = null; for (SearchHit hit : hits) { long id = Long.parseLong(hit.getId()); String title = (String) hit.getSource().get(TITLE); String summary = (String) hit.getSource().get(SUMMARY); logger.debug("id:{},title:{},summary:{}", id, title, summary); article = new Article(); article.setId(id); article.setTitle(title); article.setSummary(summary); list.add(article); } page.setTotalCount(total); page.setResult(list); return page; }