Search in sources :

Example 61 with IndexRequest

use of org.elasticsearch.action.index.IndexRequest in project main by JohnPeng739.

the class ElasticAccessorRest method save.

/**
 * {@inheritDoc}
 *
 * @see ElasticAccessor#save(Base)
 */
@Override
public <T extends Base> T save(T t) throws UserInterfaceDalErrorException {
    try {
        Class<T> clazz = (Class<T>) t.getClass();
        boolean isNew;
        if (StringUtils.isBlank(t.getId())) {
            // 新数据
            t.setId(DigestUtils.uuid());
            t.setCreatedTime(System.currentTimeMillis());
            isNew = true;
        } else {
            T check = getById(t.getId(), clazz);
            if (check != null) {
                // 修改数据
                t.setCreatedTime(check.getCreatedTime());
                if (check instanceof BaseDict) {
                    ((BaseDict) t).setCode(((BaseDict) check).getCode());
                }
                isNew = false;
            } else {
                isNew = true;
            }
        }
        t.setUpdatedTime(System.currentTimeMillis());
        t.setOperator(sessionDataStore.getCurrentUserCode());
        if (isNew) {
            IndexRequest request = new IndexRequest(index, t.getClass().getName(), t.getId());
            request.source(JSON.toJSONString(t), XContentType.JSON);
            client.index(request);
        } else {
            UpdateRequest request = new UpdateRequest(index, t.getClass().getName(), t.getId());
            request.doc(JSON.toJSONString(t), XContentType.JSON);
            client.update(request);
        }
        return getById(t.getId(), (Class<T>) t.getClass());
    } catch (IOException ex) {
        if (logger.isErrorEnabled()) {
            logger.error("Save the data into elastic fail.", ex);
        }
        throw new UserInterfaceDalErrorException(UserInterfaceDalErrorException.DalErrors.DB_OPERATE_FAIL);
    }
}
Also used : BaseDict(org.mx.dal.entity.BaseDict) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) UserInterfaceDalErrorException(org.mx.dal.error.UserInterfaceDalErrorException) IOException(java.io.IOException) OpenIndexRequest(org.elasticsearch.action.admin.indices.open.OpenIndexRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest)

Example 62 with IndexRequest

use of 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 63 with IndexRequest

use of 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 64 with IndexRequest

use of 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 65 with IndexRequest

use of 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)

Aggregations

IndexRequest (org.elasticsearch.action.index.IndexRequest)177 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)36 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)34 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)33 IOException (java.io.IOException)28 Test (org.junit.Test)27 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)26 ElasticsearchException (org.elasticsearch.ElasticsearchException)20 IndexResponse (org.elasticsearch.action.index.IndexResponse)17 HashMap (java.util.HashMap)16 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)15 Map (java.util.Map)14 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)14 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)13 GetRequest (org.elasticsearch.action.get.GetRequest)13 BytesReference (org.elasticsearch.common.bytes.BytesReference)12 ArrayList (java.util.ArrayList)10 Matchers.anyBoolean (org.mockito.Matchers.anyBoolean)9 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)8 CreateIndexRequest (org.elasticsearch.client.indices.CreateIndexRequest)8