2017-03-01 4 views
0

내 구현에서 사용자가 클라이언트에서 검색하는 경우 문자열이 파일의 일부인지 확인하고 싶습니다. 나는 그 라인 데이터를 응답으로 클라이언트에 다시 보내고있다.파일 시스템에서 데이터를 읽고 클라이언트에게 전체 메시지를 보내는 방법은 무엇입니까?

내 문제는 사용자가 testid012을 검색하려고한다고 가정합니다. 따라서 현재 코드에서는 검색 문자열이 포함 된 단 한 줄만 찾습니다. 대신 검색 문자열 (여기서는 testid012)을 포함하는 여러 줄의 응답을 보내려합니다. 이게 가능합니까?

searchService.js

fs.readFile('logs/dit/' + logfile.filename, 'utf8', function (err, data) { 
      if (err) { 
       return done(err); 
      } 
      var lines = data.split('\n'); // get the lines 
      lines.forEach(function(line) { // for each line in lines 
       if (line.indexOf(searchStr) != -1) { // if the line contain the searchSt 
        results.push({ 
        filename:logfile.filename, 
        value:line 
        }); 
       } 
      }); 
      // when you are done reading the file 
      done(); 
     }); 

DIT/

testid012 Lorem test Ipsum is simply dummy text text of the printing and typesetting industrytestid Lorem test Ipsum is simply dummy text text of the printing and typesetting industrytestid Lorem test Ipsum is simply dummy text text of the printing and typesetting industry 
Lorem test Ipsum is simply dummy text text of the printing and typesetting industry,testid Lorem test Ipsum is simply dummy text text of the printing and typesetting industry 

testid013 Lorem test Ipsum is simply dummy text text of the printing and typesetting industry,testid Lorem test Ipsum is simply dummy text text of the printing and typesetting industrytestid Lorem test Ipsum is simply dummy text text of the printing and typesetting industry 
Lorem test Ipsum is simply dummy text text of the printing and typesetting industry 
+0

이 방법을 사용하지 않는 것이 좋습니다. Solr 내부에서 파일의 색인을 생성하고 Solr에 해당 검색어를 쿼리하거나 적어도 Solr이 없으면 사용하십시오. 그것은 당신이 가지고있는 어떤 데이터베이스에서도 권장하지는 않지만 적어도 파일 검색은 당신이하는 것처럼하는 것이 아닙니다. –

+0

이 요구 사항에 대해 데이터베이스를 사용할 수 없으며 파일 검색을 수행해야합니다. 좋은 예제는 어떻게 FS를 사용하여 달성 할 수 있습니다. – hussain

답변

0

귀하의 솔루션이 작동 server.log에,하지만 당신은 하나 개의 라인 당신이 선 말까지 입력을 분할하는 것입니다을 얻고있는 이유. 이벤트가 어떻게 분리되는지 파악하고 data.split() 전화를 변경해야하는 것처럼 보입니다.

[EVENT] This is an event과 같은 모든 이벤트를 시작하는 데이터가있는 경우이를 정규식으로 분할하십시오. data.split('[EVENT] ')

+0

우리는 Kafka 버스의 이벤트를 각 이벤트 문자열로 렌더링 한 다음 파일 시스템에 기록합니다. – hussain