首页>代码>apache lucene 4.10.0入门单元测试代码demo>/lucene_4_10_0_demo/src/main/java/com/zuidaima/lucene/test/Main.java
package com.zuidaima.lucene.test;

import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.UUID;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.demo.IndexFiles;
import org.apache.lucene.demo.SearchFiles;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.IntField;
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.IndexWriterConfig.OpenMode;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
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.databene.contiperf.PerfTest;
import org.databene.contiperf.Required;
import org.databene.contiperf.junit.ContiPerfRule;
import org.databene.contiperf.timer.RandomTimer;
import org.junit.Rule;
import org.junit.Test;
import org.wltea.analyzer.lucene.IKAnalyzer;

public class Main {

	static String indexPath = "c:/index/";
	static String str1 = "司南1 test tes1百1";
	static String str2 = "司南2 test tes2百2";
	static String str3 = "司南3 tst tes3百3";
	static String str4 = "司南4 test tes4百4";
	static String str5 = "司南5 tes tes5百5";
	static String line = "test百度";
	static int hitsPerPage = 1;
	static String fieldContent = "content";
	// static Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_4_10_0);
	static Analyzer analyzer = new IKAnalyzer(false);

	@Test
	public void index() throws Exception {
		Directory dir = FSDirectory.open(new File(indexPath));
		IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_4_10_0,
				analyzer);
		iwc.setOpenMode(OpenMode.CREATE);
		// Optional: for better indexing performance, if you
		// are indexing many documents, increase the RAM
		// buffer. But if you do this, increase the max heap
		// size to the JVM (eg add -Xmx512m or -Xmx1g):
		//
		// iwc.setRAMBufferSizeMB(256.0);
		IndexWriter writer = new IndexWriter(dir, iwc);
		Document doc = null;
		Field field = null;

		doc = new Document();
		field = new TextField(fieldContent, str1, Field.Store.YES);
		doc.add(field);
		Field typeField = new IntField("type", 1, Field.Store.YES);
		doc.add(typeField);
		writer.addDocument(doc);

		doc = new Document();
		field = new TextField(fieldContent, str2, Field.Store.YES);
		doc.add(field);
		typeField = new IntField("type", 1, Field.Store.YES);
		doc.add(typeField);
		writer.addDocument(doc);

		doc = new Document();
		field = new TextField(fieldContent, str3, Field.Store.YES);
		doc.add(field);
		typeField = new IntField("type", 1, Field.Store.YES);
		doc.add(typeField);
		writer.addDocument(doc);

		doc = new Document();
		field = new TextField(fieldContent, str4, Field.Store.YES);
		doc.add(field);
		typeField = new IntField("type", 1, Field.Store.YES);
		doc.add(typeField);
		writer.addDocument(doc);

		doc = new Document();
		field = new TextField(fieldContent, str5, Field.Store.YES);
		doc.add(field);
		typeField = new IntField("type", 0, Field.Store.YES);
		doc.add(typeField);
		writer.addDocument(doc);

