<commit_before>package de.tblsoft.solr.pipeline;

import com.google.common.base.Strings;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import de.tblsoft.solr.pipeline.bean.Document;

import java.io.*;
import java.util.HashMap;
import java.util.Map;


/**
 * Read Elasticdump JSON export file
 */
public class ElasticdumpJsonReader extends AbstractReader {
    private String filepath;
    private String format; // elasticdump (default) or bulk
    private String jsonPathRoot;

    public void read() {
        filepath = getProperty("filepath", null);
        if(Strings.isNullOrEmpty(filepath)) {
            throw new RuntimeException("For the ElasticdumpJsonReader a filepath property must be defined!");
        }
        format = getProperty("format", "elasticdump");
        jsonPathRoot = "elasticdump".equals(format) ? "$['_source']" : "$";

        try {
            readFileAsLines(filepath);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    protected void readFileAsLines(String filepath) throws IOException {
        BufferedReader br = null;
        FileReader fr = null;
        try {
            fr = new FileReader(filepath);
            br = new BufferedReader(fr);

            String line;
            while ((line = br.readLine()) != null) {
                Document doc = parseJsonLineAsDoc(line, jsonPathRoot);
                if("bulk".equals(format)) {
                    String lineSource = br.readLine();
                    Document docSource = parseJsonLineAsDoc(lineSource, jsonPathRoot);
                    docSource.setField("_index", doc.getFieldValue("_index"));
                    docSource.setField("_type", doc.getFieldValue("_type"));
                    docSource.setField("_id", doc.getFieldValue("_id"));
                    doc = docSource;
                }
                executer.document(doc);<commit_msg>- add support for relative filepath in ElasticdumpJsonReader<commit_after>package de.tblsoft.solr.pipeline;

import com.google.common.base.Strings;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import de.tblsoft.solr.pipeline.bean.Document;
import de.tblsoft.solr.util.IOUtils;

import java.io.*;
import java.util.HashMap;
import java.util.Map;


/**
 * Read Elasticdump JSON export file
 */
public class ElasticdumpJsonReader extends AbstractReader {
    private String filepath;
    private String format; // elasticdump (default) or bulk
    private String jsonPathRoot;

    public void read() {
        filepath = getProperty("filepath", null);
        if(Strings.isNullOrEmpty(filepath)) {
            throw new RuntimeException("For the ElasticdumpJsonReader a filepath property must be defined!");
        }
        format = getProperty("format", "elasticdump");
        jsonPathRoot = "elasticdump".equals(format) ? "$['_source']" : "$";

        try {
            readFileAsLines(filepath);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    protected void readFileAsLines(String filepath) throws IOException {
        BufferedReader br = null;
        FileReader fr = null;
        try {
            File jsonFile = IOUtils.getAbsoluteFileAsFile(getBaseDir(), filepath);
            fr = new FileReader(jsonFile);
            br = new BufferedReader(fr);

            String line;
            while ((line = br.readLine()) != null) {
                Document doc = parseJsonLineAsDoc(line, jsonPathRoot);
                if("bulk".equals(format)) {
                    String lineSource = br.readLine();
                    Document docSource = parseJsonLineAsDoc(lineSource, jsonPathRoot);
                    docSource.setField("_index", doc.getFieldValue("_index"));
                    docSource.setField("_type", doc.getFieldValue("_type"));
                    docSource.setField("_id", doc.getFieldValue("_id"));
                    doc = docSource;
                }
                executer.document(doc);
{'fraction_non_alphanumeric': 0.10625154435384235, 'fraction_numerical': 0.0, 'mean_word_length': 2.3931265716680636, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1088200', 'n_tokens_mistral': 1098, 'n_tokens_neox': 1066, 'n_words': 246}