lucene入门代码二(TermQuery)

package com.yale.lucene;

import java.io.File;

import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

/**
 * 
 * @author yale
 *一个简单的搜索使用TermQuery
 */
public class TermQueryTest
{
	public static void main(String[] args) throws Exception
	{
		// 索引放在这个目录
		String indexDir = "F://新建文件夹//luceneTest//indexFile";
		Directory dir = FSDirectory.open(new File(indexDir));
		IndexSearcher searcher = new IndexSearcher(dir);
		Term t =new Term("contents","apache");
		Query query = new TermQuery(t);
		TopDocs docs = searcher.search(query,10);
		System.out.println("======"+docs.totalHits+"==========");
		t = new Term("contents","copyright");
		docs = searcher.search(new TermQuery(t),10);
		System.out.println("======"+docs.totalHits+"==========");
		searcher.close();
		dir.close();
				
	}
}

你可能感兴趣的:(apache,Lucene,yale)