/** Add a mapping to Elasticsearch. This will only be called if a new index has been created */ public void addMapping(XContentBuilder mapping) { esResource .getClient() .admin() .indices() .preparePutMapping(index) .setType(type) .setSource(mapping) .execute() .actionGet(); }
SharedElasticsearchResource elasticsearchResource = new SharedElasticsearchResource(); }); elasticsearchResource.initialize(esSpecifier, Maps.newHashMap()); assertNotNull(elasticsearchResource.getClient()); .getClient() .admin() .cluster() assertEquals(elasticsearch.getClusterName(), actionGet.getClusterName().value()); elasticsearchResource.destroy();
@Before public void setUp() throws Exception { elasticsearch = new EmbeddedElasticsearch5(); elasticsearchResource = new SharedElasticsearchResource(); CustomResourceSpecifier_impl esSpecifier = new CustomResourceSpecifier_impl(); esSpecifier.setParameters( new Parameter[] { new Parameter_impl(PARAM_CLUSTER, elasticsearch.getClusterName()), new Parameter_impl(PARAM_PORT, Integer.toString(elasticsearch.getTransportPort())) }); elasticsearchResource.initialize(esSpecifier, Maps.newHashMap()); history = new ElasticsearchHistory(elasticsearchResource); history.initialize(new CustomResourceSpecifier_impl(), Maps.newHashMap()); }
/** Add a mapping to Elasticsearch. This will only be called if a new index has been created */ public void addMapping(XContentBuilder mapping) { esResource .getClient() .admin() .indices() .preparePutMapping(index) .setType(type) .setSource(mapping) .execute() .actionGet(); }
@Override public void addMapping(XContentBuilder mapping) { esResource .getClient() .admin() .indices() .preparePutMapping(index) .setType(type) .setSource(mapping) .execute() .actionGet(); }
private void addMapping(String type, XContentBuilder mapping) { esResource .getClient() .admin() .indices() .preparePutMapping(index) .setType(type) .setSource(mapping) .execute() .actionGet(); }
private void save(final String index, final String type, final Object o, final String id) { try { esResource .getClient() .prepareIndex(index, type, id) .setSource(MAPPER.writeValueAsBytes(o), XContentType.JSON) .execute() .actionGet(); } catch (final Exception ee) { getMonitor().error("Couldn't persist entity to Elasticsearch", ee); } }
/** * Create an index in Elasticsearch. If necessary, this function should check whether a new index * is required. * * @return true if a new index has been created, false otherwise */ public boolean createIndex() { if (!esResource .getClient() .admin() .indices() .exists(Requests.indicesExistsRequest(index)) .actionGet() .isExists()) { esResource .getClient() .admin() .indices() .create(Requests.createIndexRequest(index)) .actionGet(); return true; } return false; }
/** Add the document (provided as JSON) to Elasticsearch, using the id provided. */ public void addDocument(String id, Map<String, Object> json) { try { esResource.getClient().prepareIndex(index, type, id).setSource(json).execute().actionGet(); } catch (ElasticsearchException ee) { getMonitor().error("Couldn't persist document to Elasticsearch", ee); } }
/** * Create an index in Elasticsearch. If necessary, this function should check whether a new index * is required. * * @return true if a new index has been created, false otherwise */ public boolean createIndex() { if (!esResource .getClient() .admin() .indices() .exists(Requests.indicesExistsRequest(index)) .actionGet() .isExists()) { esResource .getClient() .admin() .indices() .create(Requests.createIndexRequest(index)) .actionGet(); return true; } return false; }
@Override public void addDocument(String id, Map<String, Object> json) { try { esResource.getClient().prepareIndex(index, type, id).setSource(json).execute().actionGet(); } catch (ElasticsearchException ee) { getMonitor().error("Couldn't persist document to Elasticsearch", ee); } } }
/** Add the document (provided as JSON) to Elasticsearch, using the id provided. */ public void addDocument(String id, Map<String, Object> json) { try { esResource.getClient().prepareIndex(index, type, id).setSource(json).execute().actionGet(); } catch (ElasticsearchException ee) { getMonitor().error("Couldn't persist document to Elasticsearch", ee); } }
private <T> void save( final String index, final String type, final Collection<T> collection, final Function<T, String> idGenerator, final Function<T, String> parentIdGenerator) { if (collection.isEmpty()) { return; } try { final BulkRequestBuilder bulkBuilder = esResource.getClient().prepareBulk(); for (final T t : collection) { final String source = MAPPER.writeValueAsString(t); bulkBuilder.add( esResource .getClient() .prepareIndex(index, type, idGenerator.apply(t)) .setSource(source, XContentType.JSON) .setParent(parentIdGenerator.apply(t))); } final RestStatus status = bulkBuilder.execute().actionGet().status(); if (!status.equals(RestStatus.OK)) { throw new BaleenException("Rest call failed with code " + status.getStatus()); } } catch (final Exception ee) { getMonitor().error("Couldn't persist to Elasticsearch", ee); } }
@Override protected ElasticsearchDocumentHistory loadExistingDocumentHistory(String documentId) throws BaleenException { try { GetResponse response = new GetRequestBuilder(elasticsearch.getClient(), GetAction.INSTANCE) .setId(documentId) .setIndex(esIndex) .setType(esType) .get(); if (!response.isExists() || response.isSourceEmpty()) { // If we don't have any data, then let parent implementation create a new history return null; } else { ESHistory esh = mapper.readValue(response.getSourceAsBytes(), ESHistory.class); if (esh == null) { return new ElasticsearchDocumentHistory( this, documentId, new LinkedBlockingDeque<HistoryEvent>(Collections.emptyList())); } else { return new ElasticsearchDocumentHistory( this, documentId, new LinkedBlockingDeque<HistoryEvent>(esh.getEvents())); } } } catch (IOException e) { throw new BaleenException(e); } } }
@Override public void closeHistory(String documentId) { ElasticsearchDocumentHistory dh = getCachedHistoryIfPresent(documentId); if (dh == null) { LOGGER.warn( "Attempt to close a document {} which is not in cache, thus can't be persisted", documentId); return; } try { byte[] source = mapper.writeValueAsBytes(new ESHistory(documentId, dh.getAllHistory())); new IndexRequestBuilder(elasticsearch.getClient(), IndexAction.INSTANCE) .setIndex(documentId) .setIndex(esIndex) .setType(esType) .setId(documentId) .setSource(source, XContentType.JSON) .get(); } catch (JsonProcessingException e) { LOGGER.warn("Unable to convert history to source, so can't be persisted {}", documentId, e); } super.closeHistory(documentId); }
private void setMapping(final String index) throws IOException { // Create index (if needed) final IndicesAdminClient indices = esResource.getClient().admin().indices(); if (!indices.exists(Requests.indicesExistsRequest(index)).actionGet().isExists()) { // We must submit all the mappings at once for parent child indices .prepareCreate(index) .addMapping(documentType, createDocumentMapping(XContentFactory.jsonBuilder())) .addMapping(mentionType, createMentionMapping(XContentFactory.jsonBuilder())) .addMapping(entityType, createEntityMapping(XContentFactory.jsonBuilder())) .addMapping(relationType, createRelationMapping(XContentFactory.jsonBuilder())) .execute() .actionGet(); } }
private void delete( final String index, final String type, final String field, final String fieldValue) { try { DeleteByQueryAction.INSTANCE .newRequestBuilder(esResource.getClient()) .filter( QueryBuilders.boolQuery() .must(QueryBuilders.matchQuery(field, fieldValue)) .must(QueryBuilders.typeQuery(type))) .source(index) .get(); } catch (final Exception ee) { getMonitor().error("Couldn't delete from Elasticsearch", ee); } }