知识点
相关文章
更多最近更新
更多Apache Solr索引富文本(html word pdf)
2019-03-27 01:10|来源: 网路
lucene对索引的更新比solr麻烦,solr只需要调用一个函数UpdateRequest.setAction(AbstractUpdateRequest.ACTION.COMMIT, false, false)就完成了更新,而lucene需要先删除再更新,否则就变成增量索引了
lucene更新索引:http://langhua9527.iteye.com/blog/582347
前面已经简单介绍了solr的安装与使用,下面来看看如何用客户端solrj来建立索引及查询
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Collection;
- 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.UpdateRequest;
- import org.apache.solr.client.solrj.response.QueryResponse;
- import org.apache.solr.common.SolrInputDocument;
- public class SolrjTest {
- public static void main(String[] args) throws IOException,
- SolrServerException {
- String urlString = " http://localhost:8080/solr";
- SolrServer server = new CommonsHttpSolrServer(urlString);
- SolrInputDocument doc1 = new SolrInputDocument();
- doc1.addField("id", 12);
- doc1.addField("content", "my test is easy,测试solr");
- SolrInputDocument doc2 = new SolrInputDocument();
- doc2.addField("id", "solrj简单测试");
- doc2.addField("content", "doc2");
- Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
- docs.add(doc1);
- docs.add( doc2 );
- server.add(docs);
- UpdateRequest req = new UpdateRequest();
- req.setAction(AbstractUpdateRequest.ACTION.COMMIT, false, false);
- req.add(docs);
- req.process(server);
- SolrQuery query = new SolrQuery();
- query.setQuery("test");
- query.setHighlight(true).setHighlightSnippets(1);
- query.setParam("hl.fl", "content");
- QueryResponse ret = server.query(query);
- System.out.println(ret);
- }
- }
solrj要成功运行,需要导入下列包才行
From /dist:
apache-solr-solrj-3.1.0.jar
From /dist/solrj-lib:
commons-codec-1.4.jar
commons-httpclient-3.1.jar
jcl-over-slf4j-1.5.5.jar
slf4j-api-1.5.5.jar
下面这个包需要去官方下载,因为本人在solr3.1中是没发现这个jar包的,估计是在低版本中有
slf4j-jdk14-1.5.5.jar
solr从1.4版本开始,将apache Tika合并进来,Tika是一个内容抽取的工具集合(a toolkit for text extracting)。它集成了POI, Pdfbox 并且为文本抽取工作提供了一个统一的界面。solr中利用这个工具可以很简单实现对pdf、word等富文本的提取
我的是3.1版,在实现过程中,走了很多弯路,终于还是自己解决了,下面分享一下
- package test;
- import java.io.File;
- import java.io.IOException;
- import org.apache.solr.client.solrj.SolrServer;
- import org.apache.solr.client.solrj.SolrServerException;
- import org.apache.solr.client.solrj.request.AbstractUpdateRequest;
- import org.apache.solr.client.solrj.response.QueryResponse;
- import org.apache.solr.client.solrj.SolrQuery;
- import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
- import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;
- /**
- * @author aidy 2011.6.9
- */
- public class SolrExampleTests {
- public static void main(String[] args) {
- try {
- //Solr cell can also index MS file (2003 version and 2007 version) types.
- String fileName = "D://test//luceneTest//1.pdf";
- //this will be unique Id used by Solr to index the file contents.
- String solrId = "1.pdf";
- 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://localhost: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);
- }
- }
刚开始一直在solr.request(up)这一步报错,看tomcat报错是说没有ignored_meta类型,刚开始一直不理解,因为我的配置文件schema.xml中根本没有这种类型,刚开始还以为是版本原因导致,专门去下了solr1.4版,运行果然不报错,后来才想到是因为前面在入门例子中,我修改了配置文件schema.xml,而solrconfig.xml配置文件在/update/extract节点处,有ignored_类型引用,后来我在schema.xml加入ignored_类型后,运行正常
后面研究一下如何用solrj进行查询,并将查询结果展示在web页面上,因为查询结果返回的是xml形式
如果solr是1.3版本或以下,请参考:http://wiki.apache.org/solr/UpdateRichDocuments
参考资料:
1.http://wiki.apache.org/solr/ExtractingRequestHandler
2.http://www.lucidimagination.com/Community/Hear-from-the-Experts/Articles/Content-Extraction-Tika
转自:http://www.cnblogs.com/ibook360/archive/2011/11/01/2231477
相关问答
更多-
java使用openoffice将word转换为pdf的问题[2022-08-19]
推荐下面免费的方法转成PDF试试: 方法一:使用虚拟打印机pdf factory即可,而且其他格式文件只要是能够打印,选择这个虚拟打印机,都可以做成PDF文件,很简单实用; 方法二:使用专门的转换软件,把文件转成PDF文件; 方法三:用其他虚拟打印机转成PDF文件。 方法四:用WPS本身自带的转换功能转成PDF文件。 -
我会建议使用SolrNet客户端。 它支持ExtractingRequestHandler。 I would recommend using the SolrNet client. It supports the ExtractingRequestHandler.
-
实际上,Tika通过在页面开始前发送元素处理页面(至少以pdf格式)。 你可以很容易地在你的处理程序中使用这个设置页面数量(只用
和页面结束后
来计算页面数量): public abstract class MyContentHandler implements ContentHandler { private String pageTag = "p"; protected int pageNumber = 0; ... @Overrid ...
-
是的,你认为POI会这样做是错误的。 Apache POI适用于Microsoft Office文件格式,而PDF则不适用。 您要么直接使用Apache PDFBox ,要么使用Apache Tika ,它将同时使用Microsoft Office和PDF文件格式(以及许多其他格式)。 Yes, you are wrong in believing that POI will do that. Apache POI works with Microsoft Office file formats, whic ...
-
看看错误似乎apache-solr-cell jar及其依赖关系在Solr库的提取lib中缺失。
添加这些文件...... looking at the error it seems apache-solr-cell jar and its dependencies are missing in th ... -
Zend PDF包装(Zend PDF word wrapping)[2021-10-19]
确实,Zend_Pdf还不够成熟,无法与常规的Zend_YouNameIt组件进行比较。 在我看来,Zend_Pdf不应该成为Zend的一部分。 这是非常有限的,不能在大多数情况下使用,因为缺少一个或另一个功能。 至于你缺少的功能,你必须检查手册或阅读代码,但一般来说,我建议使用其他的东西来生成PDF,直到Zend_Pdf足够成熟。 我认为最好的解决方案是将Zend_Pdf作为某种命令行工具的包装。 Indeed Zend_Pdf is not mature enough to compare with t ... -
一年前我就遇到过类似的问题。 我最终决定使用OpenOffice.org我不是积极的OpenOffice.org将满足您的确切需求,但我建议您查看它。 I worked on a similar problem a year ago. I finally settled with using OpenOffice.org I'm not positive OpenOffice.org will meet your exact needs here, but I would suggest looking a ...
-
如果默认情况下正确索引,则pdf内容会在模式中正确声明时被索引到字段名称content中。 所以使用该内容字段搜索一些关键字(或*)。 例如: q=content:keyword (关键字 - >以pdf格式显示) http://localhost:8983/solr/gettingstarted/select?q=content:* 如果contetnt字段未定义。 然后在模式文件中添加字段定义。 例如:字段名称声明你可以使用这个,这段代码正在为我工作, HttpURLConnection connection = null; HttpURLConnection.setFollowRedirects(false); connection.setRequestMethod("POST"); String boundary = "---------------------------boundary"; Stri ...
索引PDF - 使用Apache Solr和Apache Tika进行分面搜索(Indexing PDF - Faceted Search with Apache Solr and Apache Tika)[2023-04-14]
Solr 6.2附带了示例/文件中的文件示例,这些示例/文件专门用于索引和浏览丰富内容文件(如PDF)。 首先使用它并尝试了解它是如何组合在一起的。 Solr 6.2 ships with files example in the example/files that is configured specifically to index and browse rich-content files (like PDF). Start by using that and try to understand h ...