`
longgangbai
  • 浏览: 7358326 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

SpringLuence的学习总结(一)

阅读更多

          在最近,看了一下SpringLuence的源代码,学习关于搜索引擎的设计思路:

    首先关于搜索引擎的两个主要操作为:

    1.建立索引文档

    2.搜索查找信息

 创建索引文档,必须针对不同的文档,获取各种文档的内容,建立文档的信息。

关于建立索引文档的方法请看Luence的基础。

这里主要说一下关于Luence设计的思路。

无论任何文档都可以转换为输入流对象,然后获取文件的内容。

关于文档处理器的接口设计如下:

DocumentHandler

 

源代码如下:

public interface DocumentHandler {

 /**
  * Return whether or not this object can create a document from an
  * instance of the given class. 检测一个对象是否可以创建一个索引文档对象
  */
 boolean supports(Class clazz);
 
 /**
  * This method indexes an object and specifies some additional
  * properties on the Lucene document basing the description parameter.
  *
  * The object to index can be either a POJO or a stream on a resource.
  *
  * @param description the description of the resource to index  Map中存储索引文档的中索引的字段用于建立索引文档时使用
  * @param inputStream the input stream which will be used to index  此处的Object可以时POJO或stream
  */
 Document getDocument(Map description, Object object) throws Exception;
}

 

Document getDocument(Map description, Object object) throws Exception;

此处设置此方法中Object目的为创建索引文档的几种对象不同而设置:

 

 

在SpringLuence中创建索引文档的方式由三种:

1.根据一个流对象创建一个索引文档

2.根据数据库中的对象创建一个索引文档。

3.根据一个类使用反射创建一个索引文档。

 

 

关于一个流创建索引文档的:

其中在各种文档创建文档中,所有的文档处理类继承自抽象输入类文档处理器:AbstractInputStreamDocumentHandler

 

创建一个抽象的类的AbstractInputStreamDocumentHandler用于处理各种文件类型的的

处理器:AbstractTypeFileDocumentHandler

//用于获取文档的独享

 public final Document doGetDocumentWithInputStream(Map description, InputStream inputStream) throws IOException {


  Document document = new Document();
//获取文档的对象

  String text = extractText(inputStream);
  if( text!=null && text.length()>0 ) {
   //The text is analyzed and indexed but not stored
   document.add(new Field("contents", text, Field.Store.NO, Field.Index.TOKENIZED));
  }
  if( description.get(AbstractInputStreamDocumentHandler.FILENAME)!=null ) {
   document.add(new Field("type", "file", Field.Store.YES, Field.Index.UN_TOKENIZED));
   document.add(new Field("filename", (String)description.get(AbstractInputStreamDocumentHandler.FILENAME), Field.Store.YES, Field.Index.UN_TOKENIZED));
  }
  return document;
 }

 

 

 以下各种处理器用于处理各种类型的文档

其中txt文件类型时Luence 默认的 文件处理类型,可以直接得到文档的内容。

 

(1)关于txt文本文件处理方式如下:

protected Document doGetDocumentWithInputStream(Map description,InputStream inputStream) {
//创建一个索引文档对象  

Document document = new Document();

 //将文本文件的内容放入一个context的文档域中在使用可以根据索引域的名称获取文本文件的内容
  document.add(new Field("contents", new InputStreamReader(inputStream)));


  if( description.get(FILENAME)!=null ) {
  //添加文件类型的索引域

   document.add(new Field("type", "file", Field.Store.YES, Field.Index.UN_TOKENIZED));

   document.add(new Field("filename", (String)description.get(FILENAME), Field.Store.YES,  Field.Index.UN_TOKENIZED));


  }
  return document;
 }

 

有Luence基础可以知道在创建索引文档中索引域时必须采用适当的域对象,不然效率可能很低,o(∩_∩)o...哈哈。

 

(2)关于RTF格式的富文本文档。

RTF不可以直接使用输入流对象必须进行处理。方可以得到RTF中内容信息。

由Java Swing 编程的开发人员一般知道一个简便处理RTF富文本类型的工具:

使用SWing中DefaultStyledDocument,RTFEditorKit类轻易解决问题。

 

处理方法如下:先将rtf文档转换为输入流inputstream,使用SWing工具处理。

 //创建样式文档对象

DefaultStyledDocument styledDoc = new DefaultStyledDocument();
 //RTF编辑工具处理文件的内容

new RTFEditorKit().read(inputStream, styledDoc, 0);

   //获取文档的内容信息
String rtfContext=styledDoc.getText(0, styledDoc.getLength());

 

(3)关于Excel的处理方式比较多:使用开源组件处理如Jxl,POI等处理,即可

这里采用的jxl处理的方式:

使用jxl代码如下:

 //创建一个工作簿对象

Workbook workbook=Workbook.getWorkbook(inputStream);

 //获取并遍历每一个工作单中
   for(int cpt = 0; cpt<workbook.getNumberOfSheets(); cpt++) {
    Sheet sheet = workbook.getSheet(cpt);

         //获取遍历每行中信息

          for(int cptRow = 0; cptRow<sheet.getRows(); cptRow++) {

                //获取并遍历每列中的信息
   for(int cptColumn = 0; cptColumn<sheet.getColumns(); cptColumn++) {

  //获取每列的信息
    Cell cell = sheet.getCell(cptColumn,cptRow);
    String cellText = cell.getContents();
    if( cellText!=null && cellText.length()>0 ) {
     appendText(text, cellText);
    }
   }
  }

(4) PDF文档的处理器设计:

针对PDF的处理器,最好使用PDFBox这个组件,因为这个组件中封装累了关于索引文档对象。如果使用POI,就需要非很
     多时间了。

采用PDFBox的LucenePDFDocument对象即可如下:

Document doc=LucenePDFDocument.getDocument(inputStream);

 

 

(5)WORD的文档的处理方式:使用textmining的组件即可 tx-extractors.jar解析或者使用POI解析都比较简单。

使用 tx-extractors.jar解析如下:

将doc文档设置成流对象从流中读取信息。

 WordExtractor wordDocument = new WordExtractor();
  String text = wordDocument.extractText(inputStream);

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics