@Test void testIndexThrowsException() { Index index = Index.create("index"); when(document.getContent()).thenReturn(xContentBuilder); when(document.getId()).thenReturn("id"); when(client.prepareIndex()).thenReturn(indexRequestBuilder); when(indexRequestBuilder.setIndex("index")).thenReturn(indexRequestBuilder); when(indexRequestBuilder.setType(any())).thenReturn(indexRequestBuilder); when(indexRequestBuilder.setId("id")).thenReturn(indexRequestBuilder); when(indexRequestBuilder.setSource(xContentBuilder)).thenReturn(indexRequestBuilder); when(indexRequestBuilder.get()).thenThrow(new ElasticsearchException("exception")); Exception exception = assertThrows(IndexException.class, () -> clientFacade.index(index, document)); assertThat(exception.getMessage()) .containsPattern("Error indexing doc with id 'id' in index 'index'\\."); }
public void index(Index index, Document document) { if (LOG.isTraceEnabled()) { LOG.trace("Indexing doc with id '{}' in index '{}' ...", document.getId(), index.getName()); } String indexName = index.getName(); String documentId = document.getId(); XContentBuilder source = document.getContent(); IndexRequestBuilder indexRequest = client .prepareIndex() .setIndex(indexName) .setType(indexName) .setId(documentId) .setSource(source); IndexResponse indexResponse; try { indexResponse = indexRequest.get(); } catch (ResourceNotFoundException e) { LOG.error("", e); throw new UnknownIndexException(index.getName()); } catch (ElasticsearchException e) { LOG.debug("", e); throw new IndexException( format("Error indexing doc with id '%s' in index '%s'.", documentId, indexName)); } if (indexResponse.getShardInfo().getSuccessful() == 0) { if (LOG.isErrorEnabled()) { LOG.error( Arrays.stream(indexResponse.getShardInfo().getFailures()) .map(ReplicationResponse.ShardInfo.Failure::toString) .collect(joining("\n"))); } throw new IndexException( format("Error indexing doc with id '%s' in index '%s'.", documentId, indexName)); } if (LOG.isDebugEnabled()) { LOG.debug("Indexed doc with id '{}' in index '{}'.", documentId, indexName); } }
ClientFacade implements Closeable { public void index(Index index, Document document) { if (LOG.isTraceEnabled()) { LOG.trace("Indexing doc with id '{}' in index '{}' ...", document.getId(), index.getName()); } String indexName = index.getName(); String documentId = document.getId(); XContentBuilder source = document.getContent(); IndexRequestBuilder indexRequest = client .prepareIndex() .setIndex(indexName) .setType(indexName) .setId(documentId) .setSource(source); IndexResponse indexResponse; try { indexResponse = indexRequest.get(); } catch (ResourceNotFoundException e) { LOG.error("", e); throw new UnknownIndexException(index.getName()); } catch (ElasticsearchException e) { LOG.debug("", e); throw new IndexException( format("Error indexing doc with id '%s' in index '%s'.", documentId, indexName)); } if (indexResponse.getShardInfo().getSuccessful() == 0) { if (LOG.isErrorEnabled()) { LOG.error( Arrays.stream(indexResponse.getShardInfo().getFailures()) .map(ReplicationResponse.ShardInfo.Failure::toString) .collect(joining("\n"))); } throw new IndexException( format("Error indexing doc with id '%s' in index '%s'.", documentId, indexName)); } if (LOG.isDebugEnabled()) { LOG.debug("Indexed doc with id '{}' in index '{}'.", documentId, indexName); } } }
ClientFacade implements Closeable { public void index(Index index, Document document) { if (LOG.isTraceEnabled()) { LOG.trace("Indexing doc with id '{}' in index '{}' ...", document.getId(), index.getName()); } String indexName = index.getName(); String documentId = document.getId(); XContentBuilder source = document.getContent(); IndexRequestBuilder indexRequest = client .prepareIndex() .setIndex(indexName) .setType(indexName) .setId(documentId) .setSource(source); IndexResponse indexResponse; try { indexResponse = indexRequest.get(); } catch (ResourceNotFoundException e) { LOG.error("", e); throw new UnknownIndexException(index.getName()); } catch (ElasticsearchException e) { LOG.debug("", e); throw new IndexException( format("Error indexing doc with id '%s' in index '%s'.", documentId, indexName)); } if (indexResponse.getShardInfo().getSuccessful() == 0) { if (LOG.isErrorEnabled()) { LOG.error( Arrays.stream(indexResponse.getShardInfo().getFailures()) .map(ReplicationResponse.ShardInfo.Failure::toString) .collect(joining("\n"))); } throw new IndexException( format("Error indexing doc with id '%s' in index '%s'.", documentId, indexName)); } if (LOG.isDebugEnabled()) { LOG.debug("Indexed doc with id '{}' in index '{}'.", documentId, indexName); } } ClientFacade(Client client); }
ClientFacade implements Closeable { public void index(Index index, Document document) { if (LOG.isTraceEnabled()) { LOG.trace("Indexing doc with id '{}' in index '{}' ...", document.getId(), index.getName()); } String indexName = index.getName(); String documentId = document.getId(); XContentBuilder source = document.getContent(); IndexRequestBuilder indexRequest = client .prepareIndex() .setIndex(indexName) .setType(indexName) .setId(documentId) .setSource(source); IndexResponse indexResponse; try { indexResponse = indexRequest.get(); } catch (ResourceNotFoundException e) { LOG.error("", e); throw new UnknownIndexException(index.getName()); } catch (ElasticsearchException e) { LOG.debug("", e); throw new IndexException( format("Error indexing doc with id '%s' in index '%s'.", documentId, indexName)); } if (indexResponse.getShardInfo().getSuccessful() == 0) { if (LOG.isErrorEnabled()) { LOG.error( Arrays.stream(indexResponse.getShardInfo().getFailures()) .map(ReplicationResponse.ShardInfo.Failure::toString) .collect(joining("\n"))); } throw new IndexException( format("Error indexing doc with id '%s' in index '%s'.", documentId, indexName)); } if (LOG.isDebugEnabled()) { LOG.debug("Indexed doc with id '{}' in index '{}'.", documentId, indexName); } } ClientFacade(Client client); void createIndex(Index index, IndexSettings indexSettings, Stream<Mapping> mappingStream); boolean indexesExist(Index index); void deleteIndex(Index index); void refreshIndexes(); long getCount(Index index); long getCount(QueryBuilder query, Index index); SearchHits search(QueryBuilder query, int from, int size, List<Index> indexes); SearchHits search(QueryBuilder query, int from, int size, Sort sort, Index index); Aggregations aggregate(
      List<AggregationBuilder> aggregations, QueryBuilder query, Index index); Explanation explain(SearchHit searchHit, QueryBuilder query); void index(Index index, Document document); void deleteById(Index index, Document document); void processDocumentActions(Stream<DocumentAction> documentActions); @Override void close(); }
