org.elasticsearch.action.get.GetResponse Java Examples

The following examples show how to use org.elasticsearch.action.get.GetResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ADStateManager.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
private ActionListener<GetResponse> onGetResponse(String adID, ActionListener<Optional<AnomalyDetector>> listener) {
    return ActionListener.wrap(response -> {
        if (response == null || !response.isExists()) {
            listener.onResponse(Optional.empty());
            return;
        }

        String xc = response.getSourceAsString();
        LOG.info("Fetched anomaly detector: {}", xc);

        try (
            XContentParser parser = XContentType.JSON.xContent().createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, xc)
        ) {
            ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
            AnomalyDetector detector = AnomalyDetector.parse(parser, response.getId());
            currentDetectors.put(adID, new SimpleEntry<>(detector, clock.instant()));
            listener.onResponse(Optional.of(detector));
        } catch (Exception t) {
            LOG.error("Fail to parse detector {}", adID);
            LOG.error("Stack trace:", t);
            listener.onResponse(Optional.empty());
        }
    }, listener::onFailure);
}
 
Example #2
Source File: ScriptService.java    From elasticsearch-river-web with Apache License 2.0 6 votes vote down vote up
private String getScriptContent(final String lang, final String script, final ScriptType scriptType) {
    switch (scriptType) {
    case INLINE:
        return script;
    case FILE:
        if (Files.exists(Paths.get(script))) {
            return FileUtil.readText(new File(script));
        } else {
            return FileUtil.readText(script);
        }
    case INDEXED:
        final GetResponse response = esClient.prepareGet(SCRIPT_INDEX, lang, script).execute().actionGet();
        if (!response.isExists()) {
            throw new ScriptExecutionException("/" + SCRIPT_INDEX + "/" + lang + "/" + script + " does not exist.");
        }
        final Map<String, Object> source = response.getSource();
        if (source != null) {
            return (String) source.get("script");
        }
        break;
    default:
        break;
    }
    return null;
}
 
Example #3
Source File: AnomalyDetectorProfileRunner.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
private ActionListener<GetResponse> onGetDetectorResponse(
    MultiResponsesDelegateActionListener<DetectorProfile> listener,
    String detectorId,
    Set<ProfileName> profiles
) {
    return ActionListener.wrap(getResponse -> {
        if (getResponse != null && getResponse.isExists()) {
            DetectorProfile profile = new DetectorProfile();
            if (profiles.contains(ProfileName.STATE)) {
                profile.setState(DetectorState.DISABLED);
            }
            listener.respondImmediately(profile);
        } else {
            listener.failImmediately(FAIL_TO_FIND_DETECTOR_MSG + detectorId);
        }
    }, exception -> { listener.failImmediately(FAIL_TO_FIND_DETECTOR_MSG + detectorId, exception); });
}
 
Example #4
Source File: TraceServiceElasticsearch.java    From hawkular-apm with Apache License 2.0 6 votes vote down vote up
@Override
public Trace getFragment(String tenantId, String id) {
    Trace ret = null;

    GetResponse response = client.getClient().prepareGet(
            client.getIndex(tenantId), TRACE_TYPE, id).setRouting(id)
            .execute()
            .actionGet();
    if (!response.isSourceEmpty()) {
        try {
            ret = mapper.readValue(response.getSourceAsString(), Trace.class);
        } catch (Exception e) {
            msgLog.errorFailedToParse(e);
        }
    }

    if (msgLog.isTraceEnabled()) {
        msgLog.tracef("Get fragment with id[%s] is: %s", id, ret);
    }

    return ret;
}
 
Example #5
Source File: Test.java    From dht-spider with MIT License 6 votes vote down vote up
public static void get(Map<String, Object> m) throws Exception{
    GetRequest getRequest = new GetRequest(
            "haha",
            "doc",
            "2");
    String[] includes = new String[]{"message","user","*Date"};
    String[] excludes = Strings.EMPTY_ARRAY;
    FetchSourceContext fetchSourceContext =
            new FetchSourceContext(true, includes, excludes);
    getRequest.fetchSourceContext(fetchSourceContext);

    GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
    String index = getResponse.getIndex();
    String type = getResponse.getType();
    String id = getResponse.getId();
    if (getResponse.isExists()) {
        long version = getResponse.getVersion();
        String sourceAsString = getResponse.getSourceAsString();
        Map<String, Object> sourceAsMap = getResponse.getSourceAsMap();
        System.out.println(sourceAsMap);
    } else {

    }
}
 
