<commit_before>define(['lodash'], function(_) {
    'use strict';

    ArchiveListController.$inject = ['$scope', '$location', 'superdesk', 'api', 'es'];
    function ArchiveListController($scope, $location, superdesk, api, es) {
        $scope.view = 'mgrid';
        $scope.selected = {};

        $scope.preview = function(item) {
            $scope.selected.preview = item;
        };

        $scope.display = function(item) {
            $scope.selected.view = item;
        };

        $scope.openUpload = function() {
            superdesk.intent('upload', 'media').then(function(items) {
                // todo: put somewhere else
                $scope.items.collection.unshift.apply($scope.items.collection, items);
            });
        };

        $scope.$watchCollection(function() {
            return $location.search();
        }, function(search) {
            var query = getQuery(search);
            fetchItems({source: query});
        });

        function buildFilters(params) {
            var filters = [];

            if (params.before || params.after) {
                var range = {VersionCreated: {}};
                if (params.before) {
                    range.VersionCreated.lte = params.before;
                }

                if (params.after) {
                    range.VersionCreated.gte = params.after;
                }

                filters.push({range: range});
            }

            return filters;
        }

        function getQuery(params) {
            var filters = buildFilters(params);
            return es(params, filters);
        }

        function fetchItems(criteria) {
            api.archive.query(criteria).then(function(items) {
                items._items = _.pluck(items.hits.hits, '_source');
                items.collection = items._items;
                $scope.items = items;
            });
        }
    }

    return ArchiveListController;
});<commit_msg>fix(media): set sort for item list<commit_after>define(['lodash'], function(_) {
    'use strict';

    ArchiveListController.$inject = ['$scope', '$location', 'superdesk', 'api', 'es'];
    function ArchiveListController($scope, $location, superdesk, api, es) {
        $scope.view = 'mgrid';
        $scope.selected = {};

        $scope.preview = function(item) {
            $scope.selected.preview = item;
        };

        $scope.display = function(item) {
            $scope.selected.view = item;
        };

        $scope.openUpload = function() {
            superdesk.intent('upload', 'media').then(function(items) {
                // todo: put somewhere else
                $scope.items.collection.unshift.apply($scope.items.collection, items);
            });
        };

        $scope.$watchCollection(function() {
            return $location.search();
        }, function(search) {
            var query = getQuery(search);
            fetchItems({source: query});
        });

        function buildFilters(params) {
            var filters = [];

            if (params.before || params.after) {
                var range = {VersionCreated: {}};
                if (params.before) {
                    range.VersionCreated.lte = params.before;
                }

                if (params.after) {
                    range.VersionCreated.gte = params.after;
                }

                filters.push({range: range});
            }

            return filters;
        }

        function getQuery(params) {
            var filters = buildFilters(params);
            var query = es(params, filters);
            query.sort = ['_score', {VersionCreated: 'desc'}];
            return query;
        }

        function fetchItems(criteria) {
            api.archive.query(criteria).then(function(items) {
                items._items = _.pluck(items.hits.hits, '_source');
                items.collection = items._items;
                $scope.items = items;
            });
        }
    }

    return ArchiveListController;
});
{'fraction_non_alphanumeric': 0.132334581772784, 'fraction_numerical': 0.0, 'mean_word_length': 1.6114732724902217, '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': '3218016', 'n_tokens_mistral': 1066, 'n_tokens_neox': 1011, 'n_words': 188}