[英]How to add file in Solr?
我使用Apache Solr,以便可以處理文件,可以通過Spring添加常規文本字段,但是我不知道如何添加TXT / pdf
@SolrDocument(solrCoreName = "accounting")
public class Accounting {
@Id
@Field
private String id;
@Field
private File txtFile;
@Field
private String docType;
@Field
private String docTitle;
public Accounting() {
}
public Accounting(String id, String docType, String docTitle) {
this.id = id;
this.docTitle = docTitle;
this.docType = docType;
}
這是txtFile字段的問題
<field name="docTitle" type="strings"/>
<field name="docType" type="strings"/>
這些字段是我手動添加到schema.xml的,我不知道如何在此處添加將負責該文件的字段,例如,我將在此處添加txt文件,該怎么做? 非常感謝你。 我是否正確聲明了字段private File txtFile;
在文件的實體中?
Solr不會在任何地方存儲實際文件。 但是,根據您的配置,它可以存儲二進制內容。 使用提取請求處理程序Apache Solr(依賴於Apache Tika)從文檔中提取內容。
您可以嘗試以下代碼。 當前代碼未使用springboot中的任何內容。 在這里,內容是從pdf文檔中讀取的,然后將數據與id和文件名一起索引到solr中。 我已使用tika api提取pdf的內容。
public static void main(final String[] args) throws IOException, TikaException, SAXException {
String urlString = "http://localhost:8983/solr/TestCore1";
SolrClient solr = new HttpSolrClient.Builder(urlString).build();
BodyContentHandler handler = new BodyContentHandler();
Metadata metadata = new Metadata();
File file = new File("C://Users//abhijitb//Desktop//TestDocument.pdf");
FileInputStream inputstream = new FileInputStream(file);
ParseContext pcontext = new ParseContext();
// parsing the document using PDF parser
PDFParser pdfparser = new PDFParser();
pdfparser.parse(inputstream, handler, metadata, pcontext);
// getting the content of the document
//System.out.println("Contents of the PDF :" + handler.toString());
try {
String fileName = file.getName();
SolrInputDocument document = new SolrInputDocument();
document.addField("id", "123456");
document.addField("title", fileName);
document.addField("text", handler.toString());
solr.add(document);
solr.commit();
} catch (SolrServerException | IOException e) {
e.printStackTrace();
}
}
索引數據后,可以在solr admin頁面上通過查詢對其進行驗證。 請找到圖片以供參考。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.