Example #6
Source File: AuditLoginsCounterBolt.java    From Kafka-Storm-ElasticSearch with Apache License 2.0 6 votes vote down vote up
private int getCounterValue(String host_user){

        try{
        	GetResponse response = client.prepareGet(index, type, host_user)
        	        .execute()
        	        .actionGet();
        	
        	Map<String, Object> test = response.getSource();
        	
        	return (int)test.get("counter");
        }
        catch (Exception e){
        	System.out.println("Error in get elasticSearch get: maybe index is still not created");
        	return 0;
        }
    }
 
Example #7
Source File: ElasticsearchDataModel.java    From elasticsearch-taste with Apache License 2.0 6 votes vote down vote up
private void createUserID(final long userID) {
    final GetResponse getResponse = client
            .prepareGet(userIndex, userType, Long.toString(userID))
            .setRefresh(true).execute().actionGet();
    if (!getResponse.isExists()) {
        final Map<String, Object> source = new HashMap<>();
        source.put("system_id", Long.toString(userID));
        source.put(userIdField, userID);
        source.put(timestampField, new Date());
        final IndexResponse response = client
                .prepareIndex(userIndex, userType, Long.toString(userID))
                .setSource(source).setRefresh(true).execute().actionGet();
        if (!response.isCreated()) {
            throw new TasteException("Failed to create " + source);
        }
    }
}
 
Example #8
Source File: ElasticsearchSpewerTest.java    From datashare with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void test_metadata() throws Exception {
    Path path = get(getClass().getResource("/docs/a/b/c/doc.txt").getPath());
    TikaDocument document = new Extractor().extract(path);

    spewer.write(document);

    GetResponse documentFields = es.client.get(new GetRequest(TEST_INDEX, "doc", document.getId()));
    assertThat(documentFields.getSourceAsMap()).includes(
            entry("contentEncoding", "ISO-8859-1"),
            entry("contentType", "text/plain"),
            entry("nerTags", new ArrayList<>()),
            entry("contentLength", 45),
            entry("status", "INDEXED"),
            entry("path", path.toString()),
            entry("dirname", path.getParent().toString())
    );
}
 
Example #9
Source File: UITemplateManagementEsDAO.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public TemplateChangeStatus addTemplate(final DashboardSetting setting) throws IOException {
    try {
        final UITemplate.Builder builder = new UITemplate.Builder();
        final UITemplate uiTemplate = setting.toEntity();

        final GetResponse response = getClient().get(UITemplate.INDEX_NAME, uiTemplate.id());
        if (response.isExists()) {
            return TemplateChangeStatus.builder().status(false).message("Template exists").build();
        }

        XContentBuilder xContentBuilder = map2builder(builder.data2Map(uiTemplate));
        getClient().forceInsert(UITemplate.INDEX_NAME, uiTemplate.id(), xContentBuilder);
        return TemplateChangeStatus.builder().status(true).build();
    } catch (IOException e) {
        log.error(e.getMessage(), e);
        return TemplateChangeStatus.builder().status(false).message("Can't add a new template").build();
    }
}
 
Example #10
Source File: ElasticSearchTypeImpl.java    From core-ng-project with Apache License 2.0 6 votes vote down vote up
@Override
public Optional<T> get(GetRequest request) {
    var watch = new StopWatch();
    String index = request.index == null ? this.index : request.index;
    int hits = 0;
    try {
        var getRequest = new org.elasticsearch.action.get.GetRequest(index, request.id);
        GetResponse response = elasticSearch.client().get(getRequest, RequestOptions.DEFAULT);
        if (!response.isExists()) return Optional.empty();
        hits = 1;
        return Optional.of(mapper.fromJSON(response.getSourceAsBytes()));
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } finally {
        long elapsed = watch.elapsed();
        ActionLogContext.track("elasticsearch", elapsed, hits, 0);
        logger.debug("get, index={}, id={}, elapsed={}", index, request.id, elapsed);
        checkSlowOperation(elapsed);
    }
}
 
