⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 productindexer.java

📁 一个搜索引擎,希望对大家有用
💻 JAVA
字号:
package com.luceneheritrixbook.index;

import java.io.*;

import jeasy.analysis.MMAnalyzer;

import org.apache.lucene.analysis.*;
import org.apache.lucene.index.*;

import com.luceneheritrixbook.core.Product;
import com.luceneheritrixbook.searchengine.config.PropertyConfiguration;

public class ProductIndexer {

	private String indexPath = "";
	
	private IndexWriter writer = null;
	
	private Analyzer analyzer = null;
	
	private String dictionary_file = PropertyConfiguration.getWordDictionary();

	public ProductIndexer(String indexPath) throws Exception {
		this.indexPath = indexPath;
		initialize();
	}

	/**
	 * 
	 * @throws Exception
	 */
	private void initialize() throws Exception {
		analyzer = new MMAnalyzer();
//		FileReader reader = new FileReader(dictionary_file);
//		((MMAnalyzer)analyzer).addDictionary(reader);
		writer = new IndexWriter(indexPath, analyzer, true);
	}
	
	
	public void close() {
		try{
			writer.close();
		} catch(Exception e) {
			e.printStackTrace();
			writer = null;
		}
	}
	
	public void addProduct(Product product, int id) throws Exception {
		writer.addDocument(ProductDocument.buildProductDocument(product, id));
	}
	
	public void optimizeIndex() throws Exception {
		writer.optimize();
	}
	

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -