• Apache Solr索引富文本(html word pdf)


    lucene对索引的更新比solr麻烦,solr只需要调用一个函数UpdateRequest.setAction(AbstractUpdateRequest.ACTION.COMMIT, false, false)就完成了更新,而lucene需要先删除再更新,否则就变成增量索引了

    lucene更新索引:http://langhua9527.iteye.com/blog/582347

    前面已经简单介绍了solr的安装与使用,下面来看看如何用客户端solrj来建立索引及查询

    1. import java.io.IOException;  
    2. import java.util.ArrayList;  
    3. import java.util.Collection;  
    4.   
    5. import org.apache.solr.client.solrj.SolrQuery;  
    6. import org.apache.solr.client.solrj.SolrServer;  
    7. import org.apache.solr.client.solrj.SolrServerException;  
    8. import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;  
    9. import org.apache.solr.client.solrj.request.AbstractUpdateRequest;  
    10. import org.apache.solr.client.solrj.request.UpdateRequest;  
    11. import org.apache.solr.client.solrj.response.QueryResponse;  
    12. import org.apache.solr.common.SolrInputDocument;  
    13.   
    14. public class SolrjTest {  
    15.   
    16.     public static void main(String[] args) throws IOException,  
    17.             SolrServerException {  
    18.   
    19.         String urlString = " http://localhost:8080/solr";  
    20.         SolrServer server = new CommonsHttpSolrServer(urlString);  
    21.   
    22.         SolrInputDocument doc1 = new SolrInputDocument();  
    23.         doc1.addField("id"12);  
    24.         doc1.addField("content""my test is easy,测试solr");  
    25.         SolrInputDocument doc2 = new SolrInputDocument();  
    26.         doc2.addField("id""solrj简单测试");  
    27.         doc2.addField("content""doc2");  
    28.         Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();  
    29.         docs.add(doc1);  
    30.         docs.add( doc2 );  
    31.         server.add(docs);  
    32.         UpdateRequest req = new UpdateRequest();  
    33.         req.setAction(AbstractUpdateRequest.ACTION.COMMIT, falsefalse);  
    34.         req.add(docs);  
    35.         req.process(server);  
    36.   
    37.         SolrQuery query = new SolrQuery();  
    38.   
    39.         query.setQuery("test");  
    40.         query.setHighlight(true).setHighlightSnippets(1);                                                     
    41.         query.setParam("hl.fl""content");  
    42.   
    43.         QueryResponse ret = server.query(query);  
    44.   
    45.         System.out.println(ret);  
    46.     }  
    47. }  

    solrj要成功运行,需要导入下列包才行

    From /dist:

    apache-solr-solrj-3.1.0.jar

    From /dist/solrj-lib: 
    commons-codec-1.4.jar 
    commons-httpclient-3.1.jar 
    jcl-over-slf4j-1.5.5.jar 
    slf4j-api-1.5.5.jar

    下面这个包需要去官方下载,因为本人在solr3.1中是没发现这个jar包的,估计是在低版本中有
    slf4j-jdk14-1.5.5.jar

    solr从1.4版本开始,将apache Tika合并进来,Tika是一个内容抽取的工具集合(a toolkit for text extracting)。它集成了POI, Pdfbox 并且为文本抽取工作提供了一个统一的界面。solr中利用这个工具可以很简单实现对pdf、word等富文本的提取

    我的是3.1版,在实现过程中,走了很多弯路,终于还是自己解决了,下面分享一下

    1. package test;  
    2.   
    3. import java.io.File;  
    4. import java.io.IOException;  
    5. import org.apache.solr.client.solrj.SolrServer;  
    6. import org.apache.solr.client.solrj.SolrServerException;  
    7.   
    8. import org.apache.solr.client.solrj.request.AbstractUpdateRequest;  
    9. import org.apache.solr.client.solrj.response.QueryResponse;  
    10. import org.apache.solr.client.solrj.SolrQuery;  
    11. import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;  
    12. import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;  
    13.   
    14. /** 
    15.  * @author aidy 2011.6.9 
    16.  */  
    17. public class SolrExampleTests {  
    18.   
    19.   public static void main(String[] args) {  
    20.     try {  
    21.       //Solr cell can also index MS file (2003 version and 2007 version) types.  
    22.       String fileName = "D://test//luceneTest//1.pdf";   
    23.       //this will be unique Id used by Solr to index the file contents.  
    24.       String solrId = "1.pdf";   
    25.         
    26.       indexFilesSolrCell(fileName, solrId);  
    27.         
    28.     } catch (Exception ex) {  
    29.       System.out.println(ex.toString());  
    30.     }  
    31.   }  
    32.     
    33.   /** 
    34.    * Method to index all types of files into Solr.  
    35.    * @param fileName 
    36.    * @param solrId 
    37.    * @throws IOException 
    38.    * @throws SolrServerException 
    39.    */  
    40.   public static void indexFilesSolrCell(String fileName, String solrId)   
    41.     throws IOException, SolrServerException {  
    42.       
    43.     String urlString = "http://localhost:8080/solr";   
    44.     SolrServer solr = new CommonsHttpSolrServer(urlString);  
    45.       
    46.     ContentStreamUpdateRequest up   
    47.       = new ContentStreamUpdateRequest("/update/extract");  
    48.       
    49.     up.addFile(new File(fileName));  
    50.       
    51.     up.setParam("literal.id", solrId);  
    52.     up.setParam("fmap.content""attr_content");  
    53.       
    54.     up.setAction(AbstractUpdateRequest.ACTION.COMMIT, truetrue);  
    55.       
    56.     solr.request(up);  
    57.       
    58.     QueryResponse rsp = solr.query(new SolrQuery("*:*"));  
    59.       
    60.     System.out.println(rsp);  
    61.   }  
    62. }  

    刚开始一直在solr.request(up)这一步报错,看tomcat报错是说没有ignored_meta类型,刚开始一直不理解,因为我的配置文件schema.xml中根本没有这种类型,刚开始还以为是版本原因导致,专门去下了solr1.4版,运行果然不报错,后来才想到是因为前面在入门例子中,我修改了配置文件schema.xml,而solrconfig.xml配置文件在/update/extract节点处,有ignored_类型引用,后来我在schema.xml加入ignored_类型后,运行正常

    后面研究一下如何用solrj进行查询,并将查询结果展示在web页面上,因为查询结果返回的是xml形式

    如果solr是1.3版本或以下,请参考:http://wiki.apache.org/solr/UpdateRichDocuments

    参考资料:

    1.http://wiki.apache.org/solr/ExtractingRequestHandler
    2.http://www.lucidimagination.com/Community/Hear-from-the-Experts/Articles/Content-Extraction-Tika

  • 相关阅读:
    基于屏幕的可用区域
    Legacy Browser/Windows/Office Support
    getBoundingClientRect 和 getClientRect
    基础健康知识——12.自限性疾病
    基础健康知识——11.弊病
    基础健康知识系列
    基础健康知识——常见疾病:腹泻
    基础健康知识——常见疾病:感冒
    基础健康知识——10.就医
    基础健康知识——9.反复感染
  • 原文地址:https://www.cnblogs.com/ibook360/p/2231477.html
Copyright © 2020-2023  润新知