云深处 发表于 2016-12-16 11:18:06

solr中为pdf word excel等文档做索引和存诸时如何把回车空格等去掉

  当用solr的"/update/extract"方法为pdf 或 word 或excel 等文件做索引时,当高亮时总会出现很多回车或空格占了很多空间,让搜索结果时长时短很不好看,而配设中又没有配置可以完成这项目功能,唯一可以实现的就是改源码!
  我用的是solr4.7.2的源码去改,源码在以下网址找到
  http://grepcode.com/snapshot/repo1.maven.org/maven2/org.apache.solr/solr-cell/4.7.2/
  改的文件是
  http://grepcode.com/file/repo1.maven.org/maven2/org.apache.solr/solr-cell/4.7.2/org/apache/solr/handler/extraction/SolrContentHandler.java
  改的地方是:
  改动如下:
  添加 private char LastChar=7;
  public void startElement 去掉 bldrStack.getLast().append(' ');
  public void endElement 去掉 bldrStack.getLast().append(' ');
  public void characters(char[] chars, int offset, int length) throws SAXException
  改为如下:
  public void characters(char[] chars, int offset, int length) throws SAXException {
  if(chars.length>0)
  {
  char[] TmpChars=new char;
  int TmpCharsCount=0;
  for(int i=offset;i<chars.length;i++)
  {
  if(chars=='\n' || chars=='\r' || chars=='\t' || chars==' ' || chars==' ')
  {
  if(LastChar!=' ')
  {
  TmpChars=' ';
  TmpCharsCount++;
  } 
  LastChar=' ';
  }
  else
  {
  TmpChars=chars;
  TmpCharsCount++;
  LastChar=chars;
  }
  }
  if(TmpCharsCount>0)
  {
  bldrStack.getLast().append(TmpChars, 0, TmpCharsCount);
  }
  }
  }
  然后解压solr-cell-4.7.2.jar这个文件,替换里面的SolrContentHandler.class文件,再重新打包成zip文件再把后辍改成jar,这样替换掉原来的solr-cell-4.7.2.jar就OK了!
页: [1]
查看完整版本: solr中为pdf word excel等文档做索引和存诸时如何把回车空格等去掉