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三种构造器

  1. 基础,最常用的一种只需要将Term对象传进即可。
 /** Constructs a query for the term <code>t</code>. */
  public TermQuery(Term t) {
    this(t, -1);
  }
  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;
  }
  1. 通过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); 
    }

三、结束。谢谢大家!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值