`

lucene3.6入门实例

 
阅读更多

import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import com.wangxiaowang.article.Article;
import com.wangxiaowang.article.ArticleProvider;
public class ArticleIndexBuilder {

private String indexPath;
private Analyzer analyzer;
private int recordCountPreTime;

public ArticleIndexBuilder(String indexPath, Analyzer analyzer, int recordCountPreTime) {
this.indexPath = indexPath;
this.analyzer = analyzer;
this.recordCountPreTime = recordCountPreTime;
}

public void build() {
FSDirectory directory = null;
IndexWriterConfig conf = null;
IndexWriter writer = null;
try {
directory = FSDirectory.open(new File(indexPath));
conf = new IndexWriterConfig(Version.LUCENE_36, analyzer);
conf.setOpenMode(OpenMode.CREATE);
writer = new IndexWriter(directory, conf);

ArticleProvider articleProvider = new ArticleProvider(recordCountPreTime);
while (articleProvider.hasNext()) {
List<Article> articleList = articleProvider.next();
addDocs(writer, articleList);
}

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
writer.close();
directory.close();
writer = null;
directory = null;
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

private void addDocs(IndexWriter writer, List<Article> articleList) throws CorruptIndexException, IOException {
for (Article article : articleList) {
Document doc = new Document();
addFileds(doc, article);
writer.addDocument(doc);
System.out.println("=========>one record ok " + article.getStr("title"));
}
}

private void addFileds(Document doc, Article article) {
doc.add(getKeywordsField("id", article.getInt("id") + ""));
doc.add(getIndexField("title", article.getStr("title")));
doc.add(getIndexField("content", article.getStr("keywords")));
doc.add(getKeywordsField("subject_id", article.getInt("subject_id") + ""));
doc.add(getKeywordsField("subject_name", article.getStr("subject_name")));
doc.add(getKeywordsField("publish_time", fomartPublishTime(article.getTimestamp("publish_time"))));
}

private String fomartPublishTime(Timestamp time) {
String result = "";
if (time == null)
time = new Timestamp(System.currentTimeMillis());
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
result = df.format(time);
return result;
}

private Field getKeywordsField(String name, String value) {
return new Field(name, value, Store.YES, Index.NOT_ANALYZED);
}

private Field getIndexField(String name, String value) {
return new Field(name, value, Store.YES, Index.ANALYZED);
}
}

 

import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
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 com.jfinal.plugin.activerecord.Page;
import com.wangxiaowang.article.Article;

public class ArticleIndexSearcher {
private String indexPath;
private Analyzer analyzer;

public ArticleIndexSearcher(String indexPath, Analyzer analyzer) {
this.indexPath = indexPath;
this.analyzer = analyzer;
}

public Page<Article> search(String queryStr, int pageSize, int pageNum, int limits) {
FSDirectory directory = null;
IndexReader reader = null;
IndexSearcher searcher = null;
List<Article> articleList = new ArrayList<Article>();
Page<Article> articlePage = null;
int start = (pageNum - 1)*pageSize + 1;
int end = pageNum*pageSize;
int total = 0;
try {
directory = FSDirectory.open(new File(indexPath));
reader = IndexReader.open(directory);
searcher = new IndexSearcher(reader);
QueryParser qp = new MultiFieldQueryParser(Version.LUCENE_36, new String[] {"title","keywords"}, analyzer);
Query query = qp.parse(queryStr);

//不需要排序
ScoreDoc[] docs = searcher.search(query, limits).scoreDocs;

//高亮
SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("<font color='#FF0000''>", "</font>");
Highlighter highlighter = new Highlighter(simpleHTMLFormatter, new QueryScorer(query));
highlighter.setTextFragmenter(new SimpleFragmenter(1500));

total = docs.length;
for (int i=start; i<=end && i<total; i++) {
Document d = searcher.doc(docs[i].doc);
String titleToBeHightlight = d.get("title");
if (titleToBeHightlight == null)
titleToBeHightlight = "";
TokenStream tokenStream = analyzer.tokenStream("title", new StringReader(titleToBeHightlight));
String title = highlighter.getBestFragment(tokenStream, titleToBeHightlight);
Article article = buildArticle(d.get("id"), title, d.get("content"), d.get("subject_id"), d.get("subject_name"), d.get("publish_time"));
articleList.add(article);
}

articlePage = new Page<Article>(articleList, pageNum, pageSize, (total+pageSize-1)/pageSize, total);
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (InvalidTokenOffsetsException e) {
e.printStackTrace();
} finally {
try {
searcher.close();
reader.close();
directory.close();
} catch (IOException e) {
e.printStackTrace();
}
}

return articlePage;
}

private Article buildArticle(String id, String title, String keywords, String subjectId, String subjectName, String publishTime) {
Article article = new Article();
article.set("id", id);
article.set("title", title);
article.set("content", keywords);
article.set("subject_id", subjectId);
article.set("subject_name", subjectName);
article.set("publish_time", publishTime == null ? "2012-06-01" : publishTime);
return article;
}
}

分享到:
评论

相关推荐

    lucene3.6入门实例教程

    《Lucene 3.6 入门实例教程》是一份专为初学者设计的指南,旨在帮助用户快速掌握Apache Lucene 3.6版本的基本概念和应用。Lucene是一个高性能、全文检索库,广泛用于构建搜索功能强大的应用程序。这份教程通过完整的...

    lucene3.6 搜索例子

    《Lucene 3.6 搜索实例解析》 Apache Lucene 是一个开源全文搜索引擎库,为开发者提供了在Java应用程序中实现高效、可扩展的搜索功能的工具。在本篇文章中,我们将深入探讨Lucene 3.6版本中的搜索功能,通过实例...

    第一个Lucene 3.6 (3.X) 入门实例

    【标题】:“第一个Lucene 3.6 (3.X) 入门实例” ...总之,Lucene 3.6入门实例涉及从设置开发环境、创建索引到实现搜索功能的全过程。理解这些基本步骤和核心概念,是掌握Lucene并进一步构建高效检索系统的基石。

    lucene3.6.jar

    这里的“lucene3.6.jar”是一个包含了Lucene 3.6版本核心功能的Java类库,它是实现全文检索的基础。 Lucene的核心特性包括索引构建、查询解析、搜索执行以及结果排序等。索引构建允许开发者将大量文本数据转换为...

    lucene3.6实例(索引和查询)

    在网上找了实例,但是发现不能使用,只能简历索引。...lucene3.6版本,能够建立索引,能搜索。inderwriter,indexsearch. 其中包C下的helloword实例能用,其余的全是网上不能用的。直接下载 可以运行

    lucene3.6的入门案例

    **Lucene 3.6 入门案例** Lucene 是一个高性能、全文本搜索库,由 Apache 软件基金会开发。它提供了完整的搜索功能,包括索引、查询、评分等,广泛应用于各种项目和产品中。在这个入门案例中,我们将深入理解如何...

    lucene 3.6 全文检索

    在Lucene 3.6中,首先需要创建一个IndexWriter实例,用于构建和更新索引。IndexWriter配置了各种参数,如分词器(Analyzer)、写入模式等。接着,通过addDocument()方法将一个个文档添加到索引中。每个文档由多个...

    lucene3.6 模仿百度自动补全

    lucene3.6 模仿百度自动补全(lucene3.6 模仿百度自动补全(lucene3.6 模仿百度自动补全(lucene3.6 模仿百度自动补全(lucene3.6 模仿百度自动补全(lucene3.6 模仿百度自动补全(lucene3.6 模仿百度自动补全

    Lucene 3.6 学习笔记

    第一章 LUCENE基础 2 1.1 索引部分的核心类 2 1.2 分词部分的核心类 2 1.3 搜索部分的核心类 2 第二章 索引建立 3 2.1 创建Directory 3 2.2 创建Writer 3 2.3 创建文档并且添加索引 4 2.4 查询索引的基本信息 5 2.5 ...

    IKAnalyzer修复源码,Lucene3.6 Jar及使用示例

    在实际应用中,开发者可以结合Lucene3.6提供的API,构建索引和执行查询。例如,将分词后的文本作为Lucene的Document字段,建立索引;然后,使用Analyzer进行搜索查询,实现高效的全文搜索功能。 总结来说,这个修复...

    lucene 3.6

    在 Lucene 3.6 版本中,它提供了强大的文本搜索功能,适用于Java开发人员。这个版本相对稳定,对于初学者来说是一个很好的学习起点。 首先,我们来了解一下 Lucene 的核心概念: 1. **索引(Index)**:Lucene 的...

    lucene3.6工程原文件

    **Lucene 3.6 全文检索框架详解** Lucene 是一个开源的全文检索库,由 Apache 软件基金会开发。它提供了一个高级的、灵活的、可扩展的搜索程序开发框架,使得开发者可以轻松地在应用程序中集成全文搜索功能。在...

    基于lucene3.6平台搜索工具相关包及使用说明

    《基于Lucene 3.6平台的搜索工具详解与应用指南》 Lucene是一个高性能、全文本搜索引擎库,由Apache软件基金会开发并维护。在3.6版本中,Lucene提供了一套完整的搜索解决方案,包括索引构建、查询解析、结果排序等...

    lucene3.6 的源代码

    《深入理解Lucene 3.6:源代码解析》 Lucene是一个开源的全文检索库,由Apache软件基金会开发并维护。在版本3.6中,Lucene提供了一整套强大的文本搜索功能,包括索引、查询、排序、分词等。这个版本的源代码为我们...

    lucene 3.6 索引格式总结

    本文档详细介绍了lucene3.6中的索引,以及每个部分对应于硬盘下的文件夹里的哪个文件。这个根据本人多年学术及编程经验总结的

    lucene3.6+IKAnalyzer2012FF_u1

    《深入理解Lucene 3.6与IKAnalyzer 2012FF_u1:构建高效中文搜索引擎》 在信息技术领域,搜索引擎是数据检索的核心工具,而Lucene作为Apache软件基金会的开源全文搜索引擎库,因其强大的搜索功能和灵活性,被广泛...

    基于Lucene3.6进行全文检索的小案例

    在本文中,我们将深入探讨如何使用Apache Lucene 3.6版本进行全文检索的开发。Apache Lucene是一个高性能、全文本搜索库,它为开发者提供了强大的文本搜索功能,使得在各种应用中实现复杂而高效的搜索引擎成为可能。...

    lucene 3.6 检索文件 pdf word ppt excel txt html xml

    《Lucene 3.6 全文检索技术详解与应用》 Lucene 是一个高性能、全文本搜索引擎库,由Apache软件基金会开发。在版本3.6中,它提供了强大的文件检索功能,支持对多种文件类型的搜索,包括PDF、Word、PPT、Excel、TXT...

    lucene 3.0 入门实例

    **Lucene 3.0 入门实例** Lucene 是一个高性能、全文本搜索库,由 Apache 软件基金会开发。它提供了完整的搜索功能,包括索引、查询解析、排序以及高级的文本分析能力。在 Lucene 3.0 版本中,开发者可以利用其强大...

    Lucene3.0入门实例含jar包

    **Lucene 3.0 入门实例及关键知识点** Lucene 是一个开源的全文搜索引擎库,由 Apache 软件基金会开发。它为开发者提供了在应用程序中实现文本搜索功能的强大工具。本实例主要针对 Lucene 3.0 版本,这个版本虽然...

Global site tag (gtag.js) - Google Analytics