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("<", "<").replace(">", ">"));
} 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;
}
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();
}
}
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);
}
}
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());
}
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();
}
Aggregations