Lucene3.5 之 索引和搜索简单示例
2012-03-02 13:59
525 查看
import java.io.File; import java.io.FileReader; 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.CorruptIndexException; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.queryParser.ParseException; import org.apache.lucene.queryParser.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.store.LockObtainFailedException; import org.apache.lucene.util.Version; public class HelloLucene { public void index() { //1.创建Directroy //创建在内存中 // Directory directory = new RAMDirectory(); //2.创建IndexWriter IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_35,new StandardAnalyzer(Version.LUCENE_35)); IndexWriter writer = null; try { //创建在硬盘上 Directory directory = FSDirectory.open(new File("C:\\Users\\ETHAN\\workspace\\hellolucene\\index01")); writer = new IndexWriter(directory, iwc); //3.创建Document对象 Document doc = null; //4.为Document添加Field File f = new File("C:\\Users\\ETHAN\\workspace\\hellolucene\\example"); for(File file:f.listFiles()) { doc = new Document(); doc.add(new Field("content",new FileReader(file))); doc.add(new Field("filename",file.getName(),Field.Store.YES,Field.Index.NOT_ANALYZED)); doc.add(new Field("path",file.getAbsolutePath(),Field.Store.YES,Field.Index.NOT_ANALYZED)); //5.通过IndexWriter添加文档到索引中 writer.addDocument(doc); } } catch (CorruptIndexException e) { e.printStackTrace(); } catch (LockObtainFailedException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(writer!=null) { try { writer.close(); } catch (CorruptIndexException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } } public void searcher() { //1.创建Directroy //创建在硬盘上 IndexReader reader = null; try { Directory directory = FSDirectory.open(new File("C:\\Users\\ETHAN\\workspace\\hellolucene\\index01")); //2.创建IndexReader reader = IndexReader.open(directory); //3.根据IndexReader创建IndexSearcher IndexSearcher searcher = new IndexSearcher(reader); //4.创建搜索的Query //创建parser来确定要搜索文件的内容,第二个参数表示要搜索的域 QueryParser parser = new QueryParser(Version.LUCENE_35,"content",new StandardAnalyzer(Version.LUCENE_35)); //创建query,表示搜索域为content中包含java的文档 Query query = parser.parse("java"); //5.根据searcher搜索并返回TopDocs,这里设置返回十条 TopDocs tds = searcher.search(query, 10); //6.根据TopDocs获取ScoreDoc对象 ScoreDoc[] sds = tds.scoreDocs; for(ScoreDoc sd:sds) { //7.根据searcher和ScoredDoc对象获取具体的Document对象 Document d = searcher.doc(sd.doc); //8.根据Document对象获取需要的值 System.out.println(d.get("filename")+"["+d.get("path")+"]"); } } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } finally { //关闭reader try { if(reader!=null) reader.close(); } catch (IOException e) { e.printStackTrace(); } } } }
import org.junit.*; public class LuceneTest { @Test public void testIndex() { HelloLucene h1 = new HelloLucene(); h1.index(); } @Test public void testSearch() { HelloLucene h1 = new HelloLucene(); h1.searcher(); } }
搜索显示结果:test1.txt[C:\Users\ETHAN\workspace\hellolucene\example\test1.txt]
相关文章推荐
- Lucene中最简单的索引和搜索示例
- 11、学习Lucene3.5索引搜索之分页显示
- lucene3.5学习笔记02--创建索引和建立搜索
- 9、学习Lucene3.5索引搜索之其他搜索
- 19、学习Lucene3.5索引之高亮搜索
- 全文检索Lucene入门之创建索引及简单搜索
- Lucene数据索引搜索示例
- 10、学习Lucene3.5索引搜索之QueryParser
- Lucene索引和搜索核心类的简单介绍
- 使用lucene3.6创建索引和实现简单搜索
- Lucene2.9.2 + 盘古分词2.3.1(一) 入门: 建立简单索引,搜索(原创)
- 使用lucene3.6创建索引和实现简单搜索
- 17、学习Lucene3.5索引之使用Filter(过滤器)搜索
- lucene创建索引简单示例
- 一步一步跟我学习lucene(8)---lucene搜索之索引的查询原理和查询工具类(支持分页)示例
- 8、学习Lucene3.5索引搜索之TermRange
- Lucene.NET使用入门(二)【简单的搜索网站示例】
- 使用Lucene开发简单的站内新闻搜索引擎(索引的搜索)
- c#使用Lucene.net创建索引,实现搜索的代码示例
- C#使用Lucene建索引和搜索代码示例