欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

lucene入门代码二(TermQuery)

程序员文章站 2022-07-09 10:26:35
...

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();

}
}