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();
}
}
上一篇: Android面试题精选,自己收藏下
下一篇: lucene入门系列(二、建立索引)