		writer.close();
	}

	@Test
	public void searchByKeys() throws Exception {
		index();
		IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(
				indexPath)));
		IndexSearcher searcher = new IndexSearcher(reader);
		String[] keys = new String[] { "test", "0" };
		String[] fields = new String[] { "title", "type" };
		Query query = MultiFieldQueryParser.parse(keys, fields, analyzer);
		TopDocs results = searcher.search(query, 5 * hitsPerPage);
		ScoreDoc[] hits = results.scoreDocs;
		int numTotalHits = results.totalHits;
		int start = 0;
		int end = Math.min(numTotalHits, hitsPerPage);
		for (int i = start; i < end; i++) {
			Document doc = searcher.doc(hits[i].doc);
			String content = doc.get("content");
			System.out.println(content);
		}
		reader.close();
	}

	@Test
	public void search1() throws Exception {
		index();
		IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(
				indexPath)));
		IndexSearcher searcher = new IndexSearcher(reader);
		QueryParser parser = new QueryParser(fieldContent, analyzer);
		Query query = parser.parse(line);
		TopDocs results = searcher.search(query, 5 * hitsPerPage);
		ScoreDoc[] hits = results.scoreDocs;
		int numTotalHits = results.totalHits;
		for (int i = 0; i < hits.length; i++) {
			Document doc = searcher.doc(hits[i].doc);
			String content = doc.get("content");
			System.out.println(content);
		}
		reader.close();
	}

	@Test
	public void indexFromFile() throws Exception {
		String[] args = { "-index", "c:/lucene/index/", "-docs",
				"c:/lucene/docs/" };
		IndexFiles.main(args);
	}

	@Test
	public void searchFromFile() throws Exception {
		String[] args = { "-index", "c:/lucene/index/" };
		SearchFiles.main(args);
	}

	@Test
	public void test2() throws Exception {
		String indexPath = "E:\\Lucene\\index"; // 索引保存目录
		Directory dir = FSDirectory.open(new File(indexPath));
		IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_4_10_0,
				analyzer);
		IndexWriter indexWriter = new IndexWriter(dir, iwc);
		indexWriter.deleteAll();
		indexWriter.commit();

		Document doc = new Document();
		Field fieldType = new TextField("type", "0", Field.Store.YES);
		doc.add(fieldType);
		Field fieldContents = new TextField("contents", "今晚的辩题很道地:在我们这些人当中?",
				Field.Store.YES);
		doc.add(fieldContents);
		Field fieldId = new TextField("id", "1", Field.Store.YES);
		doc.add(fieldId);
		indexWriter.addDocument(doc);
		System.out.println(doc);

		doc = new Document();
		fieldType = new TextField("type", "0", Field.Store.YES);
		doc.add(fieldType);
		fieldContents = new TextField("contents", "我们为电影《今朝》是一部不错的影片。",
				Field.Store.YES);
		doc.add(fieldContents);
		fieldId = new IntField("id", 2, Field.Store.YES);
		doc.add(fieldId);
		indexWriter.addDocument(doc);
		System.out.println(doc);

		doc = new Document();
		fieldType = new TextField("type", "0", Field.Store.YES);
		doc.add(fieldType);
		fieldContents = new TextField("contents", "我们到底是啥意思呢?", Field.Store.YES);
		doc.add(fieldContents);
		fieldId = new IntField("id", 3, Field.Store.YES);
		doc.add(fieldId);
		indexWriter.addDocument(doc);
		System.out.println(doc);
		indexWriter.close();

		System.out.println("------------------------------------------------");
		IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(
				indexPath)));
		IndexSearcher searcher = new IndexSearcher(reader);
		// 要查找的字符串数组
		String[] stringQuery = { "影片意思", "0" };
		// 待查找字符串对应的字段
		String[] fields = { "contents", "type" };
		Occur[] occ = { Occur.MUST, Occur.MUST };

		Query query = MultiFieldQueryParser.parse(stringQuery, fields, occ,
				new StandardAnalyzer());
		TopDocs results = searcher.search(query, 5 * hitsPerPage);
		ScoreDoc[] hits = results.scoreDocs;
		for (int i = 0; i < hits.length; i++) {
			Document _doc = searcher.doc(hits[i].doc);
			System.out.println("Document内容为 : " + _doc);
		}
		System.out.println("共检索出符合条件的Document " + hits.length + " 个。");
	}

	@Test
	public void test3() throws Exception {
		String indexPath = "E:\\Lucene\\index"; // 索引保存目录
		Directory dir = FSDirectory.open(new File(indexPath));
		IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_4_10_0,
				analyzer);
		IndexWriter indexWriter = new IndexWriter(dir, iwc);
		indexWriter.deleteAll();
		indexWriter.commit();

		Document doc = new Document();
		Field fieldType = new TextField("type", "0", Field.Store.YES);
		doc.add(fieldType);
		Field fieldContents = new TextField("contents", "今晚的辩题很道地:在我们这些人当中?",
				Field.Store.YES);
		doc.add(fieldContents);
		Field fieldId = new TextField("id", "1", Field.Store.YES);
		doc.add(fieldId);
		indexWriter.addDocument(doc);
		System.out.println(doc);

		doc = new Document();
		fieldType = new TextField("type", "0", Field.Store.YES);
		doc.add(fieldType);
		fieldContents = new TextField("contents", "java我们为电影《今朝》是一部不错的影片。",
				Field.Store.YES);
		doc.add(fieldContents);
		fieldId = new IntField("id", 2, Field.Store.YES);
		doc.add(fieldId);
		indexWriter.addDocument(doc);
		System.out.println(doc);

		doc = new Document();
		fieldType = new TextField("type", "0", Field.Store.YES);
		doc.add(fieldType);
		fieldContents = new TextField("contents", "我们到底是啥意思呢?", Field.Store.YES);
		doc.add(fieldContents);
		fieldId = new IntField("id", 3, Field.Store.YES);
		doc.add(fieldId);
		indexWriter.addDocument(doc);
		System.out.println(doc);
		indexWriter.close();

		System.out.println("------------------------------------------------");
		IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(
				indexPath)));
		IndexSearcher searcher = new IndexSearcher(reader);
		BooleanQuery query = new BooleanQuery();

		String key = "java";
		String type = "0";
		Query contentQuery = new TermQuery(new Term("contents", key));
		query.add(contentQuery, BooleanClause.Occur.MUST);

		Query typeQuery = new TermQuery(new Term("type", type));
		query.add(typeQuery, BooleanClause.Occur.MUST);

		TopDocs results = searcher.search(query, 5);
		ScoreDoc[] hits = results.scoreDocs;
		for (int i = 0; i < hits.length; i++) {
			Document _doc = searcher.doc(hits[i].doc);
			System.out.println("Document内容为 : " + _doc);
		}
		System.out.println("共检索出符合条件的Document " + hits.length + " 个。");
	}

	@Test
	public void test4() throws Exception {
		System.out.println("Main thread waiting...");
		String indexPath = "E:\\Lucene\\index"; // 索引保存目录
		Directory dir = FSDirectory.open(new File(indexPath));
		IndexWriterConfig iwc1 = new IndexWriterConfig(Version.LUCENE_4_10_0,
				analyzer);
		final IndexWriter indexWriter1 = new IndexWriter(dir, iwc1);
		System.out.println(Thread.currentThread().getName() + " ");
		Document doc = new Document();
		Field fieldType = new TextField("type", "0", Field.Store.YES);
		doc.add(fieldType);
		Field fieldContents = new TextField("contents", "今晚的辩题很道地:在我们这些人当中?",
				Field.Store.YES);
		doc.add(fieldContents);
		Field fieldId = new TextField("id", new Random().nextInt(1000) + "",
				Field.Store.YES);
		doc.add(fieldId);
		try {
			indexWriter1.addDocument(doc);
			indexWriter1.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		// IndexWriterConfig iwc2 = new IndexWriterConfig(Version.LUCENE_4_10_0,
		// analyzer);
		// IndexWriter indexWriter2 = new IndexWriter(dir, iwc2);

	}

	@Rule
	public ContiPerfRule rule = new ContiPerfRule();
	static IndexWriter indexWriter1 = null;
	static String indexPath2 = "E:\\Lucene\\index"; // 索引保存目录
	static IndexReader reader = null;
	static {
		Directory dir;
		try {
			dir = FSDirectory.open(new File(indexPath2));
			IndexWriterConfig iwc1 = new IndexWriterConfig(
					Version.LUCENE_4_10_0, analyzer);
			indexWriter1 = new IndexWriter(dir, iwc1);
			reader = DirectoryReader.open(dir);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Test
	@PerfTest(invocations = 10, threads = 10, timer = RandomTimer.class, timerParams = {
			20, 60 })
	@Required(max = 15000, average = 8000)
	public void testMultiThread() throws IOException {
		indexWriter1.deleteAll();
		indexWriter1.commit();
		print("delete");

		UUID id = UUID.randomUUID();
		Document doc = new Document();
		Field fieldType = new TextField("type", "0", Field.Store.YES);
		doc.add(fieldType);
		Field fieldContents = new TextField("contents", "今晚的辩题很道地:在我们这些人当中?"
				+ id, Field.Store.YES);
		doc.add(fieldContents);
		Field fieldId = new TextField("id", id + "", Field.Store.YES);
		doc.add(fieldId);
		try {
			indexWriter1.addDocument(doc);
			indexWriter1.commit();
		} catch (IOException e) {
			e.printStackTrace();
		}
		print("add");
	}

	public void print(String tag) throws IOException {
		String name = Thread.currentThread().getName();
		System.out.println("--------------------start " + name + " " + tag
				+ "----------------------------------");
		String key = "今晚";
		Query contentQuery = new TermQuery(new Term("contents", key));
		IndexSearcher searcher = new IndexSearcher(reader);
		TopDocs results = searcher.search(contentQuery, 5);
		ScoreDoc[] hits = results.scoreDocs;
		for (int i = 0; i < hits.length; i++) {
			Document _doc = searcher.doc(hits[i].doc);
			System.out.println(_doc.get("contents") + "  " + name + " " + tag);
		}
		System.out.println("--------------------end " + name + " " + tag
				+ "----------------------------------\n");
	}
}
最近下载更多
813405250  LV5 2021年1月2日
pengkui  LV14 2020年6月25日
96199chao  LV3 2019年12月21日
人间蒸发  LV23 2019年4月21日
2985225214  LV10 2019年4月18日
764938214  LV7 2019年1月25日
justinlucas  LV6 2018年11月9日
icestan  LV2 2018年6月22日
hefenyuan91  LV11 2018年3月30日
yidongdematong  LV2 2018年3月14日
最近浏览更多
lirui9900  LV1 2021年11月16日
MOKE_YE  LV3 2021年10月8日
shion994  LV6 2021年5月18日
lclinlclin  LV14 2021年1月18日
813405250  LV5 2021年1月2日
cgp0219  LV6 2020年11月9日
fsrylb 2020年9月21日
暂无贡献等级
3248066  LV2 2020年6月1日
mjx201  LV11 2020年2月6日
lekers  LV14 2020年1月2日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友