@Override public Status insert(String table, String key, Map<String, ByteIterator> values) { try (XContentBuilder doc = jsonBuilder()) { doc.startObject(); for (final Entry<String, String> entry : StringByteIterator.getStringMap(values).entrySet()) { doc.field(entry.getKey(), entry.getValue()); } doc.field(KEY, key); doc.endObject(); final IndexResponse indexResponse = client.prepareIndex(indexKey, table).setSource(doc).get(); if (indexResponse.getResult() != DocWriteResponse.Result.CREATED) { return Status.ERROR; } if (!isRefreshNeeded) { synchronized (this) { isRefreshNeeded = true; } } return Status.OK; } catch (final Exception e) { e.printStackTrace(); return Status.ERROR; } }
@Override public Status update(final String table, final String key, final Map<String, ByteIterator> values) { try { final SearchResponse response = search(table, key); if (response.getHits().totalHits == 0) { return Status.NOT_FOUND; } final SearchHit hit = response.getHits().getAt(0); for (final Entry<String, String> entry : StringByteIterator.getStringMap(values).entrySet()) { hit.getSource().put(entry.getKey(), entry.getValue()); } final IndexResponse indexResponse = client.prepareIndex(indexKey, table, hit.getId()).setSource(hit.getSource()).get(); if (indexResponse.getResult() != DocWriteResponse.Result.UPDATED) { return Status.ERROR; } if (!isRefreshNeeded) { synchronized (this) { isRefreshNeeded = true; } } return Status.OK; } catch (final Exception e) { e.printStackTrace(); return Status.ERROR; } }
if (mapping.getParent() == null) { bulkRequestBuilder.add(transportClient .prepareIndex(mapping.get_index(), mapping.get_type(), idVal.toString()) .setSource(esFieldData)); } else { .add(transportClient.prepareIndex(mapping.get_index(), mapping.get_type()) .setSource(esFieldData)); } else {
/** * 插入数据 * * @param mapping * @param pkVal * @param esFieldData * @return */ public boolean insert(ESMapping mapping, Object pkVal, Map<String, Object> esFieldData) { BulkRequestBuilder bulkRequestBuilder = transportClient.prepareBulk(); if (mapping.get_id() != null) { bulkRequestBuilder .add(transportClient.prepareIndex(mapping.get_index(), mapping.get_type(), pkVal.toString()) .setSource(esFieldData)); } else { SearchResponse response = transportClient.prepareSearch(mapping.get_index()) .setTypes(mapping.get_type()) .setQuery(QueryBuilders.termQuery(mapping.getPk(), pkVal)) .setSize(MAX_BATCH_SIZE) .get(); for (SearchHit hit : response.getHits()) { bulkRequestBuilder .add(transportClient.prepareDelete(mapping.get_index(), mapping.get_type(), hit.getId())); } bulkRequestBuilder .add(transportClient.prepareIndex(mapping.get_index(), mapping.get_type()).setSource(esFieldData)); } return commitBulkRequest(bulkRequestBuilder); }
/** * @param index 索引 * @param type 类型 * @return d */ public IndexRequestBuilder indexRequest(String index, String type) { return client.prepareIndex(index, type); }
@Override protected void onPersist(EntityMetadata entityMetadata, Object entity, Object id, List<RelationHolder> rlHolders) { try { Map<String, Object> values = new HashMap<String, Object>(); MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata() .getMetamodel(entityMetadata.getPersistenceUnit()); EntityType entityType = metaModel.entity(entityMetadata.getEntityClazz()); String keyAsString = getKeyAsString(id, entityMetadata, metaModel); addSource(entity, values, entityType); addRelations(rlHolders, values); addDiscriminator(values, entityType); IndexResponse response = txClient .prepareIndex(entityMetadata.getSchema().toLowerCase(), entityMetadata.getTableName(), keyAsString) .setSource(values).setRefresh(isRefreshIndexes()).execute().actionGet(); assert response.getId() != null; } finally { // Nothing as of now. } }
/** * @param index 索引 * @param type 类型 * @param id d * @return d */ public IndexRequestBuilder indexRequest(String index, String type, String id) { return client.prepareIndex(index, type, id); }
public void index(TodoView todoView) { try { IndexResponse ir = transportClient .prepareIndex(TodoView.INDEX, TodoView.TYPE, todoView.getId()) .setSource(objectMapper.writeValueAsString(todoView), XContentType.JSON) .get(); } catch (JsonProcessingException e) { throw new RuntimeException(e); } }
public void index(TodoView todoView) { try { IndexResponse ir = transportClient .prepareIndex(TodoView.INDEX, TodoView.TYPE, todoView.getId()) .setSource(objectMapper.writeValueAsString(todoView), XContentType.JSON) .get(); } catch (JsonProcessingException e) { throw new RuntimeException(e); } }
@Override public void insertById(String index, String type, String id, String jsonStr) { transportClient.prepareIndex(index, type,id).setSource(jsonStr, XContentType.JSON).get(); }
@Override public void insertById(String index, String type, String id, Map<String, Object> dataMap) { transportClient.prepareIndex(index, type, id).setSource(dataMap).get(); }
private boolean insert(String id, Map source) { Preconditions.checkNotNull(source, "source must not be null."); logger.info("insert data : {}", JSON.toJSONString(source)); IndexRequestBuilder builder = client.prepareIndex(index, type).setSource(source); if (StringUtils.isNotBlank(id)) { builder.setId(id); } IndexResponse response = builder.get(); logger.info("insert response : {}", response.toString()); return response.isCreated(); }
@Override public void update(String index, String type, List<ElasticsearchMetadata.EsRowData> esRowDataList) { esRowDataList.forEach(esRowData -> { String idColumn = esRowData.getIdColumn(); Map<String, Object> dataMap = esRowData.getRowData(); String id = (String) esRowData.getRowData().get(idColumn); transportClient.prepareIndex(index, type, id).setSource(dataMap).get(); logger.info("Update into elasticsearch ====> {} ", index + "." + type + "." + id); }); }
@Override public void insertById(final String index, final String type, final List<ElasticsearchMetadata.EsRowData> esRowDataList) { esRowDataList.forEach(esRowData -> { String idColumn = esRowData.getIdColumn(); Map<String, Object> dataMap = esRowData.getRowData(); String id = (String) esRowData.getRowData().get(idColumn); transportClient.prepareIndex(index, type, id).setSource(dataMap).get(); logger.info("Insert into elasticsearch ====> {} ", index + "." + type + "." + id); }); }
@Override public boolean add(String indexName, String type, String id, String json) { IndexResponse response = client.prepareIndex(indexName, type, id).setSource(json, XContentType.JSON).execute().actionGet(); return true; }
/** * 向ES中添加数据 * @param index 索引 * @param type 类型 * @param param 要添加的数据,Map类型 * @return */ public boolean insert(String index,String type,Map<String,Object> param){ return client.prepareIndex(index,type).setSource(param).get().status().name() .equals(ESRestStatus.CREATED.name()); }
@Override protected void addDocument(SearchDocument doc) throws IOException { ElasticsearchDocument esDoc = (ElasticsearchDocument)doc; doIndexRequest(client.prepareIndex(esDoc.getIndex(), esDoc.getType(), esDoc.getId()).setSource( esDoc.getSource())); }
@Test public void testCreateIndexRequest() { ElasticMetaObject obj = new ElasticMetaObject(() -> "type"); obj.addProperty(new ElasticMetaProperty("cluster.id", "system_ou/plugins", Sets.newHashSet(String.class))); obj.addProperty(new ElasticMetaProperty("type", "plugins", Sets.newHashSet(String.class))); this.provider.createIndexRequest(obj); verify(transportClient).prepareIndex(eq("system_ou_plugins"), eq("plugins")); }
private IndexRequest getPropertyDocumentIndexRequest(IndexInfo indexInfo, Element element, Property property) throws IOException { XContentBuilder jsonBuilder = buildJsonContentFromProperty(indexInfo, property); if (jsonBuilder == null) { return null; } String id = getChildDocId(element, property); //LOGGER.debug(jsonBuilder.string()); IndexRequestBuilder builder = getClient().prepareIndex(indexInfo.getIndexName(), PROPERTY_TYPE, id); builder = builder.setParent(element.getId()); builder = builder.setSource(jsonBuilder); return builder.request(); }
private IndexRequest getPropertyDocumentIndexRequest(IndexInfo indexInfo, Element element, Property property) throws IOException { XContentBuilder jsonBuilder = buildJsonContentFromProperty(indexInfo, property); if (jsonBuilder == null) { return null; } String id = getChildDocId(element, property); //LOGGER.debug(jsonBuilder.string()); IndexRequestBuilder builder = getClient().prepareIndex(indexInfo.getIndexName(), PROPERTY_TYPE, id); builder = builder.setParent(element.getId()); builder = builder.setSource(jsonBuilder); return builder.request(); }