lucene学习01之简单小例子初步认识lucene

来源:转载


        lucene是干嘛用的,这里不多说。这些写个简单例子,简单对lucene的使用有个初步了解。


LuceneFileHelper.java

package com.fei;import java.io.File;import java.io.FileReader;import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.document.TextField;import org.apache.lucene.index.DirectoryReader;import org.apache.lucene.index.IndexReader;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.IndexWriterConfig;import org.apache.lucene.index.IndexWriterConfig.OpenMode;import org.apache.lucene.queryparser.classic.QueryParser;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.Query;import org.apache.lucene.search.ScoreDoc;import org.apache.lucene.search.TopDocs;import org.apache.lucene.store.Directory;import org.apache.lucene.store.FSDirectory;import org.apache.lucene.util.Version;public class LuceneFileHelper { private final static String FIELD_CONTENT = "content"; private final static String FIELD_FILEPATH = "filePath"; private final static String FIELD_FILENAME = "fileName"; /** * 创建索引 * @param sourceFilePath * @param indexFileDir */ public static void createIndex(File sourceFile,String indexFileDir){ Directory directory = null; IndexWriter indexWriter = null; try { //1.创建Directory directory = FSDirectory.open(new File(indexFileDir)); //2.创建IndexWriter IndexWriterConfig indexWriterConf = new IndexWriterConfig(Version.LUCENE_4_9,new StandardAnalyzer(Version.LUCENE_4_9)); indexWriter = new IndexWriter(directory,indexWriterConf); //3.创建Document Document document = new Document(); document.add(new TextField(FIELD_CONTENT,new FileReader(sourceFile))); document.add(new TextField(FIELD_FILENAME,sourceFile.getName(),Field.Store.YES)); document.add(new TextField(FIELD_FILEPATH,sourceFile.getAbsolutePath(),Field.Store.YES)); //4.把Document写到索引中 indexWriter.addDocument(document); } catch (Exception e) { e.printStackTrace(); }finally{ if(indexWriter != null){ try { indexWriter.close(); } catch (IOException e) { indexWriter = null; } } } } /** * 在指定的索引目录下搜索指定的内容,并返回指定内容所在的文件名 * @param searcherContent * @param indexDir * @return */ public static List<String> searcher(String searcherContent, String indexDir){ List<String> filePaths = new ArrayList<String>(); try { //1.创建Directory Directory directory = FSDirectory.open(new File(indexDir)); //2.创建IndexReader IndexReader indexReader = DirectoryReader.open(directory); //3.根据IndexReader创建IndexSearcher IndexSearcher indexSearcher = new IndexSearcher(indexReader); //4.创建搜索的Query //先创建QueryParser,确定要搜索文档中的field QueryParser parser = new QueryParser(Version.LUCENE_4_9,FIELD_CONTENT,new StandardAnalyzer(Version.LUCENE_4_9)); //指定要搜索的内容 Query query = parser.parse(searcherContent); //5.根据IndexSearcher搜索并返回TopDocs TopDocs topDocs = indexSearcher.search(query, 10);//最多返回10个 //6.根据TopDocs获取ScoreDoc[]对象 ScoreDoc[] scoreDocs = topDocs.scoreDocs; for(ScoreDoc sd : scoreDocs){ Document document = indexSearcher.doc(sd.doc); filePaths.add(document.get(FIELD_FILEPATH)); } indexReader.close(); } catch (Exception e) { e.printStackTrace(); } return filePaths; }}

  这里使用的 lucene的版本是4.9的,不同的版本,创建某些对象时,方法有可能有一些差别,具体的用法,可以下载对应版本的源码来看看里面自带的demo。

LuceneFileHelperTest.java

package lucene001;import java.io.File;import java.util.List;import org.junit.Test;import com.fei.LuceneFileHelper;public class LuceneFileHelperTest { @Test public void indexTest(){ String sourceFileDir = "E://lucene//source"; String indexFileDir = "E://lucene//index"; for(File file : new File(sourceFileDir).listFiles()){ LuceneFileHelper.createIndex(file, indexFileDir); } } @Test public void searcherTest(){ String indexFileDir = "E://lucene//index"; List<String> filePaths = LuceneFileHelper.searcher("lucene", indexFileDir); System.out.println("========含有/"lucene/"的文件==========="); System.out.println(filePaths.toString()); filePaths = LuceneFileHelper.searcher("hello", indexFileDir); System.out.println("========含有/"hello/"的文件==========="); System.out.println(filePaths.toString()); }}
   建立文件夹e://lucene//source 及e://lucene//index,前者放一些测试文件,后者放lucene根据测试文件生成的索引文件,在e://lucene//source下创建a.txt,内容写上:hello java,b.txt写上:hello lucene.

   写运行测试类LuceneFileHelperTest.java中的indexTest()方法,运行后,看到e://lucene//index下生成了一些文件.



    运行测试类LuceneFileHelperTest.java中的searcherTest()方法,控制台中,会看到:








分享给朋友:
您可能感兴趣的文章:
随机阅读: