@Override public String index(IndexQuery query) { String documentId = prepareIndex(query).execute().actionGet().getId(); // We should call this because we are not going through a mapper. if (query.getObject() != null) { setPersistentEntityId(query.getObject(), documentId); } return documentId; }
private IndexRequestBuilder prepareIndex(IndexQuery query) { try { String indexName = isBlank(query.getIndexName()) ? retrieveIndexNameFromPersistentEntity(query.getObject().getClass())[0] : query.getIndexName(); String type = isBlank(query.getType()) ? retrieveTypeFromPersistentEntity(query.getObject().getClass())[0] : query.getType(); IndexRequestBuilder indexRequestBuilder = null; if (query.getObject() != null) { String entityId = null; if (isDocument(query.getObject().getClass())) { entityId = getPersistentEntityId(query.getObject()); } // If we have a query id and a document id, do not ask ES to generate one. if (query.getId() != null && entityId != null) { indexRequestBuilder = client.prepareIndex(indexName, type, query.getId()); } else { indexRequestBuilder = client.prepareIndex(indexName, type); } indexRequestBuilder.setSource( resultsMapper.getEntityMapper().mapToString(query.getObject())); } else if (query.getSource() != null) { indexRequestBuilder = client.prepareIndex(indexName, type, query.getId()).setSource(query.getSource()); } else { throw new ElasticsearchException( "object or source is null, failed to index the document [id: " + query.getId() + "]"); } if (query.getVersion() != null) { indexRequestBuilder.setVersion(query.getVersion()); indexRequestBuilder.setVersionType(EXTERNAL); } if (query.getParentId() != null) { indexRequestBuilder.setParent(query.getParentId()); } return indexRequestBuilder; } catch (IOException e) { throw new ElasticsearchException( "failed to index the document [id: " + query.getId() + "]", e); } }