<commit_before>"use strict";

var environment = require('../lib/environment'),
    models = require('../models'),
    LongEncoding = environment.graphstore.g.java.import('com.thinkaurelius.titan.util.encoding.LongEncoding'),
    socketserver = require('../lib/socketserver'),
    ZOOMStream = require('zoomstream'),
    marcjs = require('marcjs'),
    Field = models.Field;

exports.suggest = function(req, res) {
    if (typeof req.query.q !== 'undefined' && req.query.q) {
        var query = { query: { match_phrase_prefix: { } }, fields: environment.esclient.fields };
        query.query.match_phrase_prefix[environment.indexes['key'].id] = req.query.q;
        environment.esclient.search(environment.esclient.indexname, 'vertex', query, function (err, data) {
            if (typeof data === 'string') data = JSON.parse(data);
            var records = [ ];
            var count = 0;
            if (typeof data.hits !== 'undefined' && data.hits.total > 0) {
                count = data.hits.total;
                data.hits.hits.forEach(function (hit) {
                    var rec = { };
                    rec._id = LongEncoding.decodeSync(hit._id);
                    for (var key in hit.fields) {
                        rec[environment.esclient.index[key]] = hit.fields[key];
                    }
                    records.push(rec);
                });
            }
            res.json({ count: count, records: records });
        });
    } else {
        res.json({ count: 0, records: [ ] });
    }
};

exports.copy = function(req, res) {
    var stream = new marcjs.MarcxmlReader(new ZOOMStream('lx2.loc.gov:210/LCDB', decodeURIComponent(req.query.q)));
    var records = [ ];
    stream.on('data', function (rec) {
        records.push(marcjs.MiJWriter.toMiJ(rec));
    });
    stream.on('end', function () {
        res.json({ format: 'marc21', records: records });
    });
};

exports.bulk = function (req, res) {
    var data = { view: 'cataloging' };<commit_msg>Degrade gracefully if yaz/ZOOM isn't available<commit_after>"use strict";

var environment = require('../lib/environment'),
    models = require('../models'),
    LongEncoding = environment.graphstore.g.java.import('com.thinkaurelius.titan.util.encoding.LongEncoding'),
    socketserver = require('../lib/socketserver'),
    ZOOMStream,
    marcjs = require('marcjs'),
    Field = models.Field;

try {
    ZOOMStream = require('zoomstream');
} catch (e) {
}
exports.suggest = function(req, res) {
    if (typeof req.query.q !== 'undefined' && req.query.q) {
        var query = { query: { match_phrase_prefix: { } }, fields: environment.esclient.fields };
        query.query.match_phrase_prefix[environment.indexes['key'].id] = req.query.q;
        environment.esclient.search(environment.esclient.indexname, 'vertex', query, function (err, data) {
            if (typeof data === 'string') data = JSON.parse(data);
            var records = [ ];
            var count = 0;
            if (typeof data.hits !== 'undefined' && data.hits.total > 0) {
                count = data.hits.total;
                data.hits.hits.forEach(function (hit) {
                    var rec = { };
                    rec._id = LongEncoding.decodeSync(hit._id);
                    for (var key in hit.fields) {
                        rec[environment.esclient.index[key]] = hit.fields[key];
                    }
                    records.push(rec);
                });
            }
            res.json({ count: count, records: records });
        });
    } else {
        res.json({ count: 0, records: [ ] });
    }
};

exports.copy = function(req, res) {
    if (ZOOMStream) {
        var stream = new marcjs.MarcxmlReader(new ZOOMStream('lx2.loc.gov:210/LCDB', decodeURIComponent(req.query.q)));
        var records = [ ];
        stream.on('data', function (rec) {
            records.push(marcjs.MiJWriter.toMiJ(rec));
        });
        stream.on('end', function () {
            res.json({ format: 'marc21', records: records });
        });
    } else {
        res.json({ format: 'marc21', records: [ ] });
    }
};

exports.bulk = function (req, res) {
    var data = { view: 'cataloging' };
{'fraction_non_alphanumeric': 0.15829746532759445, 'fraction_numerical': 0.004782400765184123, 'mean_word_length': 2.159365558912387, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '7357966', 'n_tokens_mistral': 1284, 'n_tokens_neox': 1245, 'n_words': 240}