Search in sources :

Example 36 with GetResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse in project pyramid by cheng-li.

the class Visualizer method createTable.

private List<Object> createTable(List<Object> data, String fields, Map<String, String> classDescription) throws IOException {
    int line_count = 0;
    List<Object> output = new ArrayList<>();
    for (Object rowData : data) {
        final Map<String, Object> row = (Map<String, Object>) rowData;
        final Map<String, Object> oneRow = new HashMap<>();
        line_count += 1;
        final List<Object> r = new ArrayList<>();
        final Map<String, Object> idLabels = new HashMap<>();
        idLabels.put("id", row.get("id"));
        idLabels.put("internalId", row.get("internalId"));
        idLabels.put("internalLabels", row.get("internalLabels"));
        idLabels.put("feedbackSelect", "none");
        idLabels.put("feedbackText", "");
        final List<Object> internalLabels = new ArrayList<>();
        final Set<String> releLabels = new HashSet<>();
        for (int i = 0, rowSize = ((List<Object>) row.get("labels")).size(); i < rowSize; i++) {
            Map<Object, Object> label = new HashMap<>();
            label.put(((List<Object>) row.get("internalLabels")).get(i), ((List<Object>) row.get("labels")).get(i));
            internalLabels.add(label);
            releLabels.add(((List<String>) row.get("labels")).get(i));
        }
        final List<Object> predictions = new ArrayList<>();
        final Set<String> pres = new HashSet<>();
        for (int i = 0, rowSize = ((List<Object>) row.get("prediction")).size(); i < rowSize; i++) {
            Map<Object, Object> label = new HashMap<>();
            label.put(((List<Object>) row.get("internalPrediction")).get(i), ((List<Object>) row.get("prediction")).get(i));
            predictions.add(label);
            pres.add(((List<String>) row.get("prediction")).get(i));
        }
        idLabels.put("predictions", predictions);
        final Set<String> intersections = new HashSet<>(releLabels);
        intersections.retainAll(pres);
        final Set<String> unions = new HashSet<>(releLabels);
        unions.addAll(pres);
        if (unions.size() == 0) {
            idLabels.put("overlap", "N/A");
        } else {
            idLabels.put("overlap", String.format("%.2f", (double) intersections.size() / unions.size()));
        }
        if (releLabels.size() == 0) {
            idLabels.put("recall", "N/A");
        } else {
            idLabels.put("recall", String.format("%.2f", -(double) intersections.size() / releLabels.size()));
        }
        if (pres.size() == 0) {
            idLabels.put("precision", "N/A");
        } else {
            idLabels.put("precision", (double) intersections.size() / pres.size());
        }
        oneRow.put("probForPredictedLabels", row.get("probForPredictedLabels"));
        oneRow.put("predictedRanking", new ArrayList<>());
        for (Map<String, Object> label : (List<Map<String, Object>>) row.get("predictedRanking")) {
            final List<Object> curInternalPrediction = (List<Object>) row.get("internalPrediction");
            final List<Object> curInternalLabels = (List<Object>) row.get("internalLabels");
            final Object labelClassIndex = label.get("classIndex");
            if (curInternalLabels.contains(labelClassIndex) && curInternalPrediction.contains(labelClassIndex)) {
                label.put("type", "TP");
            } else if (!curInternalLabels.contains(labelClassIndex) && curInternalPrediction.contains(labelClassIndex)) {
                label.put("type", "FP");
            } else if (!curInternalLabels.contains(labelClassIndex) && !curInternalPrediction.contains(labelClassIndex)) {
                label.put("type", "FN");
            } else {
                label.put("type", "");
            }
            r.add(includesLabel(label.get("className"), internalLabels));
            ((List<Object>) oneRow.get("predictedRanking")).add(label);
        }
        for (Map<String, Object> labels : (List<Map<String, Object>>) row.get("predictedLabelSetRanking")) {
            labels.put("types", new ArrayList<>());
            final List<Object> curInternalLabels = (List<Object>) row.get("internalLabels");
            final List<Object> curInternalPrediction = (List<Object>) row.get("internalPrediction");
            for (Object index : (List<Object>) labels.get("internalLabels")) {
                if (curInternalLabels.contains(index) && curInternalPrediction.contains(index)) {
                    ((List<Object>) labels.get("types")).add("TP");
                } else if (!curInternalLabels.contains(index) && curInternalPrediction.contains(index)) {
                    ((List<Object>) labels.get("types")).add("FP");
                } else if (curInternalLabels.contains(index) && !curInternalPrediction.contains(index)) {
                    ((List<Object>) labels.get("types")).add("FN");
                } else {
                    ((List<Object>) labels.get("types")).add("");
                }
            }
        }
        oneRow.put("predictedLabelSetRanking", row.get("predictedLabelSetRanking"));
        double sumOfR = 0.0;
        double sumOfPrec = 0.0;
        int last = 0;
        for (int i = 0; i < r.size(); i++) {
            if (r.get(i).equals(new Integer(1))) {
                sumOfR += (Integer) r.get(i);
                sumOfPrec += sumOfR / (i + 1);
                last = i + 1;
            }
        }
        if (releLabels.size() == 0) {
            idLabels.put("ap", "N/A");
        } else {
            idLabels.put("ap", String.format("%.2f", sumOfPrec / releLabels.size()));
        }
        if (sumOfR < releLabels.size()) {
            idLabels.put("rankoffullrecall", "N/A");
        } else {
            idLabels.put("rankoffullrecall", last);
        }
        oneRow.put("idLabels", idLabels);
        GetResponse res = config.getClient().prepareGet(config.getEsIndexName(), "document", (String) row.get("id")).execute().actionGet();
        String keys = fields;
        oneRow.put("text", new HashMap<String, Map<String, Object>>());
        oneRow.put("others", new HashMap<String, Map<String, Object>>());
        for (String key : ((Map<String, Object>) res.getContext().get("_source")).keySet()) {
            if (keys.equals(key)) {
                ((Map<String, Object>) oneRow.get("text")).put(key, ((String) ((Map<String, Object>) res.getContext().get("_source")).get(key)).replace("<", "&lt").replace(">", "&gt"));
            } else {
                ((Map<String, Object>) oneRow.get("others")).put(key, ((String) ((Map<String, Object>) res.getContext().get("_source")).get(key)));
            }
        }
        createTFPNColumns(row, line_count, oneRow, classDescription);
        output.add(oneRow);
    }
    return output;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GetResponse(org.elasticsearch.action.get.GetResponse) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 37 with GetResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse in project wildfly-camel by wildfly-extras.

the class ElasticsearchIntegrationTest method testDeleteContent.

@Test
public void testDeleteContent() throws Exception {
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:index").to("elasticsearch://local?operation=INDEX&indexName=twitter&indexType=tweet");
            from("direct:get").to("elasticsearch://local?operation=GET_BY_ID&indexName=twitter&indexType=tweet");
            from("direct:delete").to("elasticsearch://local?operation=DELETE&indexName=twitter&indexType=tweet");
        }
    });
    camelctx.getComponent("elasticsearch", ElasticsearchComponent.class).setClient(client);
    camelctx.start();
    try {
        Map<String, String> indexedData = new HashMap<>();
        indexedData.put("content", "test");
        // Index some initial data
        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBody("direct:index", indexedData);
        String indexId = template.requestBody("direct:index", indexedData, String.class);
        Assert.assertNotNull("Index id should not be null", indexId);
        // Retrieve indexed data
        GetResponse getResponse = template.requestBody("direct:get", indexId, GetResponse.class);
        Assert.assertNotNull("getResponse should not be null", getResponse);
        // Delete indexed data
        DeleteResponse deleteResponse = template.requestBody("direct:delete", indexId, DeleteResponse.class);
        Assert.assertNotNull("deleteResponse should not be null", deleteResponse);
        // Verify that the data has been deleted
        getResponse = template.requestBody("direct:get", indexId, GetResponse.class);
        Assert.assertNotNull("getResponse should not be null", getResponse);
        Assert.assertNull("getResponse source should be null", getResponse.getSource());
    } finally {
        camelctx.stop();
    }
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) ProducerTemplate(org.apache.camel.ProducerTemplate) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) RouteBuilder(org.apache.camel.builder.RouteBuilder) HashMap(java.util.HashMap) ElasticsearchComponent(org.apache.camel.component.elasticsearch.ElasticsearchComponent) GetResponse(org.elasticsearch.action.get.GetResponse) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Test(org.junit.Test)

