summaryrefslogtreecommitdiffstats
path: root/indexing/Main.js
blob: c0a0d4061181ca9a027b38f614be7209faa8a9cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
var searchApp = angular.module('SearchApp', ['ngResource']);

searchApp.factory('SearchAPI', ['$http',
    function ($http) {
        return {
           getDocuments: function () {
               var promise = $http.get('/documents').then(function(response) {
                    return response.data;
               }, function (error) {
                    return [];
               })
               return promise;
            },
            reindexDocuments: function () {
               var promise = $http.post('/reindex').then(function(response) {
                    return true;
               }, function (error) {
                    return false;
               })
               return promise;
            },
            search: function (query) {
                var queryJson = { "query" : query };
                var promise = $http.post('/search', queryJson).then(function(response) {
                    return response.data;
               }, function (error) {
                    return null;
               })
               return promise;
            },
            getResultImage: function(jsonResult) {
                var promise = $http.post('/image', jsonResult).then(function(response) {
                    return response.data;
               }, function (error) {
                    return null;
               })
               return promise;
            }
        }
    }
]);

searchApp.controller('SearchMainController', ['$scope', 'SearchAPI', function($scope, SearchAPI) {

$scope.searchResult = []

$scope.searchString = "";
$scope.status = "Ready";

$scope.reindexClicked = function() {
    $scope.status = "Reindexing....";

    SearchAPI.reindexDocuments().then(function(data) {
        $scope.status = "Finished reindexing";
    });
};

$scope.clearSearchResults = function() {
    $scope.searchResult = []
}

$scope.searchClicked = function() {
    $scope.searchResult = []
    SearchAPI.search($scope.searchString).then(function(jsonResult) {
        $scope.status = "Search finished " + jsonResult.length;
        for (let i = 0; i < jsonResult.length; i++) {
            let result = jsonResult[i];
            $scope.searchResult[i] = result;
            SearchAPI.getResultImage(result).then(function(image) {
                result.image = image;
            });
        }
    });

    $scope.status = "Searching... "  + $scope.searchString;
};

$scope.searchDisabled = function() {
    return $scope.searchString == "";
};

$scope.documents = []

SearchAPI.getDocuments().then(function(data) {
    $scope.documents = data;
});

$scope.documentSize = function() {
    return $scope.documents.length;
};

}]);