lucene 创建索引 查询实例
2015-05-25 18:01
363 查看
package com.jr.demo; import java.io.File; import java.io.IOException; import java.net.URI; import java.nio.file.Path; import java.nio.file.Paths; 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.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 com.jr.util.Util; import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List; public class Demo1 { public static void main(String[] args) throws Exception { //index("d://index"); QueryContent("搜"); } /** * 创建索引 * @param strindex */ public static void index(String strindex){ Path ps=Paths.get("d://"); File f=ps.toFile(); File []files=f.listFiles(); try { /* Path p=Paths.get(strindex); Directory directory=FSDirectory.open(p);*/ Util.OpenDirectory("d://index"); IndexWriterConfig indexWriterConfig=new IndexWriterConfig(new StandardAnalyzer()); IndexWriter indexWriter=new IndexWriter(Util.directory, indexWriterConfig); for (File file:files) { Document document=new Document(); String filename=file.getName(); String prefixpathname=file.getCanonicalPath(); String path=file.getPath(); document.add(new TextField("filename", filename, Field.Store.YES)); document.add(new TextField("prefixpathname", prefixpathname, Field.Store.YES)); document.add(new TextField("path", path, Field.Store.YES)); indexWriter.addDocument(document); // System.out.println(filename); } indexWriter.commit(); indexWriter.close(); } catch (Exception e) { e.printStackTrace(); } } public static List QueryContent(String s){ Util.OpenDirectory("d://index"); try { IndexReader indexReader=DirectoryReader.open(Util.directory); IndexSearcher searcher=new IndexSearcher(indexReader); QueryParser parser=new QueryParser("filename", new StandardAnalyzer()); Query query=parser.parse(s); TopDocs docs=searcher.search(query, 10000); ScoreDoc[] docs2=docs.scoreDocs; for (ScoreDoc doc:docs2) { Document document=searcher.doc(doc.doc); System.out.println("filename:"+document.get("filename")); System.out.println("prefixpathname:"+document.get("prefixpathname")); System.out.println("path:"+document.get("path")); } indexReader.close(); } catch (Exception e) { e.printStackTrace(); } return null; } }
工具类:
package com.jr.util; import java.io.IOException; import java.nio.file.Paths; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; /** * 创建Directory工具类 * * */ public class Util { public static Directory directory; public static boolean OpenDirectory(String path){ try { directory=FSDirectory.open(Paths.get(path)); return true; } catch (IOException e) { e.printStackTrace(); return false; } } }
相关文章推荐
- lucene4.3简单创建和查询索引实例
- lucene5学习 - 索引基本操作(创建,查询,更新,删除,分页)
- Lucene实现索引和查询的实例讲解
- java鬼混笔记:lucene 1、简单的创建索引和查询
- lucene 索引创建查询
- Lucene学习-创建索引、关键词查询
- lucene索引创建与查询入门例子
- lucene4.8.0 + IKAnalyzer5.0.1 创建索引与查询demo
- Lucene学习一:入门级Demo,创建索引和查询高亮显示
- 使用org.apache.lucene创建和查询索引核心代码详解
- 使用org.apache.lucene创建和查询索引核心代码详解
- Lucene创建、查询、删除、更新 索引
- Lucene学习笔记(1)-索引创建和简单的查询
- Lucene索引创建、查询与高亮
- Lucene学习之一:使用lucene为数据库表创建索引,并按关键字查询
- lucene创建索引,分组查询
- solr入门之lucene创建索引和查询索引及查询的源码读取类确定
- Lucene的入门例子 - 创建索引,利用索引查询
- Lucene多线程创建索引及多目录下查询索引
- lucene-5.1.0 索引的创建与查询 demo