2017-10-26 3 views
0

이 방법으로 ElasticSearch에서 결과를 얻습니다. 내가 그들을 점점 알 수 있도록 나는 결과를 기록 할 수 있습니다 ..이 약속 (async/await)이 대기중인 이유가 무엇입니까

async querySearch(queryObj) { 
    let hits = []; 
    const client = this.client; 
    client.search({ 
    index: this.index, 
    scroll: '30s', 
    body: { 
     query: { 
     match: { 
      _all: queryObj.q, 
     }, 
     }, 
    }, 
    }, async function getNextResults(error, response) { 
    console.log(' getting more... '); 
    response.hits.hits.forEach((hit) => { 
     hits.push(hit); 
     console.log(' >> hits is ', hits.length); 
    }); 

    if (response.hits.total > hits.length) { 
     await client.scroll({ 
     scrollId: response._scroll_id, 
     scroll: '30s' 
     }, await getNextResults); 
    } else { 
     console.log(`NOW ${response.hits.total} <= ${hits.length}`); 
     console.log(' XXX hits is ', hits.length); 
     // console.log('got lota of results : ', hits); 
     return hits; 
    } 
    }); 
} 

그러나 발신자, 여기, '정의되지 않은'도착하고 전달하는 결과를 기다리지 않습니다를 ...

async querySearch(queryObj) { 
    const hits = await this.connector.querySearch(queryObj); 

    return hits; 
    } 

왜 이런 일이 발생하며 어떻게 해결할 수 있습니까? 내 하위 방법에서 내 히트 곡을 어떻게 얻을 수 있습니까?

+0

원인 * querySearch * 아무것도 (일명 ​​* 정의되지 않은 *) –

+2

'client.scroll 기다리고의 예상 결과는 무엇을 반환하지? – guest271314

+0

@ guest271314, ElasticSearch 결과의 다음 세트 (다음 스크롤 페이지)를 가져옵니다. 하위 메소드는 스크롤 페이지를 반복하여 모든 결과를 얻습니다 (모두 약 200 개). – brainstormtrooper

답변

1

@ Kaiido의 조언 덕분에 방금 내 메서드를 다시 작성하고 콜백을 제거했습니다. 콜백을 별도의 메서드로 만들었으니 이제는 모두 잘 작동합니다. ({: response._scroll_id, 스크롤 '30' scrollId} 기다리고 getNextResults)`