Lucene的使用(java)

1

1 testLucene 入門程序

package com.shi.lucene;

import java.io.File;

import org.apache.commons.io.FileUtils;
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.Field.Store;
import org.apache.lucene.document.LongField;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;


/**
 * 
 * @author: SHF
 * @date: 2018年3月6日 下午4:37:44
 * @Description: 第一個入門級lucune程序
 * 建立索引
 */
public class LuceneFirst {

    //建立索引
	@Test
	public void testLucene()throws Exception{
		// 1 建立java工程 ,並導入jar包
		//2 建立一個indexWriter對象.
			//  2.1 指定索引庫的存放位置 Directory對象
		 	//  2.2 指定一個分析器,對內容進行分析
		Directory directory=FSDirectory.open(new File("E:\\Eclipse\\javaProject\\31Lucene\\temp"));
		Analyzer analyzer=new StandardAnalyzer();//官方推薦的分析器
		IndexWriterConfig config=new IndexWriterConfig(Version.LATEST, analyzer);
		IndexWriter indexWriter=new IndexWriter(directory, config);
		  
		 //3 建立document對象
		 // 4 建立field對象,將field添加到document對象中
		 File f=new File("E:\\Eclipse\\javaProject\\31Lucene\\text");
		 File[] files=f.listFiles();
		 for(File file:files){
			 Document document=new Document();
			 //文件名稱
			 String file_name=file.getName();
			 Field fileNameField=new TextField("fileName", file_name, Store.YES);
			 //文件大小
			 Long file_size=FileUtils.sizeOf(file);
			 Field fileSizeField=new LongField("fileSize", file_size, Store.YES);
			 //文件路徑
			 String file_path=file.getPath();
			 Field filePathField=new StoredField("filePath", file_path);
			 //文件內容
			 String file_content=FileUtils.readFileToString(file);
			 Field fileContentField=new TextField("fileContent", file_content, Store.YES);
		 
			 document.add(fileNameField);
			 document.add(fileSizeField);
			 document.add(filePathField);
			 document.add(fileContentField);
			 
			//5 使用indexWriter對象講document對象寫入索引庫,此過程進行索引建立,並將索引和document對象寫入索引庫
			 indexWriter.addDocument(document);
		 }
		 // 6 關閉indexWriter對象
		 indexWriter.close();
	}


        //搜索索引
	@Test
	public void searchLuceneTest()throws Exception{
		//1 建立一個Directory對象,也就是存放索引的位置
		Directory directory= FSDirectory.open(new File("E:\\Eclipse\\javaProject\\31Lucene\\temp"));
		//2 建立一個indexReader對象,須要指定Directory對象
		IndexReader indexReader=DirectoryReader.open(directory);
		//3 建立一個indexsearcher對象,須要指定IndexReader對象
		IndexSearcher indexSearcher=new IndexSearcher(indexReader);
		//4 建立一個TermQuery對象,指定查詢的域和關鍵字。
		Query query=new TermQuery(new Term("fileName","mysql"));
		//5 執行查詢
		TopDocs topDocs= indexSearcher.search(query, 10);
		//6 返回查詢結果,遍歷查詢結果並輸出
		ScoreDoc[] scoreDocs=topDocs.scoreDocs;
		for(ScoreDoc scoreDoc:scoreDocs){
			int doc=scoreDoc.doc;
			Document document=indexSearcher.doc(doc);
			String fileName=document.get("fileName");
			System.out.println(fileName);
			String fileContent=document.get("fileContent");
			System.out.println(fileContent);
			String fileSize=document.get("fileSize");
			System.out.println(fileSize);
			String filePath=document.get("filePath");
			System.out.println(filePath);
			
			System.out.println("--------------------------");
		}
		//7 關閉IndexReader對象
		indexReader.close();
	}
}

2 LuceneManager

package com.shi.lucene;

import java.io.File;
import java.io.IOException;

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.Store;
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.index.Term;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.queryparser.xml.builders.NumericRangeQueryBuilder;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.NumericRangeQuery;
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;
import org.junit.Test;
import org.wltea.analyzer.lucene.IKAnalyzer;

/**
 * 
 * @author: SHF
 * @date: 2018年3月7日 上午10:49:20
 * @Description:索引庫的管理
 * 增長  入門程序
 * 查詢  入門程序
 * 修改  
 * 刪除
 */
public class LuceneManager {
	
	//*************獲取IndexWriter對象*************
	public IndexWriter getIndexWriter() throws IOException{
		//2 建立一個indexWriter對象.
			//  2.1 指定索引庫的存放位置 Directory對象
		 	//  2.2 指定一個分析器,對內容進行分析
		Directory directory=FSDirectory.open(new File("E:\\Eclipse\\javaProject\\31Lucene\\temp"));
//		Analyzer analyzer=new StandardAnalyzer();//官方推薦的分析器
		Analyzer analyzer=new IKAnalyzer();//使用IK中文分詞器
		IndexWriterConfig config=new IndexWriterConfig(Version.LATEST, analyzer);
		IndexWriter indexWriter=new IndexWriter(directory, config);
		return indexWriter;
	}
	
	//*************刪除全部*************
	@Test
	public void testDeleteAll() throws IOException{
		IndexWriter indexWriter=getIndexWriter();
		indexWriter.deleteAll();
		indexWriter.close();
	}
	
	//*************根據條件刪除*************
	@Test
	public void testDeleteQuery() throws IOException{
		IndexWriter indexWriter=getIndexWriter();
		Query query=new TermQuery(new Term("fileName","mysql"));
		indexWriter.deleteDocuments(query);;
		indexWriter.close();
	}
	
	//*************修改*************
	@Test
	public void testUpdate()throws Exception{
		IndexWriter indexWriter=getIndexWriter();
		Document doc=new Document();
		doc.add(new TextField("fileName","fileName修改測試",Store.YES));
		doc.add(new TextField("fileContent","fileContent修改測試",Store.YES));
		indexWriter.updateDocument(new Term("fileName","mysql"), doc,new IKAnalyzer());
		indexWriter.close();
	}
	
	//*************查詢全部  根據範圍查詢*************
	@Test
	public void testMarchAllDocsQuery()throws Exception{
		//1 建立一個Directory對象,也就是存放索引的位置
		Directory directory= FSDirectory.open(new File("E:\\Eclipse\\javaProject\\31Lucene\\temp"));
		//2 建立一個indexReader對象,須要指定Directory對象
		IndexReader indexReader=DirectoryReader.open(directory);
		//3 建立一個indexsearcher對象,須要指定IndexReader對象
		IndexSearcher indexSearcher=new IndexSearcher(indexReader);
		
		//4 建立一個TermQuery對象,指定查詢的域和關鍵字。
//		Query query=new MatchAllDocsQuery();//查詢全部
//		Query query=NumericRangeQuery.newLongRange("fileSize", 0l, 100l, true, true);//根據範圍查詢
//		Query query=NumericRangeQuery.newDoubleRange("fileSize", 0.0, 15075.0, true, true);
		
		/**
		 * *************組合查詢*************
		 */
		BooleanQuery query=new BooleanQuery();
		Query query1=new TermQuery(new Term("fileName","mysql"));
		Query query2=new TermQuery(new Term("fileName","查詢"));
		query.add(query1,Occur.MUST);
		query.add(query2,Occur.MUST);
		System.out.println(query);
		//5 執行查詢
		TopDocs topDocs= indexSearcher.search(query, 10);
		//6 返回查詢結果,遍歷查詢結果並輸出
		ScoreDoc[] scoreDocs=topDocs.scoreDocs;
		for(ScoreDoc scoreDoc:scoreDocs){
			int doc=scoreDoc.doc;
			Document document=indexSearcher.doc(doc);
			String fileName=document.get("fileName");
			System.out.println(fileName);
			String fileContent=document.get("fileContent");
			System.out.println(fileContent);
			String fileSize=document.get("fileSize");
			System.out.println(fileSize);
			String filePath=document.get("filePath");
			System.out.println(filePath);
			System.out.println("--------------------------");
		}
		//7 關閉IndexReader對象
		indexReader.close();
	}
	
	
	
		//*************條件解釋的對象查詢*************
		@Test
		public void testQueryParser()throws Exception{
			//1 建立一個Directory對象,也就是存放索引的位置
			Directory directory= FSDirectory.open(new File("E:\\Eclipse\\javaProject\\31Lucene\\temp"));
			//2 建立一個indexReader對象,須要指定Directory對象
			IndexReader indexReader=DirectoryReader.open(directory);
			//3 建立一個indexsearcher對象,須要指定IndexReader對象
			IndexSearcher indexSearcher=new IndexSearcher(indexReader);
			
			//4 建立一個TermQuery對象,指定查詢的域和關鍵字。
			/**
			 * 條件解釋
			 */
			QueryParser queryParser=new QueryParser("fileName",new IKAnalyzer());
			//*:*   域:值
			Query query=queryParser.parse("+fileName:mysql +fileName:查詢");
			System.out.println(query);
			//5 執行查詢
			TopDocs topDocs= indexSearcher.search(query, 10);
			//6 返回查詢結果,遍歷查詢結果並輸出
			ScoreDoc[] scoreDocs=topDocs.scoreDocs;
			for(ScoreDoc scoreDoc:scoreDocs){
				int doc=scoreDoc.doc;
				Document document=indexSearcher.doc(doc);
				String fileName=document.get("fileName");
				System.out.println(fileName);
				String fileContent=document.get("fileContent");
				System.out.println(fileContent);
				String fileSize=document.get("fileSize");
				System.out.println(fileSize);
				String filePath=document.get("filePath");
				System.out.println(filePath);
				System.out.println("--------------------------");
			}
			//7 關閉IndexReader對象
			indexReader.close();
		}
	
}

1

2

6666

相關文章
相關標籤/搜索