Example #11
Source File: OpenShiftRestResponseTest.java    From openshift-elasticsearch-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void testMGetResponse() throws Exception {
    
    String body = "{\"docs\":[{\"_index\":\"%1$s\",\"_type\":\"config\",\"_id\":\"0\",\"found\":true"
            + "},{\"_index\":\"%1$s\",\"_type\":\"config\","
            + "\"_id\":\"1\",\"found\":true}]}";
    MultiGetItemResponse [] items = new MultiGetItemResponse [2];
    for (int i = 0; i < items.length; i++) {
        String itemBody =  "{\"_index\":\"%1$s\",\"_type\":\"config\",\"_id\":\"" + i + "\",\"found\":true}";
        XContentParser parser = givenContentParser(itemBody);
        items[i] = new MultiGetItemResponse(GetResponse.fromXContent(parser), null);
    }
    
    MultiGetResponse actionResponse = new MultiGetResponse(items);
    
    OpenShiftRestResponse osResponse = whenCreatingResponseResponse(actionResponse);
    thenResponseShouldBeModified(osResponse, body);
}
 
Example #12
Source File: ESIndex.java    From pyramid with Apache License 2.0 6 votes vote down vote up
public Object getField(String id, String field){
        GetResponse response = client.prepareGet(this.indexName, this.documentType, id)
                .setFetchSource(field,null)
//                .setStoredFields(field)
                .execute()
                .actionGet();
        Object res = null;
        if (response==null){
            if (logger.isWarnEnabled()){
                logger.warn("no response from document "+id+" when fetching field "+field+"!");
            }
            return null;
        }else {
            res = response.getSourceAsMap().get(field);
            if (res==null){
                if (logger.isWarnEnabled()) {
                    logger.warn("document " + id + " has no field " + field + "!");
                }
            }
        }

        return res;
//        return response.getField(field).getValue();
    }
 
Example #13
Source File: TasteSearchRestAction.java    From elasticsearch-taste with Apache License 2.0 6 votes vote down vote up
private Map<String, Object> getObjectMap(final Client client,
        final String prefix, final String index, final String type,
        final String id) {
    try {
        return cache.get(prefix + id, () -> {
            final GetResponse response = client.prepareGet(index, type, id)
                    .execute().actionGet();
            if (response.isExists()) {
                return response.getSource();
            }
            return null;
        });
    } catch (final ExecutionException e) {
        throw new TasteException("Failed to get data for " + index + "/"
                + type + "/" + id, e);
    }
}
 
Example #14
Source File: ElasticsearchConsolePersistence.java    From foxtrot with Apache License 2.0 6 votes vote down vote up
@Override
public ConsoleV2 getV2(String id) {
    try {
        GetResponse result = connection.getClient()
                .prepareGet()
                .setIndex(INDEX_V2)
                .setType(TYPE)
                .setId(id)
                .execute()
                .actionGet();
        if (!result.isExists()) {
            return null;
        }
        return mapper.readValue(result.getSourceAsBytes(), ConsoleV2.class);
    } catch (Exception e) {
        throw new ConsolePersistenceException(id, "console get failed", e);
    }
}
 
Example #15
Source File: ElasticSearchDAOV6.java    From conductor with Apache License 2.0 6 votes vote down vote up
@Override
public String get(String workflowInstanceId, String fieldToGet) {
    String docType = StringUtils.isBlank(docTypeOverride) ? WORKFLOW_DOC_TYPE : docTypeOverride;
    GetRequest request = new GetRequest(workflowIndexName, docType, workflowInstanceId)
            .fetchSourceContext(new FetchSourceContext(true, new String[]{fieldToGet}, Strings.EMPTY_ARRAY));
    GetResponse response = elasticSearchClient.get(request).actionGet();

    if (response.isExists()) {
        Map<String, Object> sourceAsMap = response.getSourceAsMap();
        if (sourceAsMap.get(fieldToGet) != null) {
            return sourceAsMap.get(fieldToGet).toString();
        }
    }

    LOGGER.debug("Unable to find Workflow: {} in ElasticSearch index: {}.", workflowInstanceId, workflowIndexName);
    return null;
}
 
