如何在Solr中添加文件?
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 Tika 的提取请求处理程序 Apache Solr 从文档中提取内容。
您可以尝试类似下面的代码。当前代码没有使用 springboot 中的任何东西。这里的内容是从 pdf 文档中读取的,然后数据与 id 和文件名一起被索引到 solr 中。我已经使用tika apis提取了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 管理页面上对其进行验证。
请找到图片供您参考。
我使用 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 Tika 的提取请求处理程序 Apache Solr 从文档中提取内容。
您可以尝试类似下面的代码。当前代码没有使用 springboot 中的任何东西。这里的内容是从 pdf 文档中读取的,然后数据与 id 和文件名一起被索引到 solr 中。我已经使用tika apis提取了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 管理页面上对其进行验证。 请找到图片供您参考。