solrj创建富文本索引

来源:互联网 发布:乌克兰金丝猫事件知乎 编辑:程序博客网 时间:2024/05/01 17:45

        今天主要使用solrj为富文本内容创建索引,参考网上的例子,一直没有调试成功。solrj客户端报的是org.apache.solr.common.SolrException: Internal Server Error错误,没有提示任何信息,因此自己感觉一头雾水,不知从何下手。solr程序后台没有报任何错误。

    后来把dist文件夹下的lib包都拷贝到solr程序中,solr后台终于显示了错误信息。根据错误信息的提示把contrib\extraction下的所有jar报都拷贝到solr程序中,程序终于运行起来了。

   下面是利用solrj创建富文本内容索引的程序。

   

import java.io.File;import java.io.IOException;import org.apache.solr.client.solrj.SolrQuery;import org.apache.solr.client.solrj.SolrServer;import org.apache.solr.client.solrj.SolrServerException;import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;import org.apache.solr.client.solrj.request.AbstractUpdateRequest;import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;import org.apache.solr.client.solrj.response.QueryResponse;public class SolrJDemo2 {  public static void main(String[] args) {    try {      //Solr cell can also index MS file (2003 version and 2007 version) types.      String fileName = "D://lucene3//SQL性能优化.docx";       //this will be unique Id used by Solr to index the file contents.      String solrId = "SQL性能优化";             indexFilesSolrCell(fileName, solrId);          } catch (Exception ex) {      System.out.println(ex.toString());    }  }    /**   * Method to index all types of files into Solr.    * @param fileName   * @param solrId   * @throws IOException   * @throws SolrServerException   */  public static void indexFilesSolrCell(String fileName, String solrId)     throws IOException, SolrServerException {        String urlString = "http://127.0.0.1:8080/solr";     SolrServer solr = new CommonsHttpSolrServer(urlString);        ContentStreamUpdateRequest up       = new ContentStreamUpdateRequest("/update/extract");        up.addFile(new File(fileName));        up.setParam("literal.id", solrId);    up.setParam("fmap.content", "attr_content");        up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);        solr.request(up);        QueryResponse rsp = solr.query(new SolrQuery("*:*"));        System.out.println(rsp);  }}

 

      up.setParam("literal.id", solrId);   //设置document的id域值为solrid
      up.setParam("fmap.content", "msg_content");//设置存储文件内容的域为msg_content,我们必须在schema.xml中定义msg_content字段。

      
 

0 0
原创粉丝点击