Lucene索引从文本文件中搜索

时间:2015-05-04 20:41:49

标签: java lucene

我试图从包含关键字的文本文件中搜索lucene索引,而不是在代码中包含关键字。在下面的代码中,关键字" book"在Lucene索引中搜索。我想读取一个包含我想要搜索的关键字的txt文件而不是这个方法你对我如何在下面的代码中实现这个概念有任何想法吗?

public class Searcher
{

    public void searchIndex(String instring) throws IOException, ParseException
    {
        System.out.println("Searching for ' " + instring +  " '");          
        IndexSearcher searcher = new IndexSearcher(FSDirectory.open(new File("C:\\indexing\\full")));
        Analyzer analyzer1 = new StandardAnalyzer(Version.LUCENE_34);
        QueryParser queryParser = new QueryParser(Version.LUCENE_34,"content", analyzer1);
        QueryParser queryParserfilename = new QueryParser(Version.LUCENE_34,"fullpath",analyzer1);
        Query query = queryParser.parse(instring);
        Query queryfilename = queryParserfilename.parse(instring);        
        TopDocs hits = searcher.search(query, 100);
        ScoreDoc[] document = hits.scoreDocs;

        System.out.println("Total no of hits for content: " + hits.totalHits);
        for(int i = 0;i <document.length;i++)
        {                 
            Document doc = searcher.doc(document[i].doc);      
            String filePath = doc.get("fullpath");                                      
            System.out.println(filePath);
        }

        TopDocs hitfilename = searcher.search(queryfilename,100);
        ScoreDoc[] documentfilename = hitfilename.scoreDocs;
        System.out.println("Occurrence:" + hitfilename.totalHits);       
        for(int i = 0;i < documentfilename.length ; i++)
        {
            Document doc = searcher.doc(documentfilename[i].doc);
            String filename= doc.get("filename");
            System.out.println(filename);                   
        } 
    }


    public static void main(String args[])
    {
        long startTime = System.currentTimeMillis();

        try {  
            new Searcher().searchIndex("book");
        } catch (IOException ex) {
            Logger.getLogger(Searcher.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            Logger.getLogger(Searcher.class.getName()).log(Level.SEVERE, null, ex);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("Time: " + (endTime - startTime));
    } 
}

0 个答案:

没有答案