var elasticsearch = require('elasticsearch');

if (sails.config.useElastic === true) {
    var CLIENT = new elasticsearch.Client({
        host: sails.config.elasticHost,
        log: sails.config.elasticLogLevel
    })
}

module.exports = function(req, res) {

    if (!CLIENT) {
        return res.notFound()
    }

    if (!req.body || !req.body.q || !req.socket) {
        return res.badRequest()
    }

    var query = req.body.q;

    CLIENT.search({
        index: 'search',
        from: 0,
        size: 100,
        body: {
            query: {
                query_string : {
                    fields : ["name", "id"],
                    query: query.replace(/\:/g, '\\:') + '*',
                    analyze_wildcard: true
                }
            }
        }
    }, function(err, result) {
        if (err) {
            sails.log.warn(err);
            return res.serverError()
        } else {
            console.log(result[0])
            sails.log.debug('Suggest options for %s: %d', query, result.hits.total);
            if (result.hits.total > 0) {
                return res.json(result.hits.hits)
            } else {
                return res.notFound()
            }
        }
    })

};

var elasticsearch = require('elasticsearch');

if (sails.config.useElastic === true) {
    var CLIENT = new elasticsearch.Client({
        host: sails.config.elasticHost,
        log: sails.config.elasticLogLevel
    })
}

module.exports = function(req, res) {

    if (!CLIENT) {
        return res.notFound()
    }

    if (!req.body || !req.body.q || !req.socket) {
        return res.badRequest()
    }

    var query = req.body.q;

    CLIENT.search({
        index: 'search',
        from: 0,
        size: 100,
        body: {
            query: {
                query_string : {
                    fields : ["name", "id"],
                    query: query.replace(/\:/g, '\\\:') + '*',
                    analyze_wildcard: true,
                    default_operator: 'AND'
                }
            }
        }
    }, function(err, result) {
        if (err) {
            sails.log.warn(err);
            return res.serverError()
        } else {
            console.log(result[0])
            sails.log.debug('Suggest options for %s: %d', query, result.hits.total);
            if (result.hits.total > 0) {
                return res.json(result.hits.hits)
            } else {
                return res.notFound()
            }
        }
    })

};

Allow searching for HPO term ID's and match only entries that contain all of the query words (instead of one of the query words).
Allow searching for HPO term ID's and match only entries that contain all of the query words (instead of one of the query words).
