Search in sources :

Example 81 with GetResponse

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;
}
Also used : GetResponse(org.elasticsearch.action.get.GetResponse) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse)

Example 82 with GetResponse

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);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) HashMap(java.util.HashMap) GetRequest(org.elasticsearch.action.get.GetRequest) ArrayList(java.util.ArrayList) IOException(java.io.IOException) GetResponse(org.elasticsearch.action.get.GetResponse)

Example 83 with GetResponse

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;
}
Also used : DocumentField(org.elasticsearch.common.document.DocumentField) GetRequest(org.elasticsearch.action.get.GetRequest) IOException(java.io.IOException) GetResponse(org.elasticsearch.action.get.GetResponse)

Example 84 with GetResponse

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"));
}
Also used : DocumentFactory(org.icij.extract.document.DocumentFactory) UpdatableDigester(org.icij.extract.extractor.UpdatableDigester) HashMap(java.util.HashMap) GetRequest(org.elasticsearch.action.get.GetRequest) TikaDocument(org.icij.extract.document.TikaDocument) Duplicate(org.icij.datashare.text.Duplicate) Extractor(org.icij.extract.extractor.Extractor) GetResponse(org.elasticsearch.action.get.GetResponse) Test(org.junit.Test)

Example 85 with GetResponse

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"));
}
Also used : PropertiesProvider(org.icij.datashare.PropertiesProvider) DocumentFactory(org.icij.extract.document.DocumentFactory) FieldNames(org.icij.spewer.FieldNames) HashMap(java.util.HashMap) ParsingReader(org.apache.tika.parser.ParsingReader) ByteArrayInputStream(java.io.ByteArrayInputStream) GetRequest(org.elasticsearch.action.get.GetRequest) PathIdentifier(org.icij.extract.document.PathIdentifier) TikaDocument(org.icij.extract.document.TikaDocument) GetResponse(org.elasticsearch.action.get.GetResponse) Test(org.junit.Test)

Aggregations

GetResponse (org.elasticsearch.action.get.GetResponse)167 Test (org.junit.Test)50 GetRequest (org.elasticsearch.action.get.GetRequest)28 Map (java.util.Map)27 MultiGetResponse (org.elasticsearch.action.get.MultiGetResponse)26 ArrayList (java.util.ArrayList)23 HashMap (java.util.HashMap)21 Date (java.util.Date)18 ESSyncConfig (com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig)17 Dml (com.alibaba.otter.canal.client.adapter.support.Dml)17 LinkedHashMap (java.util.LinkedHashMap)17 IOException (java.io.IOException)16 DataSource (javax.sql.DataSource)14 GetRequestBuilder (org.elasticsearch.action.get.GetRequestBuilder)14 SearchResponse (org.elasticsearch.action.search.SearchResponse)14 DeleteResponse (org.elasticsearch.action.delete.DeleteResponse)12 ElasticsearchException (org.elasticsearch.ElasticsearchException)10 Settings (org.elasticsearch.common.settings.Settings)10 Alias (org.elasticsearch.action.admin.indices.alias.Alias)7 IndexResponse (org.elasticsearch.action.index.IndexResponse)7