2010-07-26 5 views
2

lucene (Java 용 버전 3.0.2) 쿼리에서 일치 항목의 시작과 끝 위치를 찾고 싶습니다. 형광펜이나 FastVectorHighligher에서이 정보를 얻을 수 있어야하지만,이 클래스는 관련 텍스트가 강조 표시된 텍스트 조각 만 반환하는 것처럼 보입니다. 형광펜이나 ScoreDoc 자체에서이 정보를 얻을 수있는 방법이 있습니까?Lucene과 일치 항목의 시작과 끝 찾기

업데이트 :이 관련 질문을 발견 : Finding the position of search hits from Lucene

을하지만 내 쿼리 구문이 아닌 개별 용어 때문에 Allasso에 의한 대답은 나를 위해 작동하지 않습니다 생각합니다.

답변

5

내가 당신이라면 FastVectorHighlighter에서 코드를 가져옵니다. 관련 코드는 FieldTermStack에 있습니다.

 List<string> termSet = fieldQuery.getTermSet(fieldName); 
     VectorHighlightMapper tfv = new VectorHighlightMapper(termSet);  
     reader.GetTermFreqVector(docId, fieldName, tfv); // <-- look at this line 

     string[] terms = tfv.GetTerms(); 
     foreach (String term in terms) 
     { 
      if (!termSet.Contains(term)) continue; 
      int index = tfv.IndexOf(term); 
      TermVectorOffsetInfo[] tvois = tfv.GetOffsets(index); 
      if (tvois == null) return; // just return to make null snippets 
      int[] poss = tfv.GetTermPositions(index); 
      if (poss == null) return; // just return to make null snippets 
      for (int i = 0; i < tvois.Length; i++) 
       termList.AddLast(new TermInfo(term, tvois[i].GetStartOffset(), tvois[i].GetEndOffset(), poss[i])); 

주요한 점은 reader.GetTermFreqVector()입니다. 앞서 말했듯이, FastVectorHighlighter는 이미 복사 할 수있는 일부 작업을 수행하지만 원하는 경우 GetTermPositions 호출은 필요한 모든 작업을 수행해야합니다.

+0

Lucene Java 3.0.2를 사용하고 있다고 지정 했어야합니다. 그래도 FastVectorHighlighter에 대한 코드를 살펴보고 내가 필요한 것을 얻을 수 있는지 확인합니다. –

+0

@Mike : 죄송합니다. C# 구문이 Java에 충분히 가깝다고 생각했습니다. 어쨌든 TermPositionsVector는 원하는 것을해야합니다. 문구를 강조하고 싶기 때문에 조금 거칠 것입니다 (서로 옆에있는 문구를 찾아야합니다).하지만 너무 나쁘지 않아야합니다. – Xodarap

관련 문제