Angularjs 如何将弹性搜索与Meanjs(Mongodb、Express、Angular、Nodejs)集成
我已经用grunt安装了MEANJS。其现有模块工作正常。问题是我正在尝试将弹性搜索与angular Js集成。但是没有得到任何合适的解决方案。当我将elastic search连接到server.js时,终端会显示搜索结果。如何在主页上通过angular js显示搜索结果 我还想将弹性数据库与mongodb数据库连接起来,以便弹性搜索能够自动更新。任何建议对我都很有帮助。 通过我使用的弹性搜索进行连接Angularjs 如何将弹性搜索与Meanjs(Mongodb、Express、Angular、Nodejs)集成,angularjs,node.js,mongodb,
elasticsearch,meanjs,Angularjs,Node.js,Mongodb,
elasticsearch,Meanjs,我已经用grunt安装了MEANJS。其现有模块工作正常。问题是我正在尝试将弹性搜索与angular Js集成。但是没有得到任何合适的解决方案。当我将elastic search连接到server.js时,终端会显示搜索结果。如何在主页上通过angular js显示搜索结果 我还想将弹性数据库与mongodb数据库连接起来,以便弹性搜索能够自动更新。任何建议对我都很有帮助。 通过我使用的弹性搜索进行连接 var MyOpenRecipes = angular.module('myOpenRe
var MyOpenRecipes = angular.module('myOpenRecipes', ['elasticsearch'],
['$locationProvider', function($locationProvider){
$locationProvider.html5Mode(true);
}]
);
MyOpenRecipes.factory('recipeService',
['$q', 'esFactory', '$location', function($q, elasticsearch, $location){
var client = elasticsearch({
host: $location.host() + ":9200"
});
/**
* Given a term and an offset, load another round of 10 recipes.
*
* Returns a promise.
*/
var search = function(term, offset){
var deferred = $q.defer();
var query = {
"match": {
"_all": term
}
};
client.search({
"index": 'facilities',
"type": 'facility',
"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
};
}]
);
var MyOpenRecipes=angular.module('MyOpenRecipes',['elasticsearch'],
['$locationProvider',函数($locationProvider){
$locationProvider.html5Mode(true);
}]
);
MyOpenRecipes.factory(“配方服务”,
['$q','esFactory','$location',函数($q,elasticsearch,$location){
var client=elasticsearch({
主机:$location.host()+“:9200”
});
/**
*给定一个术语和偏移量,加载另一轮10个配方。
*
*回报一个承诺。
*/
var搜索=函数(术语、偏移量){
var deferred=$q.deferred();
变量查询={
“匹配”:{
“_all”:术语
}
};
client.search({
“索引”:“设施”,
“类型”:“设施”,
“正文”:{
“尺寸”:10,
“从”:(偏移量| | 0)*10,
“查询”:查询
}
}).然后(函数(结果){
var ii=0,输入输入,输出=[];
hits|u in=(result.hits | |{})。hits | |[];
对于(;ii
基本上,您要做的是:
- 运行弹性搜索(ES)服务器
- 在服务器端代码(MEAN)中,您将编写一个处理搜索的路由
- 让你的角代码发送请求到你的后端路由,通过ES进行搜索
您不想让Angular通过网络直接与ES通话——好吧,这样做没有安全的方法。Hi终于找到了解决方案 我已经附上了elastic.angular.js文件/var/www/meanjs/config/env/all.js 在/var/www/meanjs/public/modules/core/controllers/home.client.controller中。我已经写了下面的代码,它的工作与搜索顺利
angular.module('core').factory('recipeService',
['$q', 'esFactory', '$location', function($q, elasticsearch, $location){
var client = elasticsearch({
host: $location.host() + ':9200'
});
/**
* Given a term and an offset, load another round of 10 recipes.
*
* Returns a promise.
*/
var search = function(term, offset){
var deferred = $q.defer();
var query = {
'match': {
'_all': term
}
};
client.search({
'index': 'facilities',
'type': 'facility',
'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
};
}]
);
angular.module('core').controller('recipeCtrl',
['recipeService', '$scope', '$location', function(recipes, $scope, $location){
// Provide some nice initial choices
var initChoices = [
'ADS AMBULATORY SURGERY CTR',
'NOVAMED EYE SURGERY CENTER OF OVERLAND PARK',
'DISCOVER VISION SURGERY & LASER CENTER LLC',
'HUTCHINSON AMBULATORY SURGERY CENTER LLC',
'SHAWNEE MISSION PRAIRIE STAR SURGERY CENTER LLC',
'LASER CENTER',
'QUINLAN EYE SURGERY & LASER CENTER',
'ADS AMBULATORY SURGERY CTR'
];
var idx = Math.floor(Math.random() * initChoices.length);
// Initialize the scope defaults.
$scope.recipes = []; // An array of recipe results to display
$scope.page = 0; // A counter to keep track of our current page
$scope.allResults = false; // Whether or not all results have been found.
// And, a random search term to start if none was present on page load.
$scope.searchTerm = $location.search().q || initChoices[idx];
/**
* A fresh search. Reset the scope variables to their defaults, set
* the q query parameter, and load more results.
*/
$scope.search = function(){
$scope.page = 0;
$scope.recipes = [];
$scope.allResults = false;
$location.search({'q': $scope.searchTerm});
$scope.loadMore();
};
/**
* Load the next page of results, incrementing the page counter.
* When query is finished, push results onto $scope.recipes and decide
* whether all results have been returned (i.e. were 10 results returned?)
*/
$scope.loadMore = function(){
recipes.search($scope.searchTerm, $scope.page++).then(function(results){
if(results.length !== 10){
$scope.allResults = true;
}
var ii = 0;
for(;ii < results.length; ii++){
$scope.recipes.push(results[ii]);
}
});
};
// Load results on first run
$scope.loadMore();
}]
);
angular.module('core').factory('recipeService'),
['$q','esFactory','$location',函数($q,elasticsearch,$location){
var client=elasticsearch({
主机:$location.host()+':9200'
});
/**
*给定一个术语和偏移量,加载另一轮10个配方。
*
*回报一个承诺。
*/
var搜索=函数(术语、偏移量){
var deferred=$q.deferred();
变量查询={
“匹配”:{
“所有人”:术语
}
};
client.search({
“索引”:“设施”,
“类型”:“设施”,
“身体”:{
“大小”:10,
“from”:(偏移量| | 0)*10,
“查询”:查询
}
}).然后(函数(结果){
var ii=0,输入输入,输出=[];
hits|u in=(result.hits | |{})。hits | |[];
对于(;ii
您没有提供足够的信息,也没有解释可能提供帮助的特定问题。查看“关于提问”部分,获取有关此问题的建议。