13

基本的に、インデックス タイプのすべてのレコードを表示しようとしています。これで、クエリで match_all() を使用すると、elasticsearch はデフォルトで 10 件の結果を表示します。スクロールを使用してすべての結果を表示できます。スクロール API を実装しようとしていますが、動作させることができません。私のコードは10件の結果しか表示されていません:

module.exports.searchAll = function (searchData, callback) {

client.search({
    index: 'test',
    type: 'records',
    scroll: '10s',
    //search_type: 'scan', //if I use search_type then it requires size otherwise it shows 0 result
    body: {
        query: {
            "match_all": {}
        }
    }
}, function (err, resp) {
    client.scroll({
        scrollId: resp._scroll_id,
        scroll: '10s'
    }, callback(resp.hits.hits));
});
}

誰でも助けてもらえますか?

4

7 に答える 7

21

client.scrollレコードが返されなくなるまで繰り返し呼び出す必要があります。elasticsearch documentation に良い例があります。以下のサンプルコードを再現しました。質問に合わせてわずかに変更しました

var allRecords = [];

// first we do a search, and specify a scroll timeout
client.search({
  index: 'test',
  type: 'records',
  scroll: '10s',
  body: {
     query: {
         "match_all": {}
     }
  }
}, function getMoreUntilDone(error, response) {
  // collect all the records
  response.hits.hits.forEach(function (hit) {
    allRecords.push(hit);
  });

  if (response.hits.total !== allRecords.length) {
    // now we can call scroll over and over
    client.scroll({
      scrollId: response._scroll_id,
      scroll: '10s'
    }, getMoreUntilDone);
  } else {
    console.log('all done', allRecords);
  }
});
于 2016-09-21T20:12:33.930 に答える