Elasticsearch仅限10,000个结果

时间:2017-12-01 17:53:27

标签: node.js elasticsearch scroll

我正在使用nodejs并在elasticsearch中查询。我想要符合条件的输出。但是,由于弹性搜索中的max_result_window设置在弹性搜索中默认为10,000,因此我无法输出超过10,000条记录。

在谷歌搜索如何获得超过10,000条记录,我找到了2种方法: 1.使用: curl -XPUT "http://localhost:9200/my_index/_settings" -d '{ "index" : { "max_result_window" : 500000 } }' 我无法运行上述命令。有人可以说在nodejs中运行上面的方法。

  1. 使用滚动方法: 在这里,我可以设置时间限制,例如1m,但我无法使用_scrollid访问连续记录。

1 个答案:

答案 0 :(得分:0)

  1. 滚动
  2. async function getResultsFromElastic() {
            let responseAll = {};
            responseAll["hits"] = {};
            responseAll.hits.hits = [];
            const responseQueue = [];
        
            searchQuery = {
                            index: 'test',
                            type: 'records',
                            body: { 
                                    query: {
                                       "match_all": {}
                                    }
                            }
            }
            searchQuery.scroll='10s';
            searchQuery.size=10000;
        
            responseQueue.push(await esclient.search(searchQuery));
        
            while (responseQueue.length) {
              const response = responseQueue.shift();
        
              responseAll.hits.hits = responseAll.hits.hits.concat(response.hits.hits);
        
              if (response.hits.total == responseAll.hits.hits.length) {
                break;
              }
        
              // get the next response if there are more to fetch
              responseQueue.push(
                await esclient.scroll({
                  scrollId: response._scroll_id,
                  scroll: '30s'
                })
              );
            }
        
            return responseAll;
    }

    1. 从/尺寸
    2. async function getLocationsFromElastic(searchQuery) {
          let responseAll = {};
          responseAll["hits"] = {};
          responseAll.hits.hits = [];
          searchQuery.from = searchQuery.from || 0;
          searchQuery.size = searchQuery.size || 10000; //If there are more than 10k results from Elastic, the elastic client throws an error.
      
          while( true ) {
              let response = await esclient.search(searchQuery);
              responseAll.hits.hits = responseAll.hits.hits.concat(response.hits.hits);
      
              if (response.hits.total == responseAll.hits.hits.length) {
                break;
              } else {
                searchQuery.from += 10000;
              }
          }
          return responseAll;
      }