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