我們使用的百度搜索和電商網站的搜索功能一般都是基於Lucene實現的,Solr就是對Lucene進行的封裝,就像Servlet和Struts2,SpringMvc一樣
說的專業點就是全文檢索
實現全文檢索的流程的大致操作如下
這張圖表現的很清晰,網上扒下來的
索引庫中應該包含兩部分,一部分是索引,一部分是文檔,索引包含對應文檔的id,通過該id可以查找到文檔

來看看我的第一個Lucene入門程序
先整理步驟
/* * * 創建索引 * 查詢索引 * 1,創建indexwriter對象, * 指定索引庫存放位置Directory對象 * 指定一個分析器,對文檔內容進行分析 * 2,創建document對象 * 3,創建field對象,將field添加到document對象中 * 4,使用indexwriter對象把document對象寫入索引庫, * 此過程進行索引創建,並將document對象和索引都寫入索引庫 * 5,關閉indexwriter對象 */ public class FirstLucene { //創建索引和查詢索引要使用同一份分析器(分詞器) @Test public void createIndex() throws Exception { //指定索引庫存放位置Directory對象 Directory directory = FSDirectory.open(new File("D://lucene//index")); Analyzer analyzer = new IKAnalyzer();//中文分詞器 IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_10_3, analyzer);//指定分析器 IndexWriter indexWriter = new IndexWriter(directory, config);//創建IndexWriter File f = new File("D://lucene//searchsource");//源文件的的路徑,對這些文件進行分析,添加索引 File[] listFiles = f.listFiles(); for (File file : listFiles) { Document document = new Document(); //文件名 String name = file.getName(); //TextField存儲字符串或流 分析Yes 索引Yes 保存Yes Field fileNameField = new TextField("fielName", name, Store.YES); //文件大小 long file_size = FileUtils.sizeOf(file); //LongField存儲long型 分析Yes 索引Yes 保存Yes or No Field fileSizeField = new LongField("fileSize",file_size,Store.YES); //StringField存儲字符串 分析 N 索引Y 保存Yes or No //文件路徑 String file_path = file.getPath(); //StoreFieldc可以存儲多種類型 分析No 索引No 保存Yes Field filePathField = new StoredField("filePath", file_path); //文件內容 String file_content = FileUtils.readFileToString(file); Field fileContentField = new TextField("file_content", file_content, Store.YES); //把域添加到文檔中 document.add(fileNameField); document.add(fileSizeField); document.add(filePathField); document.add(fileContentField); //使用indexwriter對象添加文檔 ,此過程進行索引創建,並將document對象和索引都寫入索引庫 indexWriter.addDocument(document); } indexWriter.close(); } }
看到這里相信你也已經或創建索引了,加油
public class FirstLucene { //創建索引和查詢索引要使用同一份分析器(分詞器) @Test public void createIndex() throws Exception { Directory directory = FSDirectory.open(new File("D://lucene//index"));//指定索引庫存放位置Directory對象 Analyzer analyzer = new IKAnalyzer();//官方推薦使用這個實現類 IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_10_3, analyzer);//指定分析器 IndexWriter indexWriter = new IndexWriter(directory, config);//創建IndexWriter File f = new File("D://lucene//searchsource");//源文件的的路徑 File[] listFiles = f.listFiles(); for (File file : listFiles) { Document document = new Document(); //文件名 String name = file.getName(); //TextField存儲字符串或流 分析Yes 索引Yes 保存Yes Field fileNameField = new TextField("fielName", name, Store.YES); //文件大小 long file_size = FileUtils.sizeOf(file); //LongField存儲long型 分析Yes 索引Yes 保存Yes or No Field fileSizeField = new LongField("fileSize",file_size,Store.YES); //StringField存儲字符串 分析 N 索引Y 保存Yes or No //文件路徑 String file_path = file.getPath(); //StoreFieldc可以存儲多種類型 分析No 索引No 保存Yes Field filePathField = new StoredField("filePath", file_path); //文件內容 String file_content = FileUtils.readFileToString(file); Field fileContentField = new TextField("file_content", file_content, Store.YES); //把域添加到文檔中 document.add(fileNameField); document.add(fileSizeField); document.add(filePathField); document.add(fileContentField); //使用indexwriter對象添加文檔 ,此過程進行索引創建,並將document對象和索引都寫入索引庫 indexWriter.addDocument(document); } indexWriter.close(); } }
