標籤:getname div top mdi reader analyzer amp [] trace
package cn.dyg.luence;import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;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.util.Version;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.io.File;import java.io.FileReader;import java.io.IOException;/** * Created by Administrator on 2016/11/2. */public class IndexUtil { private static final Logger logger = LoggerFactory.getLogger(IndexUtil.class); private static final String indexPath = "E:\\javaweb\\luenceRepository\\index"; private IndexUtil() { } public static IndexReader getIndexReader() { logger.info("this is the indexReader ..."); return null; } public static void setIndexWriter() { //1 建立Directory// Directory directory = new RAMDirectory(); //索引建立在記憶體中 Directory directory = null; //索引建立在磁碟中 try { directory = FSDirectory.open(new File(indexPath)); } catch (IOException e) { e.printStackTrace(); } //2 建立IndexWriter IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)); IndexWriter indexWriter = null; try { indexWriter = new IndexWriter(directory, indexWriterConfig); //3 建立文檔對象 Document document = null; //4 為文檔添加filed File fileList = new File("E:\\javaweb\\luenceRepository\\example"); for (File file : fileList.listFiles()) { document = new Document(); document.add(new Field("content", new FileReader(file))); document.add(new Field("name", file.getName(), Field.Store.YES, Field.Index.NOT_ANALYZED)); document.add(new Field("path", file.getAbsolutePath(), Field.Store.YES, Field.Index.NOT_ANALYZED)); //5 通過IndexWriter 添加文檔到索引中 indexWriter.addDocument(document); } } catch (IOException e) { e.printStackTrace(); } finally { if (indexWriter != null) { try { indexWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static void search() { //1 建立Directory// Directory directory = new RAMDirectory(); //索引建立在記憶體中 Directory directory = null; //索引建立在磁碟中 try { directory = FSDirectory.open(new File(indexPath)); } catch (IOException e) { e.printStackTrace(); } //2 建立IndexReader IndexReader indexReader = null; try { indexReader = IndexReader.open(directory); //3 建立search IndexSearcher indexSearcher = new IndexSearcher(indexReader); //4 建立搜尋的Query QueryParser queryParser = new QueryParser( Version.LUCENE_35, "content", new StandardAnalyzer(Version.LUCENE_35) ); //5 建立Query, 表示搜尋域為content中包含java的文檔 Query query = queryParser.parse("3350527960"); //6 根據searcher搜尋並返回TopDocs int queryLimit = 10; TopDocs topDocs = indexSearcher.search(query, queryLimit); //7 根據topDocs獲得scoreDocs ScoreDoc[] scoreDocs = topDocs.scoreDocs; int i = 0; for (ScoreDoc scoreDoc : scoreDocs) { //8 根據searcher 和ScoreDoc擷取document對象 Document document = indexSearcher.doc(scoreDoc.doc); logger.info(i + document.get("path") + document.get("name")); } } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } finally { if (indexReader != null) { try { indexReader.close(); } catch (IOException e) { e.printStackTrace(); } } } }}
建立索引和搜尋