Lucene的使用(java)

2019-08-01 10:05:16 浏览数 (1)

1 testLucene 入门程序

代码语言:javascript复制
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

代码语言:javascript复制
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();
		}
	
}

0 人点赞