cognition.pipeline.service.ElasticsearchService.java Source code

Java tutorial

Introduction

Here is the source code for cognition.pipeline.service.ElasticsearchService.java

Source

/*
Designed and developed by Ismail E. Kartoglu
Copyright 2015 King's College London
    
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    
    http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */

package cognition.pipeline.service;

import org.apache.log4j.Logger;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cognition.common.model.DNCWorkCoordinate;
import cognition.common.utils.HTMLUtil;
import cognition.common.utils.StringTools;
import cognition.common.utils.TimeUtil;
import cognition.pipeline.data.DNCWorkUnitDao;

import java.io.IOException;
import java.util.List;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;

@Service
public class ElasticsearchService {

    private Logger logger = Logger.getLogger(ElasticsearchService.class);

    @Autowired
    private DNCWorkUnitDao dncWorkUnitDao;

    public void exportToElastic(String url, Long port) {
        Client client = new TransportClient()
                .addTransportAddress(new InetSocketTransportAddress(url, port.intValue()));

        logger.info("Getting results from the results table...");

        List<DNCWorkCoordinate> results = dncWorkUnitDao.getResults();
        results.parallelStream().forEach(coordinate -> {
            try {
                client.prepareIndex("coordinates", "coordinate", coordinate.getCoordinateString())
                        .setSource(jsonBuilder().startObject().field("id", coordinate.getCoordinateString())
                                .field("patientId", coordinate.getPatientId())
                                .field("tableName", coordinate.getSourceTable())
                                .field("idInTable", coordinate.getIdInSourceTable())
                                .field("dataType", StringTools.getOrEmpty(coordinate.getType()))
                                .field("dataColumnName", StringTools.getOrEmpty(coordinate.getPkColumnName()))
                                .field("documentXhtml", coordinate.getConversionResult())
                                .field("documentText",
                                        HTMLUtil.getContentFromHTML(coordinate.getConversionResult()))
                                .field("lastModifiedDate",
                                        TimeUtil.fromUTCFormat(HTMLUtil
                                                .getMetaValue(coordinate.getConversionResult(), "Last-Modified")))
                                .field("author", HTMLUtil.getMetaValue(coordinate.getConversionResult(), "Author"))
                                .field("pageCount", Integer.valueOf(
                                        HTMLUtil.getMetaValue(coordinate.getConversionResult(), "xmpTPg:NPages")))
                                .endObject())
                        .execute().actionGet();
            } catch (IOException e) {
                logger.error(e.getMessage());
            }
        });

        logger.info("All done.");

        client.close();
    }

}