Example #16
Source File: S3River.java    From es-amazon-s3-river with Apache License 2.0 6 votes vote down vote up
private boolean isStarted(){
   // Refresh index before querying it.
   client.admin().indices().prepareRefresh("_river").execute().actionGet();
   GetResponse isStartedGetResponse = client.prepareGet("_river", riverName().name(), "_s3status").execute().actionGet();
   try{
      if (!isStartedGetResponse.isExists()){
         XContentBuilder xb = jsonBuilder().startObject()
               .startObject("amazon-s3")
                  .field("feedname", feedDefinition.getFeedname())
                  .field("status", "STARTED").endObject()
               .endObject();
         client.prepareIndex("_river", riverName.name(), "_s3status").setSource(xb).execute();
         return true;
      } else {
         String status = (String)XContentMapValues.extractValue("amazon-s3.status", isStartedGetResponse.getSourceAsMap());
         if ("STOPPED".equals(status)){
            return false;
         }
      }
   } catch (Exception e){
      logger.warn("failed to get status for " + riverName().name() + ", throttling....", e);
   }
   return true;
}
 
Example #17
Source File: ElasticSearchDAOV5.java    From conductor with Apache License 2.0 6 votes vote down vote up
@Override
public String get(String workflowInstanceId, String fieldToGet) {
    GetRequest request = new GetRequest(indexName, WORKFLOW_DOC_TYPE, workflowInstanceId)
        .fetchSourceContext(
            new FetchSourceContext(true, new String[]{fieldToGet}, Strings.EMPTY_ARRAY));
    GetResponse response = elasticSearchClient.get(request).actionGet();

    if (response.isExists()) {
        Map<String, Object> sourceAsMap = response.getSourceAsMap();
        if (sourceAsMap.containsKey(fieldToGet)) {
            return sourceAsMap.get(fieldToGet).toString();
        }
    }

    logger.info("Unable to find Workflow: {} in ElasticSearch index: {}.", workflowInstanceId, indexName);
    return null;
}
 
Example #18
Source File: UserSyncSingleTest.java    From canal with Apache License 2.0 5 votes vote down vote up
/**
 * 单表插入
 */
@Test
public void test01() {
    Dml dml = new Dml();
    dml.setDestination("example");
    dml.setTs(new Date().getTime());
    dml.setType("INSERT");
    dml.setDatabase("mytest");
    dml.setTable("user");
    List<Map<String, Object>> dataList = new ArrayList<>();
    Map<String, Object> data = new LinkedHashMap<>();
    dataList.add(data);
    data.put("id", 1L);
    data.put("name", "Eric");
    data.put("role_id", 1L);
    data.put("c_time", new Date());
    dml.setData(dataList);

    String database = dml.getDatabase();
    String table = dml.getTable();
    Map<String, ESSyncConfig> esSyncConfigs = esAdapter.getDbTableEsSyncConfig().get(database + "-" + table);

    esAdapter.getEsSyncService().sync(esSyncConfigs.values(), dml);

    GetResponse response = esAdapter.getEsConnection()
        .getTransportClient()
        .prepareGet("mytest_user", "_doc", "1")
        .get();
    Assert.assertEquals("Eric", response.getSource().get("_name"));
}
 
Example #19
Source File: RoleSyncJoinOneTest.java    From canal with Apache License 2.0 5 votes vote down vote up
/**
 * 非子查询从表删除
 */
@Test
public void test04() {
    DataSource ds = DatasourceConfig.DATA_SOURCES.get("defaultDS");
    Common.sqlExe(ds, "delete from role where id=1");

    Dml dml = new Dml();
    dml.setDestination("example");
    dml.setTs(new Date().getTime());
    dml.setType("DELETE");
    dml.setDatabase("mytest");
    dml.setTable("role");
    List<Map<String, Object>> dataList = new ArrayList<>();
    Map<String, Object> data = new LinkedHashMap<>();
    dataList.add(data);
    data.put("id", 1L);
    data.put("role_name", "admin");

    dml.setData(dataList);

    String database = dml.getDatabase();
    String table = dml.getTable();
    Map<String, ESSyncConfig> esSyncConfigs = esAdapter.getDbTableEsSyncConfig().get(database + "-" + table);

    esAdapter.getEsSyncService().sync(esSyncConfigs.values(), dml);

    GetResponse response = esAdapter.getEsConnection()
        .getTransportClient()
        .prepareGet("mytest_user", "_doc", "1")
        .get();
    Assert.assertNull(response.getSource().get("_role_name"));
}
 
Example #20
Source File: ElasticsearchRestResource.java    From camel-quarkus with Apache License 2.0 5 votes vote down vote up
@Path("/get")
@GET
@Produces(MediaType.TEXT_PLAIN)
public Response getData(@QueryParam("indexId") String indexId) throws Exception {
    GetResponse response = producerTemplate
            .requestBody("elasticsearch-rest://elasticsearch?operation=GetById&indexName=test", indexId, GetResponse.class);

    if (response.getSource() == null) {
        return Response.status(404).build();
    }
    return Response.ok().entity(response.getSource().get("test-key")).build();
}
 
Example #21
Source File: RoleSyncJoinOneTest.java    From canal with Apache License 2.0 5 votes vote down vote up
/**
 * 非子查询从表更新
 */
@Test
public void test02() {
    DataSource ds = DatasourceConfig.DATA_SOURCES.get("defaultDS");
    Common.sqlExe(ds, "update role set role_name='admin2' where id=1");

    Dml dml = new Dml();
    dml.setDestination("example");
    dml.setTs(new Date().getTime());
    dml.setType("UPDATE");
    dml.setDatabase("mytest");
    dml.setTable("role");
    List<Map<String, Object>> dataList = new ArrayList<>();
    Map<String, Object> data = new LinkedHashMap<>();
    dataList.add(data);
    data.put("id", 1L);
    data.put("role_name", "admin2");
    dml.setData(dataList);

    List<Map<String, Object>> oldList = new ArrayList<>();
    Map<String, Object> old = new LinkedHashMap<>();
    oldList.add(old);
    old.put("role_name", "admin");
    dml.setOld(oldList);

    String database = dml.getDatabase();
    String table = dml.getTable();
    Map<String, ESSyncConfig> esSyncConfigs = esAdapter.getDbTableEsSyncConfig().get(database + "-" + table);

    esAdapter.getEsSyncService().sync(esSyncConfigs.values(), dml);

    GetResponse response = esAdapter.getEsConnection()
        .getTransportClient()
        .prepareGet("mytest_user", "_doc", "1")
        .get();
    Assert.assertEquals("admin2", response.getSource().get("_role_name"));
}
 
Example #22
Source File: ElasticsearchTemplate.java    From summerframework with Apache License 2.0 5 votes vote down vote up
public <T> T query(ESBasicInfo esBasicInfo, Class<T> clazz) throws IOException {
    GetRequestBuilder requestBuilder =
        esClient.prepareGet(esBasicInfo.getIndex(), esBasicInfo.getType(), esBasicInfo.getIds()[0]);
    GetResponse response = requestBuilder.execute().actionGet();

    return response.getSourceAsString() != null ? mapper.readValue(response.getSourceAsString(), clazz) : null;
}
 
Example #23
Source File: ESDataExchangeImpl.java    From youkefu with Apache License 2.0 5 votes vote down vote up
public UKDataBean getIObjectByPK(UKDataBean dataBean , String id) {
	if(dataBean.getTable()!=null){
		GetResponse getResponse = UKDataContext.getTemplet().getClient()
				.prepareGet(UKDataContext.CALLOUT_INDEX,
						dataBean.getTable().getTablename(), dataBean.getId())
				.execute().actionGet();
		dataBean.setValues(getResponse.getSource());
		dataBean.setType(getResponse.getType());
	}else{
		dataBean.setValues(new HashMap<String,Object>());
	}
	
	return processDate(dataBean);
}
 
Example #24
Source File: ElasticSearchIndexer.java    From james-project with Apache License 2.0 5 votes vote down vote up
public Mono<GetResponse> get(DocumentId id, RoutingKey routingKey) {
    return Mono.fromRunnable(() -> {
            Preconditions.checkNotNull(id);
            Preconditions.checkNotNull(routingKey);
        })
        .then(client.get(new GetRequest(aliasName.getValue())
                .type(NodeMappingFactory.DEFAULT_MAPPING_NAME)
                .id(id.asString())
                .routing(routingKey.asString()),
            RequestOptions.DEFAULT));
}
 
Example #25
Source File: RoleSyncJoinOne2Test.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
/**
 * 带函数非子查询从表插入
 */
