<commit_before>/*!
 * Copyright 2012 Sakai Foundation (SF) Licensed under the
 * Educational Community 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.osedu.org/licenses/ECL-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.
 */
var _ = require('underscore');
var AuthzAPI = require('oae-authz');
var log = require('oae-logger').logger('oae-search-general');
var SearchUtil = require('oae-search/lib/util');
var SearchModel = require('oae-search/lib/model');
var client = require('oae-search/lib/elasticsearch');

/**
 * General search that searches a 'general' analyzed field on content, scoping it by user access.
 *
 * @param   {Context}       ctx                 The context of the current request
 * @param   {Object}        transformers        A hash keyed by resource type, and the value is a function that transforms search docs
 *                                              into docs that can be displayed to the current user.
 * @param   {Object}        opts                General search options
 * @param   {Function}      callback            Invoked when the process completes
 * @param   {Object}        callback.err        An error that occurred, if any
 * @param   {SearchResult}  callback.results    An object that represents the results of the query
 */
module.exports = function(ctx, transformers, opts, callback) {
    if (ctx.user() && !ctx.user().isGlobalAdmin() && opts.resourceType !== 'user') {
        // we'll need to know the group membership of this user to scope by content they have access to
        AuthzAPI.getPrincipalMemberships(ctx.user().id, function(err, groups) {
            if (err) {
                return callback(err);
            }

            opts.access = groups;
            _search(ctx, transformers, opts, callback);
        });
    } else {
        _search(ctx, transformers, opts, callback);
    }
}

/**
 * Perform the search that searches a 'general' analyzed field on content, scoping it by user access. This is delegated from the
 * `module.exports` function for convenience, as it will access the members array only if necessary.
 *
 * @param   {Context}       ctx                 The context of the current request
 * @param   {Object}        transformers        A hash keyed by resource type, and the value is a function that transforms search docs
 *                                              into docs that can be displayed to the current user.
 * @param   {Object}        opts                General search options
 * @param   {Function}      callback            Invoked when the process completes
 * @param   {Object}        callback.err        An error that occurred, if any
 * @param   {SearchResult}  callback.results    An object that represents the results of the query
 */
var _search = function(ctx, transformers, opts, callback) {
    var data = {};

    // the query object for the Query DSL
    var query = {
        'query_string': {
            'default_field': 'general',
            'query': opts.q
        }
    };

    // the filter object for the Query DSL
    var filter = null;

    // the type filter will filter by a particular document type (content, user, group)
    var typeFilter = (opts.resourceType !== 'all') ? SearchUtil.filterTerm('_type', opts.resourceType) : null;

    // the access filter will filter by the user's group membership
    var accessFilter = SearchUtil.filterTerms('access', opts.access);

    if (ctx.user() && ctx.user().isGlobalAdmin()) {
        // if user is global admin, forget filtering by members and visibility, just query *everything*
        filter = typeFilter;
    } else if (ctx.user() && ctx.user().isTenantAdmin(ctx.tenant().alias)) {
        // if the user is the tenant admin, query everything only for their tenant, and apply regular "logged in user" filters<commit_msg>(Search) Changing 'query string' query to 'match' query as it does not have strict lucene syntax.<commit_after>/*!
 * Copyright 2012 Sakai Foundation (SF) Licensed under the
 * Educational Community 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.osedu.org/licenses/ECL-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.
 */
var _ = require('underscore');
var AuthzAPI = require('oae-authz');
var log = require('oae-logger').logger('oae-search-general');
var SearchUtil = require('oae-search/lib/util');
var SearchModel = require('oae-search/lib/model');
var client = require('oae-search/lib/elasticsearch');

/**
 * General search that searches a 'general' analyzed field on content, scoping it by user access.
 *
 * @param   {Context}       ctx                 The context of the current request
 * @param   {Object}        transformers        A hash keyed by resource type, and the value is a function that transforms search docs
 *                                              into docs that can be displayed to the current user.
 * @param   {Object}        opts                General search options
 * @param   {Function}      callback            Invoked when the process completes
 * @param   {Object}        callback.err        An error that occurred, if any
 * @param   {SearchResult}  callback.results    An object that represents the results of the query
 */
module.exports = function(ctx, transformers, opts, callback) {
    if (ctx.user() && !ctx.user().isGlobalAdmin() && opts.resourceType !== 'user') {
        // we'll need to know the group membership of this user to scope by content they have access to
        AuthzAPI.getPrincipalMemberships(ctx.user().id, function(err, groups) {
            if (err) {
                return callback(err);
            }

            opts.access = groups;
            _search(ctx, transformers, opts, callback);
        });
    } else {
        _search(ctx, transformers, opts, callback);
    }
}

/**
 * Perform the search that searches a 'general' analyzed field on content, scoping it by user access. This is delegated from the
 * `module.exports` function for convenience, as it will access the members array only if necessary.
 *
 * @param   {Context}       ctx                 The context of the current request
 * @param   {Object}        transformers        A hash keyed by resource type, and the value is a function that transforms search docs
 *                                              into docs that can be displayed to the current user.
 * @param   {Object}        opts                General search options
 * @param   {Function}      callback            Invoked when the process completes
 * @param   {Object}        callback.err        An error that occurred, if any
 * @param   {SearchResult}  callback.results    An object that represents the results of the query
 */
var _search = function(ctx, transformers, opts, callback) {
    var data = {};

    // the query object for the Query DSL
    var query = {
        'match': {
            'general': {
                'query': opts.q,
                'operator': 'and'
            }
        }
    };

    // the filter object for the Query DSL
    var filter = null;

    // the type filter will filter by a particular document type (content, user, group)
    var typeFilter = (opts.resourceType !== 'all') ? SearchUtil.filterTerm('_type', opts.resourceType) : null;

    // the access filter will filter by the user's group membership
    var accessFilter = SearchUtil.filterTerms('access', opts.access);

    if (ctx.user() && ctx.user().isGlobalAdmin()) {
        // if user is global admin, forget filtering by members and visibility, just query *everything*
        filter = typeFilter;
    } else if (ctx.user() && ctx.user().isTenantAdmin(ctx.tenant().alias)) {
        // if the user is the tenant admin, query everything only for their tenant, and apply regular "logged in user" filters
{'fraction_non_alphanumeric': 0.09098370266150779, 'fraction_numerical': 0.001875952632196037, 'mean_word_length': 2.5335542667771334, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6880747', 'n_tokens_mistral': 2161, 'n_tokens_neox': 2094, 'n_words': 921}