Search in sources :

Example 61 with IndexRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexRequest in project incubator-sdap-mudrod by apache.

the class TrainingImporter method importTrainingSet.

/**
 * Method of importing training set in to Elasticsearch
 *
 * @param dataFolder the path to the traing set
 * @throws IOException IOException
 */
public void importTrainingSet(String dataFolder) throws IOException {
    es.createBulkProcessor();
    File[] files = new File(dataFolder).listFiles();
    for (File file : files) {
        BufferedReader br = new BufferedReader(new FileReader(file.getAbsolutePath()));
        br.readLine();
        String line = br.readLine();
        while (line != null) {
            String[] list = line.split(",");
            String query = file.getName().replace(".csv", "");
            if (list.length > 0) {
                IndexRequest ir = new IndexRequest(props.getProperty(MudrodConstants.ES_INDEX_NAME), "trainingranking").source(jsonBuilder().startObject().field("query", query).field("dataID", list[0]).field("label", list[list.length - 1]).endObject());
                es.getBulkProcessor().add(ir);
            }
            line = br.readLine();
        }
        br.close();
    }
    es.destroyBulkProcessor();
}
Also used : BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) IndexRequest(org.elasticsearch.action.index.IndexRequest) File(java.io.File)

Example 62 with IndexRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexRequest in project incubator-sdap-mudrod by apache.

the class ImportMetadata method importToES.

/**
 * importToES: Index metadata into elasticsearch from local file directory.
 * Please make sure metadata have been harvest from web service before
 * invoking this method.
 */
private void importToES() {
    es.deleteType(props.getProperty(MudrodConstants.ES_INDEX_NAME), MudrodConstants.RECOM_METADATA_TYPE);
    es.createBulkProcessor();
    File directory = new File(props.getProperty(MudrodConstants.RAW_METADATA_PATH));
    File[] fList = directory.listFiles();
    for (File file : fList) {
        InputStream is;
        try {
            is = new FileInputStream(file);
            try {
                String jsonTxt = IOUtils.toString(is);
                JsonParser parser = new JsonParser();
                JsonElement item = parser.parse(jsonTxt);
                IndexRequest ir = new IndexRequest(props.getProperty(MudrodConstants.ES_INDEX_NAME), MudrodConstants.RECOM_METADATA_TYPE).source(item.toString());
                es.getBulkProcessor().add(ir);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    es.destroyBulkProcessor();
}
Also used : JsonElement(com.google.gson.JsonElement) IndexRequest(org.elasticsearch.action.index.IndexRequest) JsonParser(com.google.gson.JsonParser)

Example 63 with IndexRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexRequest in project incubator-sdap-mudrod by apache.

the class LinkageTriple method insertTriples.

public static void insertTriples(ESDriver es, List<LinkageTriple> triples, String index, String type, Boolean bTriple, boolean bSymmetry) throws IOException {
    es.deleteType(index, type);
    if (bTriple) {
        LinkageTriple.addMapping(es, index, type);
    }
    if (triples == null) {
        return;
    }
    es.createBulkProcessor();
    for (LinkageTriple triple : triples) {
        XContentBuilder jsonBuilder = jsonBuilder().startObject();
        if (bTriple) {
            jsonBuilder.field("concept_A", triple.keyA);
            jsonBuilder.field("concept_B", triple.keyB);
        } else {
            jsonBuilder.field("keywords", triple.keyA + "," + triple.keyB);
        }
        jsonBuilder.field("weight", Double.parseDouble(df.format(triple.weight)));
        jsonBuilder.endObject();
        IndexRequest ir = new IndexRequest(index, type).source(jsonBuilder);
        es.getBulkProcessor().add(ir);
        if (bTriple && bSymmetry) {
            XContentBuilder symmetryJsonBuilder = jsonBuilder().startObject();
            symmetryJsonBuilder.field("concept_A", triple.keyB);
            symmetryJsonBuilder.field("concept_B", triple.keyA);
            symmetryJsonBuilder.field("weight", Double.parseDouble(df.format(triple.weight)));
            symmetryJsonBuilder.endObject();
            IndexRequest symmetryir = new IndexRequest(index, type).source(symmetryJsonBuilder);
            es.getBulkProcessor().add(symmetryir);
        }
    }
    es.destroyBulkProcessor();
}
Also used : IndexRequest(org.elasticsearch.action.index.IndexRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 64 with IndexRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexRequest in project drill by apache.

the class ElasticInfoSchemaTest method prepareData.

private static void prepareData() throws IOException {
    restHighLevelClient = new RestHighLevelClient(RestClient.builder(HttpHost.create(HOST)));
    String indexName = "t1";
    indexNames.add(indexName);
    CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);
    restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
    XContentBuilder builder = XContentFactory.jsonBuilder();
    builder.startObject();
    builder.field("string_field", "a");
    builder.field("int_field", 123);
    builder.endObject();
    IndexRequest indexRequest = new IndexRequest(indexName).source(builder);
    restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
    restHighLevelClient.indices().refresh(new RefreshRequest(indexName), RequestOptions.DEFAULT);
    indexName = "t2";
    indexNames.add(indexName);
    createIndexRequest = new CreateIndexRequest(indexName);
    restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
    builder = XContentFactory.jsonBuilder();
    builder.startObject();
    builder.field("another_int_field", 321);
    builder.field("another_string_field", "b");
    builder.endObject();
    indexRequest = new IndexRequest(indexName).source(builder);
    restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
    restHighLevelClient.indices().refresh(new RefreshRequest(indexName), RequestOptions.DEFAULT);
}
Also used : RefreshRequest(org.elasticsearch.action.admin.indices.refresh.RefreshRequest) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) CreateIndexRequest(org.elasticsearch.client.indices.CreateIndexRequest) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) CreateIndexRequest(org.elasticsearch.client.indices.CreateIndexRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 65 with IndexRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexRequest in project samza by apache.

the class ElasticsearchSystemProducer method send.

@Override
public void send(String source, OutgoingMessageEnvelope envelope) {
    IndexRequest indexRequest = indexRequestFactory.getIndexRequest(envelope);
    sourceBulkProcessor.get(source).add(indexRequest);
}
Also used : IndexRequest(org.elasticsearch.action.index.IndexRequest)

Aggregations

IndexRequest (org.elasticsearch.action.index.IndexRequest)187 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)37 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)37 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)34 IOException (java.io.IOException)30 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)27 Test (org.junit.Test)27 ElasticsearchException (org.elasticsearch.ElasticsearchException)21 IndexResponse (org.elasticsearch.action.index.IndexResponse)20 HashMap (java.util.HashMap)17 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)17 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)17 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)17 Map (java.util.Map)15 GetRequest (org.elasticsearch.action.get.GetRequest)14 CreateIndexRequest (org.elasticsearch.client.indices.CreateIndexRequest)12 BytesReference (org.elasticsearch.common.bytes.BytesReference)12 ArrayList (java.util.ArrayList)11 CreateIndexRequest (org.elasticsearch.action.admin.indices.create.CreateIndexRequest)10 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)9