1
我是Lucene的新手,當我嘗試在搜索後得到一個hitcount時,我迷路了。請幫忙。 順便說一句,我正在使用Netbeans IDE。預先感謝您的幫助。Lucene 3.0 - 爲什麼我無法獲得正確的計數?
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pdflucene;
/**
*
* @author nigel
*/
import java.io.IOException;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;
public class ExtractPageContent {
public static final String inn = "/Users/nigel/Desktop/Statement_Week9/AlanWilder.pdf";
public static final String PATH = "/Users/nigel/Desktop/Statement_Week9";
public void parsePdf(String pdf) throws IOException {
String ts="";
String content="";
String [] queryStrings;
try{
String temp = "";
PDDocument pdfdocument = PDDocument.load(pdf);
Integer n=pdfdocument.getNumberOfPages();
PDFTextStripper stripper = new PDFTextStripper();
RAMDirectory idx = new RAMDirectory();
stripper.setStartPage(1);
stripper.setEndPage(4);
stripper.setSortByPosition(false);
pdfdocument.close();
IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_30, new StandardAnalyzer(Version.LUCENE_30));
IndexWriter writer = new IndexWriter(idx, conf);
Document mydoc = new Document();
mydoc.add(new Field("content", "Alan is Alan not quite alan",Field.Store.NO,Field.Index.ANALYZED));
writer.addDocument(mydoc) ;
writer.close();
IndexSearcher ss=new IndexSearcher(idx,true);
Term t = new Term("content", "Alan");
TermQuery pq= new TermQuery(t);
int results = ss.search(pq,1).totalHits;
System.out.println("total hits: " + results);
ss.close();
System.out.println("number of pages " + n);
}catch(Exception e){
System.out.println("Something Wrong at "+e.getMessage());
}
}
/**
* Main method.
* @param args no arguments needed
* @throws IOException
*/
public static void main(String[] args) throws IOException {
new ExtractPageContent().parsePdf(inn);
}
}
你遇到了什麼樣的問題?它是否引發異常? – mamoo