lucene的代码示范
来源:互联网 发布:笑傲江湖决赛知乎 编辑:程序博客网 时间:2024/06/08 15:33
import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;import java.nio.file.FileSystems;import org.apache.lucene.analysis.Analyzer;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.IndexWriter;import org.apache.lucene.index.IndexWriterConfig;import org.apache.lucene.index.Term;import org.apache.lucene.queryparser.classic.ParseException;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.TermQuery;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 IndexFile { String indexFile = null; String sourceDir = null; public IndexFile(String sourceDir, String indexFile){ this.sourceDir = sourceDir; this.indexFile = indexFile; } public void index() throws Exception { // RAMDirectory ramdirectory = new RAMDirectory(); //创建内存索引对象 Directory directory = FSDirectory.open(FileSystems.getDefault().getPath(this.indexFile)); //创建磁盘目录对象 Analyzer analyzer = new StandardAnalyzer(); //创建分词器 IndexWriterConfig writerConfig = new IndexWriterConfig(analyzer); //创建索引写入对象控制 IndexWriter writer = new IndexWriter(directory, writerConfig); //创建写入对象 this.writeDocuments(writer); //将文档写入对象// directory.close(); } public void writeDocuments(IndexWriter writer) throws Exception{ File directory = new File(sourceDir); File[] fileList = directory.listFiles(); for(int i = 0 ; i < fileList.length; i++){ String title = fileList[i].getName(); String content= this.readFile(fileList[i]); System.out.println(content); System.out.println(title); Document doc = new Document(); doc.add(new Field("name", title, TextField.TYPE_STORED)); doc.add(new Field("address", this.indexFile, TextField.TYPE_STORED)); doc.add(new Field("thing", content, TextField.TYPE_STORED)); writer.addDocument(doc); System.out.println("add one document"); } writer.close(); } public String readFile(File file) throws Exception{ BufferedReader br = null; String content = null; try{ br = new BufferedReader(new FileReader(file)); String line ; while ( (line = br.readLine()) != null){ content += line; } } finally{ if(br != null ){ br.close(); } } return content; } public void query(String queryStr) throws IOException, ParseException{ Directory directory = FSDirectory.open(FileSystems.getDefault().getPath(this.indexFile)); DirectoryReader directoryReader = DirectoryReader.open(directory); IndexSearcher indexSearcher = new IndexSearcher(directoryReader); Analyzer analyzer = new StandardAnalyzer(); QueryParser qp = new QueryParser("thing", analyzer); qp.setDefaultOperator(QueryParser.AND_OPERATOR); Query query = qp.parse(queryStr);// Term term = new Term("name", queryStr);// Query query = new TermQuery(term); TopDocs topDocs = indexSearcher.search(query, 10); ScoreDoc[] scoreDocs = topDocs.scoreDocs; System.out.println( "检索到" + topDocs.totalHits + "条记录。"); for( ScoreDoc scoreDoc : scoreDocs){ Document document = indexSearcher.doc(scoreDoc.doc); System.out.println("title:" + document.getField("name").stringValue()); System.out.println("content:" + document.getField("thing").stringValue()); } directory.close(); } public static void main(String[] agrs) throws Exception{ String indexFileName = "/home/quincy1994/QA/LuceneIndex/data"; String sourceDir = "./MovieList"; IndexFile indexFile = new IndexFile(sourceDir, indexFileName);// indexFile.index(); System.out.println("nice!"); String queryStr = "大城小事讲的是什么"; indexFile.query(queryStr); }}
0 0
- lucene的代码示范
- 简单的数据库连接代码示范!!
- 实现壁纸更换的示范代码
- 一段ADSI和EXCHANGE结合的示范代码
- 在Hibernate中调用存储过程的示范代码
- Qt中的QFileDialog类的几个示范代码
- Qt中的QFileDialog类的几个示范代码
- 在Hibernate中调用存储过程的示范代码
- Select模型的 单服务器多客户端示范代码
- 关于单例(Singleton)的示范代码:allocwithzone
- EasyUI常见几种控件动态渲染的示范代码
- Qt中的QFileDialog类的几个示范代码
- 摸索lucene的简要代码
- lucene的一段测试代码
- uploadify批量上传 代码示范
- Python装饰器代码示范
- 的撒示范点
- lucene搜索引擎技术的分析与整理(lucene代码分析)
- 【J2SE】——面向对象
- 搭建JavaWeb开发环境使用IDEA开发工具
- WebSocket注入Bean后报null,操作不了数据库的解决方法
- java面试题(七)
- bzoj 1453 双面棋盘 LCT 并查集
- lucene的代码示范
- poj3268
- Adobe PhotoShop CS6 for Mac(苹果)中文破解版下载及破解方法
- 2016年12月24号网络文摘阅读笔记
- 汇编语言编译工具DUBUG使用与安装
- C#学习笔记1
- java面试题(八)
- 柯朗微积分与数学分析习题选解(1.1 节 a)
- js处理时间的那些事