2016-11-10 40 views
1

我正嘗試在我的Node.js應用程序中將Firebase Flashlight與ElasticSearch集成,以便對我的Firebase集合進行搜索操作。我想在api.js這裏定義我的搜索路線(例如:myhost: myport/ search/mycollection/:key)。如何將Firebase手電筒集成到我的應用程序中

問題是myport與ElasticSearch正在運行的是不同的(它是9200)。

我想在路線上:myhost: myport/whatever運行我的應用程序,並在路線myhost: myport/ search/mycollection/:key運行搜索,現在可在myhost:9200

如何將它們集成到Express中?

當我配置ElasticSearch,我使用:

var config = { 
     host: 'localhost', 
     port: 9200, 
     log: 'trace' 
}; 
var esc = new ElasticSearch.Client(config); 

回答

1

這是正常的,彈性的搜索將不同的端口比你Express應用程序上運行。實際上,您可以在而不是的兩臺服務器上運行相同的端口。

手電筒更像是一個不同的應用程序,您需要在您的服務器上運行。使用npm startnpm monitor您可以在設置配置文件後啓動手電筒過程。 Flashlight將小心爲您提供Firebase中的彈性搜索數據。

要與Elastic Search交互,您只需使用Node模塊。你已經這樣做了。如您所述,Elastic Search將在端口9200上運行,而Express應用程序將運行在不同的端口上(例如3000)。

受到Flashlight的啓發,我創建了Elasticfire,它具有Flashlight不具備的一些功能(例如連接),並且可以在應用程序中用作庫。

const ElasticFire = require("elasticfire"); 

// Initialize the ElasticFire instance 
let ef = new ElasticFire({ 

    // Set the Firebase configuration 
    firebase: { 
     apiKey: "AI...BY", 
     authDomain: "em...d.firebaseapp.com", 
     databaseURL: "https://em...d.firebaseio.com", 
     storageBucket: "em...d.appspot.com", 
     messagingSenderId: "95...36" 
    } 

    // Firebase paths and how to index them in Elasticsearch 
    , paths: [ 
     { 
      // Firebase path 
      path : "articles" 

      // Elasticsearch index and type 
     , index: "articles" 
     , type : "article" 

      // Optional joined fields 
     , joins: [ 
       // The `author` is a field from the article 
       // which points to the `users` collection. 
       { 
        path: "users" 
       , name: "author" 
       } 

       // If we have an array of comment ids, pointing 
       // to another collection, then they will be joined too 
      , { 
        path: "comments" 
       , name: "comments" 
       } 
      ] 

      // Filter out some data 
     , filter: (data, snap) => snap.key !== "_id" 
     } 
    ] 
}); 

// Listen for the events emitted by 
// the ElasticFire instanceand output some data 
ef.on("error", err => { 
    console.error(err); 
}).on("index_created", name => { 
    console.log(`${name} created`); 
}).on("index_updated", name => { 
    console.log(`${name} updated`); 
}).on("index_deleted", name => { 
    console.log(`${name} deleted`); 
}); 
相關問題