use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse in project yacy_grid_mcp by yacy.
the class ElasticsearchClient method readMap.
/**
* Read a json document from the search index for a given id.
* Elasticsearch reads the '_source' field and parses the content as json.
*
* @param id
* the unique identifier of a document
* @return the document as json, matched on a Map<String, Object> object instance
*/
public Map<String, Object> readMap(final String indexName, final String typeName, final String id) {
GetResponse response = elasticsearchClient.prepareGet(indexName, typeName, id).execute().actionGet();
Map<String, Object> map = getMap(response);
return map;
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse in project gora by apache.
the class ElasticsearchStore method get.
@Override
public T get(K key, String[] fields) throws GoraException {
String[] requestedFields = getFieldsToQuery(fields);
List<String> documentFields = new ArrayList<>();
for (String requestedField : requestedFields) {
documentFields.add(elasticsearchMapping.getFields().get(requestedField).getName());
}
try {
// Prepare the Elasticsearch request
GetRequest getRequest = new GetRequest(elasticsearchMapping.getIndexName(), (String) key);
GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
if (getResponse.isExists()) {
Map<String, Object> sourceMap = getResponse.getSourceAsMap();
// Map of field's name and its value from the Document
Map<String, Object> fieldsAndValues = new HashMap<>();
for (String field : documentFields) {
fieldsAndValues.put(field, sourceMap.get(field));
}
// Build the corresponding persistent
return newInstance(fieldsAndValues, requestedFields);
} else {
return null;
}
} catch (IOException ex) {
throw new GoraException(ex);
}
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse in project datashare by ICIJ.
the class ElasticsearchIndexer method get.
@Override
public <T extends Entity> T get(String indexName, String id, String root) {
String type = null;
try {
final GetRequest req = new GetRequest(indexName, id).routing(root);
final GetResponse resp = client.get(req, RequestOptions.DEFAULT);
if (resp.isExists()) {
Map<String, Object> sourceAsMap = resp.getSourceAsMap();
sourceAsMap.put("rootDocument", ofNullable(resp.getFields().get("_routing")).orElse(new DocumentField("_routing", Collections.singletonList(id))).getValues().get(0));
type = (String) sourceAsMap.get(esCfg.docTypeField);
Class<T> tClass = (Class<T>) Class.forName("org.icij.datashare.text." + type);
return JsonObjectMapper.getObject(id, sourceAsMap, tClass);
}
} catch (IOException e) {
LOGGER.error("Failed to get entity " + id + " in index " + indexName, e);
} catch (ClassNotFoundException e) {
LOGGER.error("no entity for type " + type);
}
return null;
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse in project datashare by ICIJ.
the class ElasticsearchSpewerTest method test_duplicate_file.
@Test
public void test_duplicate_file() throws Exception {
DocumentFactory tikaFactory = new DocumentFactory().configure(Options.from(new HashMap<String, String>() {
{
put("idDigestMethod", Document.HASHER.toString());
}
}));
Extractor extractor = new Extractor(tikaFactory);
extractor.setDigester(new UpdatableDigester("project", Document.HASHER.toString()));
final TikaDocument document = extractor.extract(get(Objects.requireNonNull(getClass().getResource("/docs/doc.txt")).getPath()));
final TikaDocument document2 = extractor.extract(get(Objects.requireNonNull(getClass().getResource("/docs/doc-duplicate.txt")).getPath()));
spewer.write(document);
spewer.write(document2);
GetResponse actualDocument = es.client.get(new GetRequest(TEST_INDEX, document.getId()), RequestOptions.DEFAULT);
GetResponse actualDocument2 = es.client.get(new GetRequest(TEST_INDEX, new Duplicate(document2.getPath(), document.getId()).getId()), RequestOptions.DEFAULT);
assertThat(actualDocument.isExists()).isTrue();
assertThat(actualDocument.getSourceAsMap()).includes(entry("type", "Document"));
assertThat(actualDocument2.isExists()).isTrue();
assertThat(actualDocument2.getSourceAsMap()).includes(entry("type", "Duplicate"));
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse in project datashare by ICIJ.
the class ElasticsearchSpewerTest method test_truncated_content.
@Test
public void test_truncated_content() throws Exception {
ElasticsearchSpewer limitedContentSpewer = new ElasticsearchSpewer(es.client, text -> Language.ENGLISH, new FieldNames(), publisher, new PropertiesProvider(new HashMap<String, String>() {
{
put("maxContentLength", "20");
}
})).withRefresh(IMMEDIATE).withIndex("test-datashare");
final TikaDocument document = new DocumentFactory().withIdentifier(new PathIdentifier()).create(get("fake-file.txt"));
final ParsingReader reader = new ParsingReader(new ByteArrayInputStream("this content should be truncated".getBytes()));
document.setReader(reader);
limitedContentSpewer.write(document);
GetResponse documentFields = es.client.get(new GetRequest(TEST_INDEX, document.getId()), RequestOptions.DEFAULT);
assertThat(documentFields.getSourceAsMap()).includes(entry("content", "this content should"));
}
Aggregations