Example 38 with GetResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse in project syncope by apache.

the class ElasticsearchIndexManager method after.

@TransactionalEventListener
public void after(final AnyCreatedUpdatedEvent<Any<?>> event) throws IOException {
    GetResponse getResponse = client.prepareGet(AuthContextUtils.getDomain().toLowerCase(), event.getAny().getType().getKind().name(), event.getAny().getKey()).get();
    if (getResponse.isExists()) {
        LOG.debug("About to update index for {}", event.getAny());
        UpdateResponse response = client.prepareUpdate(AuthContextUtils.getDomain().toLowerCase(), event.getAny().getType().getKind().name(), event.getAny().getKey()).setRetryOnConflict(elasticsearchUtils.getRetryOnConflict()).setDoc(elasticsearchUtils.builder(event.getAny())).get();
        LOG.debug("Index successfully updated for {}: {}", event.getAny(), response);
    } else {
        LOG.debug("About to create index for {}", event.getAny());
        IndexResponse response = client.prepareIndex(AuthContextUtils.getDomain().toLowerCase(), event.getAny().getType().getKind().name(), event.getAny().getKey()).setSource(elasticsearchUtils.builder(event.getAny())).get();
        LOG.debug("Index successfully created for {}: {}", event.getAny(), response);
    }
}
Also used : UpdateResponse(org.elasticsearch.action.update.UpdateResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) GetResponse(org.elasticsearch.action.get.GetResponse) TransactionalEventListener(org.springframework.transaction.event.TransactionalEventListener)

