コード例 #1
0
  @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;
  }
コード例 #2
0
  @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;
    }
  }
コード例 #3
0
  @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;
  }