ClientFacade implements Closeable { public void index(Index index, Document document) { if (LOG.isTraceEnabled()) { LOG.trace("Indexing doc with id '{}' in index '{}' ...", document.getId(), index.getName()); } String indexName = index.getName(); String documentId = document.getId(); XContentBuilder source = document.getContent(); IndexRequestBuilder indexRequest = client .prepareIndex() .setIndex(indexName) .setType(indexName) .setId(documentId) .setSource(source); IndexResponse indexResponse; try { indexResponse = indexRequest.get(); } catch (ResourceNotFoundException e) { LOG.error("", e); throw new UnknownIndexException(index.getName()); } catch (ElasticsearchException e) { LOG.debug("", e); throw new IndexException( format("Error indexing doc with id '%s' in index '%s'.", documentId, indexName)); } if (indexResponse.getShardInfo().getSuccessful() == 0) { if (LOG.isErrorEnabled()) { LOG.error( Arrays.stream(indexResponse.getShardInfo().getFailures()) .map(ReplicationResponse.ShardInfo.Failure::toString) .collect(joining("\n"))); } throw new IndexException( format("Error indexing doc with id '%s' in index '%s'.", documentId, indexName)); } if (LOG.isDebugEnabled()) { LOG.debug("Indexed doc with id '{}' in index '{}'.", documentId, indexName); } } ClientFacade(Client client); void createIndex(Index index, IndexSettings indexSettings, Stream<Mapping> mappingStream); boolean indexesExist(Index index); void deleteIndex(Index index); void refreshIndexes(); long getCount(Index index); long getCount(QueryBuilder query, Index index); SearchHits search(QueryBuilder query, int from, int size, List<Index> indexes); SearchHits search(QueryBuilder query, int from, int size, Sort sort, Index index); Aggregations aggregate(
      List<AggregationBuilder> aggregations, QueryBuilder query, Index index); Explanation explain(SearchHit searchHit, QueryBuilder query); void index(Index index, Document document); void deleteById(Index index, Document document); void processDocumentActions(Stream<DocumentAction> documentActions); @Override void close();  }