Example 39 with GetResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse in project bw-calendar-engine by Bedework.

the class BwIndexEsImpl method fetchEntity.

private EntityBuilder fetchEntity(final String docType, final String val, final PropertyInfoIndex... index) throws CalFacadeException {
    if ((index.length == 1) && (index[0] == PropertyInfoIndex.HREF)) {
        final GetRequestBuilder grb = getClient().prepareGet(targetIndex, docType, val);
        final GetResponse gr = grb.execute().actionGet();
        if (!gr.isExists()) {
            return null;
        }
        return getEntityBuilder(gr.getSourceAsMap());
    }
    final SearchHit hit = fetchEntity(docType, getFilters(null).singleEntityFilter(docType, val, index));
    if (hit == null) {
        return null;
    }
    return getEntityBuilder(hit.sourceAsMap());
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) GetResponse(org.elasticsearch.action.get.GetResponse) GetRequestBuilder(org.elasticsearch.action.get.GetRequestBuilder)

Example 40 with GetResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse in project bw-calendar-engine by Bedework.

the class BwIndexEsImpl method currentChangeToken.

@Override
public String currentChangeToken() throws CalFacadeException {
    UpdateInfo ui;
    try {
        final GetRequestBuilder grb = getClient().prepareGet(targetIndex, docTypeUpdateTracker, updateTrackerId).setFields("count", "_timestamp");
        final GetResponse gr = grb.execute().actionGet();
        if (!gr.isExists()) {
            return null;
        }
        final EntityBuilder er = getEntityBuilder(gr.getFields());
        ui = er.makeUpdateInfo();
    } catch (final ElasticsearchException ese) {
        warn("Exception getting UpdateInfo: " + ese.getLocalizedMessage());
        ui = new UpdateInfo();
    }
    synchronized (updateInfo) {
        UpdateInfo tui = updateInfo.get(targetIndex);
        if ((tui != null) && (tui.getCount() >= (0111111111 - 1000))) {
            // Reset before we overflow
            tui = null;
        }
        if ((tui == null) || (!tui.getCount().equals(ui.getCount()))) {
            updateInfo.put(targetIndex, ui);
        } else {
            ui = tui;
        }
    }
    return ui.getChangeToken();
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) GetResponse(org.elasticsearch.action.get.GetResponse) UpdateInfo(org.bedework.util.elasticsearch.DocBuilderBase.UpdateInfo) GetRequestBuilder(org.elasticsearch.action.get.GetRequestBuilder)

Aggregations

GetResponse (org.elasticsearch.action.get.GetResponse)149 Test (org.junit.Test)41 Map (java.util.Map)27 MultiGetResponse (org.elasticsearch.action.get.MultiGetResponse)25 ArrayList (java.util.ArrayList)22 HashMap (java.util.HashMap)18 ESSyncConfig (com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig)17 Dml (com.alibaba.otter.canal.client.adapter.support.Dml)17 Date (java.util.Date)17 LinkedHashMap (java.util.LinkedHashMap)17 DataSource (javax.sql.DataSource)14 GetRequestBuilder (org.elasticsearch.action.get.GetRequestBuilder)14 SearchResponse (org.elasticsearch.action.search.SearchResponse)13 DeleteResponse (org.elasticsearch.action.delete.DeleteResponse)12 IOException (java.io.IOException)11 GetRequest (org.elasticsearch.action.get.GetRequest)11 ElasticsearchException (org.elasticsearch.ElasticsearchException)9 Settings (org.elasticsearch.common.settings.Settings)9 Alias (org.elasticsearch.action.admin.indices.alias.Alias)7 UpdateResponse (org.elasticsearch.action.update.UpdateResponse)7