@Test
public void test01() {
    DataSource ds = DatasourceConfig.DATA_SOURCES.get("defaultDS");
    Common.sqlExe(ds, "delete from role where id=1");
    Common.sqlExe(ds, "insert into role (id,role_name) values (1,'admin')");

    Dml dml = new Dml();
    dml.setDestination("example");
    dml.setTs(new Date().getTime());
    dml.setType("INSERT");
    dml.setDatabase("mytest");
    dml.setTable("role");
    List<Map<String, Object>> dataList = new ArrayList<>();
    Map<String, Object> data = new LinkedHashMap<>();
    dataList.add(data);
    data.put("id", 1L);
    data.put("role_name", "admin");
    dml.setData(dataList);

    String database = dml.getDatabase();
    String table = dml.getTable();
    Map<String, ESSyncConfig> esSyncConfigs = esAdapter.getDbTableEsSyncConfig().get(database + "-" + table);

    esAdapter.getEsSyncService().sync(esSyncConfigs.values(), dml);

    GetResponse response = esAdapter.getTransportClient().prepareGet("mytest_user", "_doc", "1").get();
    Assert.assertEquals("admin_", response.getSource().get("_role_name"));
}
 
Example #26
Source File: ESDataExchangeImpl.java    From youkefu with Apache License 2.0 5 votes vote down vote up
public UKDataBean getIObjectByPK(String type , String id) {
	UKDataBean dataBean = new UKDataBean() ;
	if(!StringUtils.isBlank(type)){
		GetResponse getResponse = UKDataContext.getTemplet().getClient()
				.prepareGet(UKDataContext.CALLOUT_INDEX,
						type, id)
				.execute().actionGet();
		dataBean.setValues(getResponse.getSource());
		dataBean.setType(getResponse.getType());
		dataBean.setId(getResponse.getId());
	}else{
		dataBean.setValues(new HashMap<String,Object>());
	}
	return dataBean;
}
 
Example #27
Source File: DatumFromMetadataProcessor.java    From streams with Apache License 2.0 5 votes vote down vote up
@Override
public List<StreamsDatum> process(StreamsDatum entry) {
  List<StreamsDatum> result = new ArrayList<>();

  if (entry == null || entry.getMetadata() == null) {
    return result;
  }

  Map<String, Object> metadata = entry.getMetadata();

  String index = ElasticsearchMetadataUtil.getIndex(metadata, config);
  String type = ElasticsearchMetadataUtil.getType(metadata, config);
  String id = ElasticsearchMetadataUtil.getId(entry);

  GetRequestBuilder getRequestBuilder = elasticsearchClientManager.client().prepareGet(index, type, id);
  getRequestBuilder.setFields("*", "_timestamp");
  getRequestBuilder.setFetchSource(true);
  GetResponse getResponse = getRequestBuilder.get();

  if ( getResponse == null || !getResponse.isExists() || getResponse.isSourceEmpty() ) {
    return result;
  }

  entry.setDocument(getResponse.getSource());
  if ( getResponse.getField("_timestamp") != null) {
    DateTime timestamp = new DateTime(((Long) getResponse.getField("_timestamp").getValue()).longValue());
    entry.setTimestamp(timestamp);
  }

  result.add(entry);

  return result;
}
 
Example #28
Source File: ElasticsearchIndexerTest.java    From datashare with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void test_bulk_add_should_add_ner_pipeline_once_and_for_empty_list() throws IOException {
    Document doc = new org.icij.datashare.text.Document("id", project("prj"), Paths.get("doc.txt"), "content",
            Language.FRENCH, Charset.defaultCharset(), "application/pdf", new HashMap<>(),
            INDEXED, new HashSet<Pipeline.Type>() {{ add(OPENNLP);}}, 432L);
    indexer.add(TEST_INDEX, doc);

    assertThat(indexer.bulkAdd(TEST_INDEX, OPENNLP, emptyList(), doc)).isTrue();

    GetResponse resp = es.client.get(new GetRequest(TEST_INDEX, "doc", doc.getId()));
    assertThat(resp.getSourceAsMap().get("status")).isEqualTo("DONE");
    assertThat((ArrayList<String>) resp.getSourceAsMap().get("nerTags")).containsExactly("OPENNLP");
}
 
Example #29
Source File: EsTypeImpl.java    From io with Apache License 2.0 5 votes vote down vote up
@Override
GetResponse doProcess() {
    GetResponse response = asyncGet(id, realTime).actionGet();
    if (!response.isExists()) {
        // データがなかったらnullを返す
        return null;
    }
    return response;
}
 
Example #30
Source File: ElasticsearchHistory.java    From baleen with Apache License 2.0 5 votes vote down vote up
@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);
  }
}