zoukankan      html  css  js  c++  java
  • lucene-查询query->TermQuery按词条搜索

       TermQuery是最简单、也是最常用的Query。TermQuery可以理解成为“词条搜索”,在搜索引擎中最基本的搜索就是在索引中搜索某一词条,而TermQuery就是用来完成这项工作的。

      在Lucene中词条是最基本的搜索单位,从本质上来讲一个词条其实就是一个名/值对。只不过这个“名”是字段名,而“值”则表示字段中所包含的某个关键字。

      TermQuery是lucene查询中最基本的一种原子查询,从它的名字Term我们可以看出,它只能针对一个字段进行查询。

    例:

    @Test
    public void  testGovenQuery(){
        try {
            String  keyword="java";
            Directory dic=new SimpleFSDirectory(new File("x:xxx"));
            IndexSearcher searcher=new IndexSearcher(dic);
            //------------TermQuery  单个关键字查询
            Query query=new TermQuery(new Term("contents",keyword));
            TopDocs tops=searcher.search(query, 10);
            System.out.println(tops.totalHits);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    };

     讲解

    要使用TermQuery进行搜索首先需要构造一个Term对象,示例代码如下:

    Term aTerm = new Term("contents", "java");

    然后使用aTerm对象为参数来构造一个TermQuery对象,代码设置如下:

    Query query = new TermQuery(aTerm);

    这样所有在“contents”字段中包含有“java”的文档都会在使用TermQuery进行查询时作为符合查询条件的结果返回。

    package ch11;
    
    import org.apache.lucene.analysis.standard.StandardAnalyzer;
    import org.apache.lucene.document.Document;
    import org.apache.lucene.document.Field;
    import org.apache.lucene.index.IndexWriter;
    import org.apache.lucene.index.Term;
    import org.apache.lucene.search.IndexSearcher;
    import org.apache.lucene.search.Query;
    import org.apache.lucene.search.TermQuery;
    
    public class TermQueryTest
    
    {
    
        public static void main(String[] args) throws Exception
    
        {
    
            // 生成Document对象
    
            Document doc1 = new Document();
    
            // 添加“name”字段的内容
    
            doc1.add(Field.Text("name", "word1 word2 word3"));
    
            // 添加“title”字段的内容
    
            doc1.add(Field.Keyword("title", "doc1"));
    
            // 生成索引书写器
    
            IndexWriter writer = new IndexWriter("c://index",
                    new StandardAnalyzer(), true);
    
            // 将文档添加到索引中
    
            writer.addDocument(doc1);
    
            // 关闭索引
    
            writer.close();
    
            // 生成查询对象query
    
            Query query = null;
    
            // 生成hits结果对象,保存返回的检索结果
    
            Hits hits = null;
    
            // 生成检索器
    
            IndexSearcher searcher = new IndexSearcher("c://index");
    
            // 构造一个TermQuery对象
    
            query = new TermQuery(new Term("name", "word1"));
    
            // 开始检索,并返回检索结果到hits中
    
            hits = searcher.search(query);
    
            // 输出检索结果中的相关信息
    
            printResult(hits, "word1");
    
            // 再次构造一个TermQuery对象,只不过查询的字段变成了"title"
    
            query = new TermQuery(new Term("title", "doc1"));
    
            // 开始第二次检索,并返回检索结果到hits中
    
            hits = searcher.search(query);
    
            // 输出检索结果中的相关信息
    
            printResult(hits, "doc1");
    
        }
    
        public static void printResult(Hits hits, String key) throws Exception
    
      {
    
        System.out.println("查找 /"" + key + "/" :");
    
        if (hits != null)
    
        {
    
          if (hits.length() == 0)
    
          {
    
            System.out.println("没有找到任何结果");
    
          }
    
          else
    
          {
    
            System.out.println("找到" + hits.length() + "个结果");
    
            for (int i = 0; i < hits.length(); i++)
    
            {
    
              Document d = hits.doc(i);
    
              String dname = d.get("title");
    
              System.out.print(dname + "   ");
    
            }
    
            System.out.println();
    
            System.out.println();
    
          }
    
        }
    
      }
    }

    注意:字段值是区分大小写的,因此在查询时必须注意大小写的匹配。

    在代码中通过构建TermQuery的对象,两次完成了对关键字的查找。两次查找过程中不同的是,第一次构建的TermQuery是查找“name”这个字段,而第二次构建的TermQuery则查找的是“title”这个字段。

     

  • 相关阅读:
    合理的嵌入式开发学习路线
    Nginx
    RARP
    强弱电共地
    ACDC
    各电脑进Bios方法
    Java中Integer.parseInt
    全排列
    Java实现LRU缓存方案?
    缓存有关的几个问题
  • 原文地址:https://www.cnblogs.com/hwaggLee/p/5232392.html
Copyright © 2011-2022 走看看