Spring boot 如何在Solr中添加文件?
我使用ApacheSolr以便处理文件,我可以通过Spring添加常规文本字段,但我不知道如何添加TXT/pdfSpring boot 如何在Solr中添加文件?,spring-boot,solr,Spring Boot,Solr,我使用ApacheSolr以便处理文件,我可以通过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; p
@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不会将实际文件存储在任何位置。根据您的配置,它可以存储二进制内容。使用提取请求处理程序ApacheSolr,它依赖ApacheTika从文档中提取内容
您可以尝试下面的代码。当前代码未使用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页面上通过查询数据对其进行验证。
请找到图片以供参考
我怀疑您是否可以直接添加该文件。这是通过提取文件的内容,然后只将内容推送到一个字段来完成的。如果它是pdf呢?可以是pdf、txt、word、csv……如果您正在创建solrDocument对象……您必须将文档的内容传递到字段……solr不会将文件存储在其末尾……它可以将文件路径存储在其末尾(如果您索引文件路径)…您可以使用Tika API提取pdf/文本文件的上下文…我是否需要读取文档的内容,然后保存它?或者是否可以传输文档,他是否已经读取了内容?