Lunene多样化查询(五)之TermQuery
一、TermQuery概述
对索引中指定的项进行精确搜索是最基本的搜索方式,其中Term是索引中最小的片段,每一个Term包含一个域名和一个文本值。
只有搜索对应的文本域值该文档才能被检索到
1.1 初始化一个Term实例
Term term = new Term("NAME","中国人");
1.2 TermQuery构造一个单独的Term对象作为其参数
TermQuery termQuery = new TermQuery(term);
1.3 TermQuery三种构造器
- 基础,最常用的一种只需要将Term对象传进即可。
/** Constructs a query for the term <code>t</code>. */
public TermQuery(Term t) {
this(t, -1);
}
- 频率,如果希望该词语出现的频率请使用这种
/** Expert: constructs a TermQuery that will use the
* provided docFreq instead of looking up the docFreq
* against the searcher. */
public TermQuery(Term t, int docFreq) {
term = t;
this.docFreq = docFreq;
perReaderTermState = null;
}
- 通过TermContext配置对象来初始化条件
/** Expert: constructs a TermQuery that will use the
* provided docFreq instead of looking up the docFreq
* against the searcher. */
public TermQuery(Term t, TermContext states) {
assert states != null;
term = t;
docFreq = states.docFreq();
perReaderTermState = states;
}
二、示例
@Test
public void test02(){
//新建查询
/**********TermQuery指定字符查询,精确查询**************/
Term term = new Term("NAME", "中国人");
Query query = new TermQuery(term);
IndexSearcher isearcher = new IndexSearcher(ireader);
isearcher.search(query,10);
}