`

lucene 4.7 (2)全文检索之查询

 
阅读更多
package org.apache.lucene.demo;

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;
import java.io.StringReader;
import java.util.Date;

import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryFilter;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.InvalidTokenOffsetsException;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleFragmenter;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

import thtf.ebuilder.website.search.DBIndex;

/** Simple command-line based search demo. */
public class SearchFiles {

  private SearchFiles() {}

  /** Simple command-line based search demo. */
  public static void main(String[] args) throws Exception {

    String field = "INFO_CONTENT";
    String word = "舞蹈";
    int hitsPerPage = 10;
    
    IndexReader reader = DirectoryReader.open(FSDirectory.open(DBIndex._$.getIndexFile()));
    IndexSearcher searcher = new IndexSearcher(reader);
    QueryParser parser = new QueryParser(Version.LUCENE_47, field, DBIndex._$.analyzer);
      
      Query query = parser.parse(word);
      System.out.println("Searching for: " + query.toString(field));
            
      //排序
      Sort sort=new Sort(new SortField[]{new SortField("info_id", SortField.Type.INT, true)});
      //过滤
      BooleanQuery bqf = new BooleanQuery();
      bqf.add(query,BooleanClause.Occur.SHOULD);
       
      
        Date start = new Date();
       TopDocs tDocs=searcher.search(query,new QueryFilter(bqf),100,sort);
       System.out.println("查询到:"+tDocs.scoreDocs.length);
        Date end = new Date();
        System.out.println("Time: "+(end.getTime()-start.getTime())+"ms");

      doPagingSearch(word, searcher, query, hitsPerPage);
    reader.close();
  }

  /**
   * This demonstrates a typical paging search scenario, where the search engine presents 
   * pages of size n to the user. The user can then go to the next page if interested in
   * the next hits.
   * 
   * When the query is executed for the first time, then only enough results are collected
   * to fill 5 result pages. If the user wants to page beyond this limit, then the query
   * is executed another time and all hits are collected.
   * 
   */
  public static void doPagingSearch(String word,IndexSearcher searcher, Query query, 
                                     int hitsPerPage) throws IOException {
 
    // Collect enough docs to show 5 pages
    TopDocs results = searcher.search(query, 5 * hitsPerPage);
    ScoreDoc[] hits = results.scoreDocs;
    
    int numTotalHits = results.totalHits;
    System.out.println(numTotalHits + " total matching documents");

    int start = 0;
    int end = Math.min(numTotalHits, hitsPerPage);
      end = Math.min(hits.length, start + hitsPerPage);
      System.out.println(start+"-"+end);
      for (int i = start; i < end; i++) {
        Document doc = searcher.doc(hits[i].doc);
        SimpleHTMLFormatter formatter=new SimpleHTMLFormatter("<b><font color='red'>","</font></b>");
        Highlighter highlighter=new Highlighter(formatter, new QueryScorer(query));
        highlighter.setTextFragmenter(new SimpleFragmenter(400));
        String content=doc.get("info_title");
        if(content!=null){
          TokenStream tokenstream=DBIndex._$.analyzer.tokenStream(word, new StringReader(content));
          try {
            content=highlighter.getBestFragment(tokenstream, content);
          } catch (InvalidTokenOffsetsException e) {
            e.printStackTrace();
          }
          System.out.println(doc.get("info_id")+"\t"+content);
        }
      }
  }
}

 

分享到:
评论

相关推荐

    lucene4.7官方完整包

    《Lucene 4.7:官方完整包详解》 Lucene是一个开源的全文搜索引擎库,由Apache软件基金会开发并维护。作为Java平台上的一个高性能、可扩展的信息检索库,Lucene为开发者提供了强大的文本搜索功能。本文将深入探讨...

    Lucene4.7+IK Analyzer中文分词入门教程

    【Lucene4.7+IK Analyzer中文分词入门教程】 Lucene是一个开源的全文检索库,它提供了文本分析、索引和搜索的核心工具。在这个入门教程中,我们将使用Lucene 4.7版本,结合IK Analyzer,一个专门针对中文分词的开源...

    Lucene4.7-Web 例子

    Lucene4.7是Apache Lucene项目的一个重要版本,它包含了对搜索性能的优化、新的分析器以及更强大的查询语法。此版本改进了倒排索引的存储效率,提高了搜索速度,并支持更多样化的查询表达式,使得开发者能够构建出...

    lucene4.7 开发简单实例

    Lucene 是一款强大的全文搜索引擎库,广泛应用于各种信息检索系统中。在本实例中,我们将深入探讨Lucene 4.7版本,涵盖索引的创建、修改、删除,以及查询时的排序、分页、优化和高亮显示等功能。此外,我们还将了解...

    ssh集成Lucene4.7demo

    在这个“ssh集成Lucene4.7demo”项目中,开发者将SSH框架与Lucene 4.7版本的全文搜索引擎进行了整合,同时还引入了IKAnalyzer作为中文分词器,以支持对中文文本的处理。这个示例项目不仅包含了基本的整合工作,还...

    apache Lucene4.7最全最新的jar包

    Apache Lucene是一个高性能、全文本搜索库,由Java编写,被广泛用于开发搜索引擎和需要文本检索功能的应用程序。Apache Lucene 4.7是该库的一个版本,它提供了丰富的功能和改进,使得开发者能够轻松地在他们的应用中...

    lucene4.7所需jar包

    Lucene 4.7 是一个高性能、全文本检索库,由Apache软件基金会开发并维护。这个版本的Lucene提供了一套强大的工具,用于在大量文本数据中进行高效的搜索和索引。Lucene的核心功能包括文档的索引、查询解析、评分、...

    Lucene 4.7 常用jar集合

    Lucene 是一个高性能、全文本搜索库,由 Apache 软件基金会开发,广泛应用于各种 Java 应用程序中,特别是那些需要高效检索功能的系统。在这个"Lucene 4.7 常用jar集合"中,包含了Lucene 4.7版本的一些核心组件,...

    lucene 4.7 jar

    Lucene 是一个开源的全文检索库,由Apache软件基金会维护,是Java开发的。这个"lucene 4.7 jar"文件是Lucene 4.7.0版本的归档包,包含了所有必要的类库,使得开发者可以直接在自己的项目中使用,而无需从头构建或...

    lucene-4.7.0全套jar包

    2. **搜索算法**:Lucene实现了布尔、短语、模糊、评分等高级搜索算法,使得用户可以灵活地构造复杂的查询条件。 3. **高性能**:Lucene通过内存缓存、位向量和优化的数据结构来提高搜索速度。同时,它支持多线程...

    lucene 4.7.2 Demo

    创建索引是全文检索的基础,它涉及将文本数据结构化为Lucene可以理解和查询的形式。开发者可以通过Analyzer类来处理输入的文本,进行分词、去除停用词等预处理步骤。然后,使用Document类表示要索引的数据,Field类...

    lucene-4.7.0.

    Lucene是一个由Apache软件基金会开发的开源全文检索库,它为Java开发者提供了强大的文本搜索功能。在4.7.0这个版本中,Lucene不仅强化了其核心搜索功能,还对高亮显示进行了优化,使得用户能够更直观地找到搜索结果...

    lucene-4.7.0.zip

    1. 性能优化:在4.7.0版本中,Lucene对索引构建和查询性能进行了深度优化,提高了检索速度,降低了内存消耗,使得大规模数据的搜索更加高效。 2. 新增功能:引入了新的分析器,如PerFieldAnalyzerWrapper,允许针对...

    lucene-4.7.0官方文档

    《Lucene 4.7.0官方文档》是开发者们深入了解和使用Apache Lucene库的重要参考资料,这是一款广泛应用于全文检索、信息检索领域的开源Java库。Lucene提供了强大的文本分析和索引功能,使得开发者可以轻松地在大量...

    apache solr guide 4.7

    **Apache Solr** 是一个高性能、基于 Lucene 的全文检索服务系统,广泛应用于互联网企业的搜索服务中。Solr 提供了高度可扩展且稳定的搜索功能,并支持多种集成方式,易于开发人员上手使用。 #### 二、Apache Solr ...

    solr4.7服务器

    Solr4.7服务器是基于Java的开源搜索和分析引擎,它在Apache Lucene库的基础上构建,用于提供高效、可扩展的全文检索、数据分析和分布式搜索服务。此版本的Solr适用于Java运行环境JDK1.6,这表明它是对较旧Java版本的...

    Lucene索引文件查看工具lukeall4.7.1

    在信息检索领域,Lucene作为一款强大的全文搜索引擎库,被广泛应用在各种数据检索系统中。然而,对于开发者来说,理解并调试Lucene创建的索引文件并非易事。此时,一个高效的索引查看工具显得尤为重要,这就是我们要...

    solr4.7从数据库导数据[参照].pdf

    ### Solr 4.7 从数据库导入数据创建索引详解 #### 一、概述 在实际工程应用中,从数据库导出数据并创建索引来优化搜索效率是一种常见的做法。本文将详细介绍如何使用Solr 4.7从SQL Server 2005数据库中导入数据并...

    solr4.7+中文分词器IK Analyzer 2012FF_hf1

    Solr 4.7 是一个基于 Lucene 的全文检索服务器,它提供了强大的搜索功能和配置灵活性。IK Analyzer 2012FF_hf1 是一个针对中文的分词器,专为处理中文文本而设计,旨在提高中文文本的索引和搜索效率。这个组合在描述...

    【分享:lucene学习资料】---<下载不扣分,回帖加1分,欢迎下载,童叟无欺>

    2&gt; 全文检索的实现机制 【1】lucene学习笔记的目录如下 1. 概述 3 2. lucene 的包结构 3 3. 索引文件格式 3 4. lucene中主要的类 4 4.1. Document文档类 4 4.1.1. 常用方法 4 4.1.2. 示例 4 4.2. Field字段类 4 ...

Global site tag (gtag.js) - Google Analytics