Search in sources :

Example 1 with SimpleMapping

use of de.tblsoft.solr.pipeline.filter.SimpleMapping in project solr-cmd-utils by tblsoft.

the class ElasticJsonPathReader method read.

public void read() {
    String response = "";
    String pagedUrl = "";
    String scrollId = "";
    boolean hasHits = false;
    SimpleMapping simpleMapping = new SimpleMapping(getPropertyAsList("mapping", new ArrayList<String>()));
    Map<String, List<String>> mapping = simpleMapping.getMapping();
    try {
        url = getProperty("url", null);
        scroll = getProperty("scroll", "1m");
        String scrollBaseUrl = ElasticHelper.getScrollUrl(url);
        pagedUrl = url + "&scroll=" + scroll;
        do {
            response = HTTPHelper.get(pagedUrl);
            DocumentContext context = JsonPath.parse(response);
            scrollId = context.read("$['_scroll_id']");
            List<Object> elasticHits = context.read("$['hits']['hits'][*]['_source']");
            hasHits = false;
            int count = 0;
            for (Object obj : elasticHits) {
                hasHits = true;
                Document document = new Document();
                for (Map.Entry<String, List<String>> mappingEntry : mapping.entrySet()) {
                    try {
                        Object parsedValue = JsonPath.parse(obj).read(mappingEntry.getKey());
                        for (String target : mappingEntry.getValue()) {
                            document.setField(target, parsedValue);
                        }
                    } catch (PathNotFoundException e) {
                    // ignore
                    }
                }
                executer.document(document);
            }
            pagedUrl = scrollBaseUrl + "?scroll=" + scroll + "&scroll_id=" + scrollId;
        } while (hasHits);
    // executer.end();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ArrayList(java.util.ArrayList) Document(de.tblsoft.solr.pipeline.bean.Document) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException) SimpleMapping(de.tblsoft.solr.pipeline.filter.SimpleMapping) List(java.util.List) ArrayList(java.util.ArrayList) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException) DocumentContext(com.jayway.jsonpath.DocumentContext) Map(java.util.Map)

Example 2 with SimpleMapping

use of de.tblsoft.solr.pipeline.filter.SimpleMapping in project solr-cmd-utils by tblsoft.

the class JsonPathReader method read.

public void read() {
    SimpleMapping simpleMapping = new SimpleMapping(getPropertyAsList("mapping", new ArrayList<String>()));
    Map<String, List<String>> mapping = simpleMapping.getMapping();
    try {
        String rootPath = getProperty("rootPath", "$");
        DocumentContext context = loadJsonContext();
        List<Object> jsonHits = context.read(rootPath);
        for (Object obj : jsonHits) {
            Document document = new Document();
            for (Map.Entry<String, List<String>> mappingEntry : mapping.entrySet()) {
                try {
                    Object parsedValue = JsonPath.parse(obj).read(mappingEntry.getKey());
                    for (String target : mappingEntry.getValue()) {
                        document.setField(target, parsedValue);
                    }
                } catch (PathNotFoundException e) {
                // ignore
                }
            }
            executer.document(document);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ArrayList(java.util.ArrayList) Document(de.tblsoft.solr.pipeline.bean.Document) IOException(java.io.IOException) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException) SimpleMapping(de.tblsoft.solr.pipeline.filter.SimpleMapping) ArrayList(java.util.ArrayList) List(java.util.List) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException) DocumentContext(com.jayway.jsonpath.DocumentContext) Map(java.util.Map)

Aggregations

DocumentContext (com.jayway.jsonpath.DocumentContext)2 PathNotFoundException (com.jayway.jsonpath.PathNotFoundException)2 Document (de.tblsoft.solr.pipeline.bean.Document)2 SimpleMapping (de.tblsoft.solr.pipeline.filter.SimpleMapping)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Map (java.util.Map)2 IOException (java.io.IOException)1