node.js 和 the browser使用elasticsearch.js 來呼叫elasticsearch db

  • 2750
  • 0

摘要:node.js 和 the browser使用elasticsearch.js 來呼叫elasticsearch db

announcing elasticsearch.js for node.js and the browser

announcing elasticsearch.js for node.js and the browser

node js教學


數個月前,我們對於PHP, Ruby, Python, and Perl 發布client libraries,而今天我們將加入JavaScript進入這個家庭(client libraries)!這新的client 運行於Node.js 和 modern browsers,目的在解決同樣的問題,如下:


  • 提供訪問整個Elasticsearch REST API
  • 搭配cluster運行良好
  • 在需要時將自動的搜尋到nodes
  • 智能能的處理節點失效
  • 更容易的擴展,你可以更專注於你的業務邏輯。

如果你不懂這個正在開發的clients,你可以經由觀看release the clients! ruby, python, php, perl這篇文章而了解目的。在這篇文章中解釋了為什麼我們這麼要做client做。

就像其他的client一樣,Elasticsearch.js內的所有的client都在Apache2開源許可認證下授權。Elasticsearch.js入門比 Elasticsearch更容易。

取得 node.js module

於node.js專案下安裝此module,使用npm:

npm install elasticsearch


取得browser clinet

對於一個基本 瀏覽器的項目中,建立現在熱門的瀏覽器項目都可以在這裡找到。下載其中一個檔案,並解壓縮。你將在裡面找到這三個檔案,將符合你項目的檔案貼到你的項目中:

  • elasticsearch.jquery.js – jQuery 使用。
  • elasticsearch.angular.js – angular 使用。
  • elasticsearch.js – 在其它的情況下,可使用。

每個library實體建置對應到AJAX和之前承諾的對於每個不同的library所提供的功能


設置 client

現在你已經裝備好,要開始實踐了 。第一件事情,你將需要創建一個elasticsearch.Client的實體。以下是參數訂定的幾個範例程式,當你在創建一個實體的時候,你可以使用它。配置項目的圓整配置項目可以參考the configuration docs.

var elasticsearch = require('elasticsearch');

//連接到 localhost:9200 ,使用默認的設定。
var client = new elasticsearch.Client();

// 連接多個節點
// 使用輪循的方式,自動平衡流量
var client = elasticsearch.Client({
  hosts: [
    'elasticsearch1:9200',
    'elasticsearch2:9200'
  ]
});

// 連接 host's cluster
//以下這段 我不懂功能
//, sniff for the rest of the cluster right away, and
// again every 5 minutes
var client = elasticsearch.Client({
  host: 'elasticsearch1:9200',
  sniffOnStart: true,
  sniffInterval: 300000
});

// 使用https的方式,和驗證機制、主機路徑
// 、和查詢語法來連接到host
var client = new elasticsearch.Client({
  host: 'https://user:password@elasticsearch1/search?app=blog'
});


設置browser的client

// elasticsearch.js adds the elasticsearch namespace to the window
var client = elasticsearch.Client({ ... });

// elasticsearch.jquery.js adds the es namespace to the jQuery object
var client = jQuery.es.Client({ ... });

// elasticsearch.angular.js創建一個 elasticsearch module
//,它提供了一個elasticsearch工廠    
var app = angular.module('app', ['elasticsearch']);
app.service('es', function (esFactory) {
  return esFactory({ ... });
});

使用client實體去實現呼叫api

當你創建了client,你會發現呼叫API是很簡單的事情。

// 取得當前使用的cluster目前的狀態
// Note: 參數的部分是可選擇的, 你也可以只傳送一個不用傳參callback方法。
client.cluster.health(function (err, resp) {
  if (err) {
    console.error(err.message);
  } else {
    console.dir(resp);
  }
});

// index a document
client.index({
  index: 'blog',
  type: 'post',
  id: 1,
  body: {
    title: 'JavaScript Everywhere!',
    content: 'It all started when...',
    date: '2013-12-17'
  }
}, function (err, resp) {
  // ...
});

// search for documents (and also promises!!)
client.search({
  index: 'users',
  size: 50,
  body: {
    query: {
      match: {
        profile: 'elasticsearch'
      }
    }
  }
}).then(function (resp) {
  var hits = resp.body.hits;
});