solr直接對pdf、word等建索引

1.準備solr6.6環境,這裏我搭建了3臺solr服務的環境
2.配置managed-schema,添加幾個測試字段
  <field name="literal.path" type="string" multiValued="false" indexed="true" stored="true"/>
  <field name="literal.pathuploaddate" type="string" multiValued="false" indexed="true" stored="true"/>
  <field name="literal.pathftype" type="string" multiValued="false" indexed="true" stored="true"/>
  <field name="fmap.content" type="string" multiValued="false" indexed="true" stored="true"/>
 
3.添加依賴包
在/opt/solr-6.6.2/server/solr-webapp/webapp/WEB-INF/lib下上傳以下兩個包:
mmseg4j-core-1.10.0.jar
mmseg4j-solr-2.3.1.jar
 
 
4.測試接口以下:
package solrpdf;
 
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.request.AbstractUpdateRequest.ACTION;
import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;
 
public class SolrPdf {
  public static void main(String[] args)
     {
         String fileName = "E://2019-08-09//bj_fzc_hd_fzk_qh__liyanpo__5796104_Doc5501.pdf";
         String solrId = "bj_fzc_hd_fzk_qh__liyanpo__5796104_Doc5501.pdf";
// String path = "";
         try
         {
// indexFilesSolrCell(solrId, solrId,fileName);
    indexFilesSolrCell(fileName, solrId, fileName);
         }
         catch (IOException e)
         {
             e.printStackTrace();
         }
         catch (SolrServerException e)
         {
             e.printStackTrace();
         }
         }
         private static String GetCurrentDate(){
             Date dt = new Date();
             //最後的aa表示「上午」或「下午」 HH表示24小時制 若是換成hh表示12小時制
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss aa");
             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
             String day =sdf.format(dt);
             return day;
         }
         public static void indexFilesSolrCell(String fileName, String solrId, String path)
                 throws IOException, SolrServerException
         {
             String urlString = "http://192.168.0.231:8983/solr/TESTPDF";
             SolrClient solr = new HttpSolrClient.Builder(urlString).build();
             ContentStreamUpdateRequest up = new ContentStreamUpdateRequest("/update/extract");
             String contentType = getFileContentType(fileName);
             up.addFile(new File(path), contentType);
             String fileType = fileName.substring(fileName.lastIndexOf(".")+1);
             up.setParam("literal.id", fileName);
             up.setParam("literal.path", path);//文件路徑
             up.setParam("literal.pathuploaddate", GetCurrentDate());//文件上傳時間
             up.setParam("literal.pathftype", fileType);//文件類型,doc,pdf
             up.setParam("fmap.content", "attr_content");//文件內容
             up.setAction(ACTION.COMMIT, true, true);
             solr.request(up);
         }
 
