简体   繁体   English

使用Node测试ElasticSearch。 填充MongoDB后强制更新索引

[英]Testing ElasticSearch with Node. Force update index after populating MongoDB

I'm populating MongoDB and adding river-mongodb in a before hook in my tests. 我正在测试中填充MongoDB并在before钩子中添加river-mongodb But it looks like Elasticsearch hasn't indexed the data I have in MongoDB when I query it. 但是当我查询时,Elasticsearch似乎没有索引我在MongoDB中拥有的数据。 Am I doing it wrong, or should I force an update somehow? 我做错了还是应该以某种方式强制进行更新?

  function setupMongoDBRiver() {
      var url = 'http://127.0.0.1:9200/_river/mongodb/_meta';
      var requestData = {
          "type": "mongodb",
          "mongodb": {
              "db": "harvester-test",
              "collection": "entries"
          },
          "index": {
              "name": "harvester-test",
              "type": "entries"
          }
      };
      request({
          url: url,
          method: 'PUT',
          body: JSON.stringify(requestData)
      },
  }



 describe('Entries Test Parameters', function() {
    before(function(done) {
        DatabaseHelper.restoreAll(mongoUrl, done);
        deleteTestIndex();
        setupMongoDBRiver();
        testQuery();  
       // ^ curl http://127.0.0.1:9200/harvester-test/entries/_search?pretty=true&q=*:*
    }

testQuery Does not return what I put in MongoDB. testQuery不返回我放在MongoDB中的内容。 Could be that ES needs more time to index the data? ES可能需要更多时间来索引数据吗? Do I have to specify the ES index, or is that taken care of by river-mongodb? 我是否必须指定ES索引,还是由river-mongodb处理?

Cheers, Martin 马丁,干杯

There are a few things to note about your before() function which seem to suggest that you haven't fully grasped how asynchronous function calling works on Node. 关于before()函数,有几件事需要注意,这似乎表明您尚未完全了解异步函数调用在Node上的工作方式。

Let me walk through the steps: 让我逐步执行以下步骤:

  DatabaseHelper.restoreAll(mongoUrl, done);

I assume that this function resets or restores your MongoDB to a predefined state. 我假设此功能会将MongoDB重置或还原到预定义状态。 You're calling Mocha's done callback when it has finished (which signals to Mocha that the before() is finished), yet continue to call other functions. 您正在done时调用Mocha的done回调(这向Mocha发出信号,通知before()已完成),但继续调用其他函数。 You should call done when everything is ready, not just the first function. 一切准备就绪时,您应该调用done ,而不仅仅是第一个函数。

  deleteTestIndex();
  setupMongoDBRiver();
  testQuery();

These are all asynchronous functions. 这些都是异步功能。 In other words, you need to explicitly wait for their completion before continuing on to the next step (you first want to delete the test index, then set up the MongoDB river, then run the test query). 换句话说,您需要明确地等待它们的完成,然后再继续下一步(您首先要删除测试索引, 然后设置MongoDB river, 然后运行测试查询)。

A good module to coordinate asynchronous functions is async . 一个好的模块以协调异步函数是async You can use async.series (or perhaps async.waterfall ) to coordinate your function calls, but it would require some rewriting (for instance, your setupMongoDBRiver() function doesn't call a completion callback). 您可以使用async.series (或async.waterfall )来协调函数调用,但是这需要进行一些重写(例如,您的setupMongoDBRiver()函数不会调用完成回调)。

Assuming that you rewrite your code to use completion callbacks, your before() would look something similar to this: 假设您重写代码以使用完成回调,则before()看起来类似于以下内容:

before(function(done) {
  async.series([
    function(next) {
      DatabaseHelper.restoreAll(mongoUrl, next);
    },
    function(next) {
      deleteTestIndex(next);
    },
    function(next) {
      setupMongoDBRiver(next);
    },
    function(next) {
      testQuery(next);
    }
  ], function(err) {
    if (err) return done(err);
    done();
  });
});

(NB: this can be written more concisely but I used verbose notation for clarity) (注意:这可以写得更简洁,但是为了清楚起见,我使用了冗长的符号)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM