Tika 是2008年才产生的apache的一个项目,主要用于打开各种不同类型的文档,获取其文本信息。可以解析多种类型(word、pdf、txt 、html等)文件! 甚至可以通过解析url,获取其网页信息。最后把其文本信息提起出来。这方面Tika有点像Jsoup。。一般情况下,直接对word、pdf等文件直接创建索引是不对的,用luke工具查看之后,出现一大推乱七八糟的term。这个时候就可以用Tika 去在对其创建索引之前,转化处理其文本信息。
package hhc;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.apache.tika.Tika;
import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.sax.BodyContentHandler;
import org.xml.sax.ContentHandler;
import com.chenlb.mmseg4j.analysis.MMSegAnalyzer;
public class IndexUtil {
public void index(boolean hasNew) throws IOException{
File f=new File("E:\\lucene\\learn\\example_tika");
Directory directory = FSDirectory.open(new File("E:\\lucene\\learn\\index_tika"));
IndexWriter writer=new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new MMSegAnalyzer()));
if(hasNew){
writer.deleteAll();
}
for(File file:f.listFiles()){
Document d=new Document();
d.add(new Field("content",tikaParseFileToString(file),Field.Store.YES,Field.Index.ANALYZED));
d.add(new Field("filename",file.getName(),Field.Store.YES,Field.Index.NOT_ANALYZED));
d.add(new Field("path",file.getAbsolutePath(),Field.Store.YES,Field.Index.NOT_ANALYZED));
d.add(new Field("size",String.valueOf(file.length()),Field.Store.YES,Field.Index.NOT_ANALYZED));
writer.addDocument(d);
}
writer.close();
}
/**
* 直接创建tika对象,但是效率不高
* @param file
* @return
* @throws IOException
* @throws TikaException
*/
public static String tikaAutoString(File file) throws IOException, TikaException{
Tika tika=new Tika();
//tika.parse(stream, metadata); 设置概要
return tika.parseToString(file);
}
/**
* 这种方式的效率比较高
* @param file
* @return
*/
public static String tikaParseFileToString(File file) {
// 自动获取最适合的解析器
Parser parser = new AutoDetectParser();
InputStream stream = null;
try {
stream = new FileInputStream(file);
// 所有解析出来的内容都会放入这个里面handler
ContentHandler handler = new BodyContentHandler();
//加载解析器
ParseContext context = new ParseContext();
context.set(Parser.class, parser);
//获取概要数据
Metadata data=new Metadata();
parser.parse(stream, handler, data, context);
/**
* 可以设置概要数据的属性
*/
data.set(data.AUTHOR, "胡慧超");
data.set(data.RESOURCE_NAME_KEY, file.getName());
System.out.println(data.toString());
for(String name:data.names()){
System.out.println(name);
}
return handler.toString();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
if(stream!=null)
try {
stream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "";
}
public static void main(String[] args) {
tikaParseFileToString(new File("E:\\lucene\\learn\\example_tika\\AILK_OFFER_胡慧超_2014-06-26.pdf"));
}
}
Luke 这个工具很强大,luke是一个查询索引的工具,使用时必须注意版本要与lucene版本一致,否则可能打不开索引信息。选择索引所在的目录,就可以查询和操作相应的索引信息,并且在searche中根据|Queryparser来查询对应的信息。同时可以管理索引信息。
以下是luke4.10.2版本截图:
以下是使用Tika的两种方式