         /**
         * @Author:sks
         * @Description:根據文件名獲取文件的ContentType類型
         * @Date: 
         */
         public static String getFileContentType(String filename) {
             String contentType = "";
             String prefix = filename.substring(filename.lastIndexOf(".") + 1);
             if (prefix.equals("xlsx")) {
                 contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
             } else if (prefix.equals("pdf")) {
                 contentType = "application/pdf";
             } else if (prefix.equals("doc")) {
                 contentType = "application/msword";
             } else if (prefix.equals("txt")) {
                 contentType = "text/plain";
             } else if (prefix.equals("xls")) {
                 contentType = "application/vnd.ms-excel";
             } else if (prefix.equals("docx")) {
                 contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
             } else if (prefix.equals("ppt")) {
                 contentType = "application/vnd.ms-powerpoint";
             } else if (prefix.equals("pptx")) {
                 contentType = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
             }
             else {
                 contentType = "othertype";
             }
             return contentType;
         }
}
測試結果:
索引後的全文內容:"attr_content":" \n \n date 2018-12-26T01:02:31Z \n pdf:PDFVersion 1.4 \n stream_content_type application/pdf \n access_permission:modify_annotations true \n access_permission:can_print_degraded true \n dcterms:created 2018-12-26T01:02:31Z \n Last-Modified 2018-12-26T01:02:31Z \n dcterms:modified 2018-12-26T01:02:31Z \n dc:format application/pdf; version=1.4 \n Last-Save-Date 2018-12-26T01:02:31Z \n access_permission:fill_in_form true \n pdf:docinfo:modified 2018-12-26T01:02:31Z \n meta:save-date 2018-12-26T01:02:31Z \n pdf:encrypted false \n modified 2018-12-26T01:02:31Z \n Content-Type application/pdf \n stream_size 4354 \n X-Parsed-By org.apache.tika.parser.DefaultParser \n X-Parsed-By org.apache.tika.parser.pdf.PDFParser \n meta:creation-date 2018-12-26T01:02:31Z \n created Wed Dec 26 09:02:31 GMT+08:00 2018 \n access_permission:extract_for_accessibility true \n access_permission:assemble_document true \n xmpTPg:NPages 2 \n Creation-Date 2018-12-26T01:02:31Z \n access_permission:extract_content true \n access_permission:can_print true \n producer iText1.3.1 by lowagie.com (based on itext-paulo-154) \n access_permission:can_modify true \n pdf:docinfo:producer iText1.3.1 by lowagie.com (based on itext-paulo-154) \n pdf:docinfo:created 2018-12-26T01:02:31Z \n \n \n page \n 北京市工商行政管理局海淀分局\n \n 行 政 處 罰 決 定 書\n京工商海處字〔2018〕第2331號\n \n 當事人:小米科技有限責任公司\n \n 住所:北京市海淀區清河中街68號華潤五彩城購物中心二期13層\n \n 統一社會信用代碼:91110108551385082Q\n \n 法定表明人:雷軍\n \n 經查:米家有品app爲當事人全部,當事人於2017年12月19日至12\n \n 月26日在米家有品自營店鋪所進行的「有品感恩季活動」中,對其\n \n 銷售的小米MIX2手機宣傳:「領券減100 128GB版送小米AI音箱」,\n \n 但消費者購買小米MIX2 128GB版手機領券後沒有減100元,也沒有送\n \n 小米AI音箱。當事人稱,消費者購買時若是選擇的限時購,就不會\n \n 享受領券減100元及送小米AI音箱,由於限時購與其它優惠政策不疊\n \n 加,但當事人未在產品頁面中標明。上述違法事實有當事人的營業\n \n 執照複印件、狀況說明、經當事人確認的相關網頁截圖打印件、舉\n \n 報登記單、詢問(調查)筆錄等材料在案佐證。\n \n 我局於2018年11月27日向當事人送達了《行政處罰聽證告知書》\n \n ,告知當事人我局決定做出行政處罰決定的事實、理由、依據、內\n \n 容以及當事人依法享有的陳述權、申辯權。當事人在法按期限內未\n \n 提出陳述、申辯意見,也未提出聽證。\n \n 當事人的上述行爲,違反了《中華人民共和國廣告法》第八條第\n \n 一款,構成廣告中對商品的性能、功能、產地、用途、質量、成分\n \n 、價格、生產者、有效期限、允諾等有表示的,未準確、清楚、明\n \n 白表示的違法行爲。依據《中華人民共和國廣告法》第五十九條第\n \n 一款(一)項的規定,責令當事人中止發佈上述違法廣告,並決\n \n 第 1 頁 \n \n \n page \n 定處罰以下:罰款30000元。\n \n 當事人應當自收到本處罰決定書之日起15日內到就近銀行(非稅收\n \n 入代理收繳銀行:北京銀行、建設銀行、交通銀行、光大銀行、中\n \n 信銀行)繳納罰款。逾期不繳納,每日按罰款數額的3%加處罰款。\n \n 如不服本處罰決定,可於接到本處罰決定書之日起60日內向北京市\n \n 工商行政管理局或海淀區人民政府申請複議。也可於6個月內直接向\n \n 海淀區人民法院起訴。\n \n 北京市工商行政管理局海淀分局\n \n 二〇一八年十二月二十日\n \n 第 2 頁 \n \n \n ", "_version_":1641378670909063168}] }}
參考連接:https://www.cnblogs.com/shaosks/p/8033362.html
相關文章
相關標籤/搜索