首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

自个儿写的第一个lucene查询

2012-08-17 
自己写的第一个lucene查询import java.io.BufferedReaderimport java.io.Fileimport java.io.FileInputS

自己写的第一个lucene查询

import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStreamReader;import net.paoding.analysis.analyzer.PaodingAnalyzer;import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.document.Field.Index;import org.apache.lucene.document.Field.Store;import org.apache.lucene.document.Field.TermVector;import org.apache.lucene.index.CorruptIndexException;import org.apache.lucene.index.IndexWriter;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.TopScoreDocCollector;import org.apache.lucene.store.FSDirectory;import org.apache.lucene.util.Version;import org.junit.Test;/** lucene HelloWorld */@SuppressWarnings("deprecation")public class HelloWorld {private String dataPath = "D:\\data";private String filePath = "D:\\index";/** 创建索引 */@Testpublic void createIndex() throws CorruptIndexException, IOException {File dataFile = new File(dataPath);File file = new File(filePath);Analyzer analyzer = new PaodingAnalyzer();File[] files = dataFile.listFiles();IndexWriter indexWriter = null;try {indexWriter = new IndexWriter(FSDirectory.open(file), analyzer, true,IndexWriter.MaxFieldLength.UNLIMITED);}catch (Exception e) {// TODO: handle exception}for (File fi : files) {Document doc = new Document();doc.add(new Field("content",this.readfile(fi),Store.YES, Index.ANALYZED));doc.add(new Field("path", fi.getAbsolutePath(), Store.YES, Index.ANALYZED,TermVector.WITH_POSITIONS_OFFSETS));indexWriter.addDocument(doc);}indexWriter.optimize();indexWriter.close();}/** * 搜索 *  * @throws IOException * @throws CorruptIndexException * @throws ParseException */@Testpublic void doSearch() throws CorruptIndexException, IOException, ParseException {String queryStr = "英语  西班牙";File indexDir = new File(filePath);IndexSearcher indexSearch = new IndexSearcher(FSDirectory.open(indexDir), true);Query query = null;QueryParser qp = new QueryParser(Version.LUCENE_30, "content", new PaodingAnalyzer());//qp.setDefaultOperator(QueryParser.AND_OPERATOR);query = qp.parse(queryStr);TopScoreDocCollector colector=TopScoreDocCollector.create(100, true);indexSearch.search(query, colector);ScoreDoc[] docs = colector.topDocs().scoreDocs;System.out.println("总共:"+docs.length);for(int i=0;i<docs.length;i++){Document document=new Document();            document= indexSearch.doc(docs[i].doc);System.out.println("路径:"+document.get("path"));System.out.println("内容:"+document.get("content"));}//System.out.println(colector.getTotalHits());indexSearch.close();        }public String readfile(File file) throws IOException{String text="";String encoding="GBK";InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);BufferedReader input=new BufferedReader(read);StringBuffer buffer = new StringBuffer(); while ((text = input.readLine()) != null)               {buffer.append(text + "\n");               //System.out.println(text);              }text=buffer.toString();input.close();read.close();//System.out.println(text);return text;}}

热点排行