`
chenbj920
  • 浏览: 3617 次
  • 性别: Icon_minigender_1
  • 来自: 广州
最近访客 更多访客>>
社区版块
存档分类
最新评论

lucene 索引

阅读更多
//索引UCD操作

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
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.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;
import org.wltea.analyzer.lucene.IKQueryParser;

public class UCDIndex {

private Directory dir;
private IndexWriter writer;

public UCDIndex() {
}

public UCDIndex(String indexDir) throws IOException {
this.dir = FSDirectory.open(new java.io.File(indexDir));
Analyzer analyzer = new IKAnalyzer();
this.writer = new IndexWriter(dir, new IndexWriterConfig(
Version.LUCENE_32, analyzer));
}

public boolean removeDocument(String docKey) throws IOException{

try{
Query query = IKQueryParser.parse("key", docKey);
writer.deleteDocuments(query);
}catch(IOException e){
return false;
}finally{
try {
if(dir != null && IndexWriter.isLocked(dir)){
IndexWriter.unlock(dir);
}
} catch (IOException e) {}
}

return true;
}

public boolean updateDocument(Index index) throws IOException{

removeDocument(index.getKey());

return addDocument(index);
}

public boolean addDocument(Index index){

Document doc = new Document();
doc.add(new Field("key", index.getKey(), Field.Store.YES,
Field.Index.ANALYZED));

doc.add(new Field("title", index.getTitle(), Field.Store.YES,
Field.Index.ANALYZED));

SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
doc.add(new Field("date", sdf.format(new Date()), Field.Store.YES,
Field.Index.ANALYZED));

doc.add(new Field("content", index.getContent(), Field.Store.YES,
Field.Index.ANALYZED));

doc.add(new Field("contentType", index.getContentType(), Field.Store.YES,
Field.Index.ANALYZED));

try{
writer.addDocument(doc);

return true;

}catch(Exception e){
return false;
}finally{
try {
if(dir != null && IndexWriter.isLocked(dir)){
IndexWriter.unlock(dir);
}
} catch (IOException e) {}
}
}

public boolean addDocument(List<Index> indexList){

int count = 0;
if(indexList != null && !indexList.isEmpty()){
for(Index index:indexList){
if(addDocument(index)){
count++;
}else{
break;
}
}
return count == indexList.size();
}

return false;
}

public void close() {
if(writer != null){
try {
writer.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public void commit() {
if(writer != null){
try {
writer.commit();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

//索引查询
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.document.Document;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;
import org.wltea.analyzer.lucene.IKQueryParser;
import org.wltea.analyzer.lucene.IKSimilarity;

public class QueryIndex {

private IndexSearcher searcher;

public QueryIndex() {
}

public QueryIndex(String indexDir) throws IOException {
searcher = new IndexSearcher(FSDirectory.open(new java.io.File(indexDir)));
searcher.setSimilarity(new IKSimilarity());
}

public List<Index> search(String keyword) throws IOException {

List<Index> docList = new ArrayList<Index>();

try{
Query query = IKQueryParser.parseMultiField(new String[] {"title","contents"}, keyword);

TopDocs hits = searcher.search(query, 50);

for (int i = 0; i < hits.scoreDocs.length; i++) {
Document hitDoc = searcher.doc(hits.scoreDocs[i].doc);
Index doc = new Index();
String content = hitDoc.get("contents");
if (content != null) {
if (content.length() > 200) {
content = content.substring(0, 200) + "...";
}
content = content.replaceAll(keyword, "<font color=\"red\">"
+ keyword + "</font>");
doc.setContent(content);
}
if (hitDoc.get("title") != null) {
doc.setTitle(hitDoc.get("title").replaceAll(keyword,
"<font color=\"red\">" + keyword + "</font>"));
}
doc.setDate(hitDoc.get("date"));
docList.add(doc);
}
}catch(IOException e){
searcher.close();
}

return docList;
}
}

public class Index {

private String key;
private String contentType;
private String title;
private String content;
private String date;

public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
分享到:
评论

相关推荐

    Lucene索引器实例

    **Lucene索引器实例详解** Lucene是一个高性能、全文本搜索库,由Apache软件基金会开发,被广泛应用于各种搜索引擎的构建。它提供了一个高级的、灵活的、可扩展的接口,使得开发者能够轻松地在应用程序中实现全文...

    lucene索引查看工具及源码

    在使用 Lucene 进行信息检索时,有时我们需要对建立的索引进行查看、调试或分析,这时就需要借助 Lucene 的索引查看工具。 Luke 是一个非常实用的 Lucene 索引浏览器,全称为 Lucidworks Luke。它允许用户以图形化...

    lucene 索引 查看 工具

    这就是"Lucene 索引 查看 工具"的用途,它可以帮助我们分析和理解 Lucene 索引的工作原理。 主要知识点: 1. **Lucene 索引**:Lucene 的索引是一种倒排索引,它将文档中的词项(tokens)映射到包含这些词项的文档...

    lucene索引查看程序及代码

    《深入理解Lucene索引查看程序与代码》 在信息技术领域,搜索引擎的高效运作离不开底层索引技术的支持,而Lucene作为Apache软件基金会的一个开放源代码项目,正是一个强大的全文检索库,它提供了高效的文本搜索功能...

    深入 Lucene 索引机制

    以下是对Lucene索引机制的详细解析: 一、Lucene的索引过程 1. 文档分析:当向Lucene添加文档时,首先会经过一个分词器(Tokenizer),将文本拆分成一系列的词项(Token)。接着,这些词项会被过滤(Filter)和...

    Lucene索引和查询

    **Lucene索引和查询** Lucene是Apache软件基金会的开放源码全文搜索引擎库,它提供了文本检索的核心工具,使得开发者能够快速构建自己的搜索应用。本项目中的代码旨在展示如何利用Lucene对多个文件夹下的数据进行...

    lucene索引查看工具luck7.4.0

    `Luck`,全称`Luke`,是一款强大的Lucene索引浏览器和分析器工具,可以帮助开发者、数据分析师以及对Lucene感兴趣的人员查看、理解和调试Lucene索引。 `Luke 7.4.0`是这款工具的一个特定版本,它专门设计用来与...

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

    《深入理解Lucene索引文件查看工具LukeAll 4.7.1》 在信息检索领域,Lucene作为一款强大的全文搜索引擎库,被广泛应用在各种数据检索系统中。然而,对于开发者来说,理解并调试Lucene创建的索引文件并非易事。此时...

    Lucene索引查看工具

    lukeall-0.9.jar为Lucene索引查看工具,方便大家查看索引

    Lucene 索引的简单使用

    以上就是关于“Lucene索引的简单使用”的详细介绍,包括其核心概念、创建和查询索引的步骤以及一些高级特性。希望对你理解和应用Lucene有所帮助。在实际开发中,可以根据需求选择合适的Analyzer,优化索引策略,以...

    luke源码--查看lucene索引文件

    《深入理解Luke:洞察Lucene索引文件》 在信息技术领域,搜索引擎的高效运作离不开对数据的快速检索,而Lucene作为开源全文检索库,扮演了核心角色。在这个过程中,Luke工具提供了一种直观的方式,让我们能够查看和...

    lucene索引文件格式介绍

    以下是对Lucene索引文件格式的详细说明。 首先,我们要理解Lucene索引的基本结构。一个Lucene索引位于一个文件夹中,这个文件夹包含了多个段(Segment)。每个段是独立的,包含了一组文档,并且可以与其他段合并。...

    lucene索引结构原理

    **Lucene索引结构原理** Lucene是Apache软件基金会的开放源代码全文搜索引擎库,它为Java开发人员提供了强大的文本搜索功能。理解Lucene的索引结构原理对于优化搜索性能和设计高效的搜索应用至关重要。 首先,我们...

    Lucene索引的基本操作

    **Lucene索引的基本操作** Lucene是一款由Apache软件基金会开发的全文检索库,它提供了高效、可扩展的全文检索功能。在Java开发环境中,Lucene是广泛使用的文本搜索工具,能够帮助开发者构建复杂的搜索引擎。本文将...

    很好的lucene索引查看工具,欢迎各位lucene研究者前来下载

    本文将详细介绍一款被称为“很好的lucene索引查看工具”的实用软件,旨在帮助用户更好地理解和调试Lucene索引。 Lucene索引查看工具是一款专为Lucene设计的可视化工具,它允许用户直观地浏览和分析由Lucene创建的...

    lucene 索引小示例

    《Lucene索引小示例解析》 Lucene是一个高性能、全文检索库,它由Apache软件基金会开发并维护。在Java编程环境中,Lucene被广泛应用于构建搜索功能,特别是对于大量文本数据的高效检索。本篇文章将通过一个简单的小...

    lucene索引查看工具

    这款已经老了,2.4以后的lucene索引用不了。我上传了最新版本的,有需要的话!请到http://download.csdn.net/source/1423241 下。一款可以查看Lucene分词后在索引的排名以及是否有无该词,很多时候用于查看有无需要...

    lucene索引结构原理.docx

    而在Lucene中,基本单位是Document,它同样由多个字段组成,但Lucene索引的是这些字段的内容,以加速文本检索。 - **索引构建**:Lucene支持增量索引和批量索引,可以处理数据源的小幅变化或大规模数据。数据库通常...

    lucene索引库查看器5.3.0

    《深入理解Lucene索引库查看器5.3.0》 Lucene是一个开源的全文检索库,被广泛应用于各种搜索引擎的开发。在对Lucene进行开发和调试时,一个强大的工具——Lucene索引库查看器(Luke)发挥了至关重要的作用。 Luke ...

    luke--- lucene索引数据查看器

    **luke——Lucene索引数据查看器** Luke是一款强大的Lucene索引浏览器,它为开发者和搜索引擎优化人员提供了一种直观的方式来查看和分析由Apache Lucene创建的索引。Lucene是一个开源全文检索库,广泛应用于各种...

Global site tag (gtag.js) - Google Analytics