[AngularJS/Elasticsearch] 使用AngularJS為基於Elasticsearch的應用建立前端,elasticsearch

來源:互聯網
上載者:User

[AngularJS/Elasticsearch] 使用AngularJS為基於Elasticsearch的應用建立前端,elasticsearch
使用AngularJS為基於Elasticsearch的應用建立前端

如果使用Elasticsearch使用應用的資料來源,我們可以很方便的使用AngularJS結合Elasticsearch提供的相關模組為它建立一個前端。

以建立一個簡單的員工資訊花名冊為例。

準備工作

準備工作分為以下兩個方面:

添加前端依賴
  1. 安裝Bower
  2. 在bower.json中添加對於AngularJS和Elasticsearch的依賴:
"dependencies": {    "angular": "~1.2.15",    "elasticsearch": "~2.4.0"}
準備運行時環境

對於簡單的應用和Demo,可以直接使用Node環境中提供的http-server,非常簡單快捷。

  1. 安裝Node
  2. 安裝http-server,通過命令:npm install -g http-server
配置AngularJS以及ES Client配置AngularJS
var rosterApp = angular.module('rosterApp', ['elasticsearch']);
配置Elasticsearch Client

對於一個唯讀應用,我們只需要將搜尋的功能暴露出來。可以通過自訂一個factory來實現:

rosterApp.factory('rosterService',    ['$q', 'esFactory', '$location', function($q, elasticsearch, $location){        var client = elasticsearch({            host: $location.host() + ":9200"        });        var search = function(term, offset){            var deferred = $q.defer();            var query = {                "match": {                    "_all": term                }            };            client.search({                "index": 'roster',                "type": 'employee',                "body": {                    "size": 10,                    "from": (offset || 0) * 10,                    "query": query                }            }).then(function(result) {                var ii = 0, hits_in, hits_out = [];                hits_in = (result.hits || {}).hits || [];                for(;ii < hits_in.length; ii++){                    hits_out.push(hits_in[ii]._source);                }                deferred.resolve(hits_out);            }, deferred.reject);            return deferred.promise;        };        return {            "search": search        };    }]);

在以上定義的factory中,我們只暴露了search方法。該方法調用了client的search方法完成基於關鍵字的搜尋。

控制器(Controller)

下面需要做的就是實現一個控制器來完成頁面與上述search方法的互動:

rosterApp.controller('rosterCtrl',    ['rosterService', '$scope', '$location', function(rosterService, $scope, $location){        $scope.employees = [];        $scope.page = 0;        $scope.allResults = false;        $scope.searchTerm = '';        $scope.search = function(){            $scope.page = 0;            $scope.employees = [];            $scope.allResults = false;            $location.search({'q': $scope.searchTerm});            $scope.loadMore();        };        $scope.loadMore = function(){            rosterService.search($scope.searchTerm, $scope.page++).then(function(results){                if(results.length !== 10){                    $scope.allResults = true;                }                var ii = 0;                for(;ii < results.length; ii++){                    $scope.employees.push(results[ii]);                }            });        };        $scope.loadMore();    }]);

search方法用來執行搜尋,loadMore方法用於載入更多記錄。

頁面

頁面主要用於展示Employee對象的具體屬性。

<div ng-app='rosterApp' ng-controller='rosterCtrl'>    <header>        <h1>Employee Search</h1>    </header>    <section class='searchField'>        <form ng-submit='search()'>            <input ng-model='searchTerm' type='text'>            <input type='submit' value='Search for employees'>        </form>    </section>    <section class='results'>       <div class='no-employees' ng-hide='employees.length'>No results</div>       <article class='employee' ng-cloak ng-repeat='employee in employees'>           <h2>              <a ng-href='{{employee.link}}'>{{employee.name}}</a>           </h2>           <ul>              <li ng-repeat='historyItem in employee.historyItems'>{{ historyItem }}</li>           </ul>           <p>              {{employee.introduction}}              <a ng-href='{{employee.link}}'>More information...</a>           </p>       </article>       <div class='load-more' ng-cloak ng-hide='allResults'>            <a ng-click='loadMore()'>More...</a>       </div>    </section></div>
運行

進入工程目錄,運行http-server。訪問http://localhost:8080。

當需要使用不同的查詢類型時,通過修改search方法中query的建立部分即可。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.