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

📄 baseindexingtestcase.java

📁 一个用lucene做的文件索引的基类
💻 JAVA
字号:
import junit.framework.TestCase;

import org.apache.lucene.analysis.*;
import org.apache.lucene.analysis.standard.*;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
import org.apache.lucene.queryParser.*;
import org.apache.lucene.search.*;
import org.apache.lucene.search.function.*;
import org.apache.lucene.search.payloads.*;
import org.apache.lucene.search.spans.*;
import org.apache.lucene.store.*;
import org.apache.lucene.util.*;

import java.io.*;

public class BaseIndexingTestCase extends TestCase {
	
	protected String[] keywords = {"1","2"};
	protected String[] unIndexed = {"Netherlands","Italy"};
	protected String[] unStored = {"Amsterdam has lots of bridges",
		"Venice has lots of cannals"};
	protected String[] text = {"Amsterdam","Venice"};
	protected Directory dir;
	
	
	protected void setUp() throws IOException {
		
		String indexDir = System.getProperty("java.io.tmpdir","tmp") + 
		  System.getProperty("file.separator") + "index-dir";
		// deprecated: use 
		//dir = FSDirectory.getDirectory(indexDir,true);
		dir = FSDirectory.getDirectory(indexDir);
		addDocuments(dir);
	}
	
	
	protected void addDocuments(Directory dir) throws IOException {
		
		IndexWriter iWriter = new IndexWriter(dir,getAnalyzer(),true);
		iWriter.setUseCompoundFile(isCompound());
		
		for(int i=0; i<keywords.length; i++){
			Document doc = new Document();
			doc.add(new Field("id",keywords[i],Field.Store.YES,Field.Index.UN_TOKENIZED));
			doc.add(new Field("country",unIndexed[i],Field.Store.YES,Field.Index.NO));
			doc.add(new Field("contents",unStored[i],Field.Store.NO,Field.Index.TOKENIZED));
			doc.add(new Field("city",text[i],Field.Store.YES,Field.Index.TOKENIZED));
			iWriter.addDocument(doc);
		}
		
		iWriter.optimize();
		iWriter.close();
	}
	
	
	protected Analyzer getAnalyzer() {
		return new SimpleAnalyzer();
	}
	
	
	protected boolean isCompound() {
		return true;
	}
	
	
	
	public static void main(String[] args) {
		BaseIndexingTestCase BITC = new BaseIndexingTestCase();
		try {
			BITC.setUp();
		}
		catch(Exception e) {
			e.printStackTrace();
		}
	}
	
}

⌨️ 快捷键说明

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