solr学习三(测试类,含普通与ExtractingRequestHandler测试)
Posted cxhfuujust
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了solr学习三(测试类,含普通与ExtractingRequestHandler测试)相关的知识,希望对你有一定的参考价值。
solr客户端基本是配置出来的,服务端可以对其进行测试,我使用的是solrj服务端。
如果初学solr,先使用普通的测试类:
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Date;
- 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 SolrTest {
- public static void main(String[] args) throws IOException,
- SolrServerException {
- String urlString = " http://localhost:8393/keyPlace";
- SolrServer server = new CommonsHttpSolrServer(urlString);
- testAdd(server);
- testQuery(server);
- }
- static void testAdd(SolrServer server) throws IOException,
- SolrServerException {
- SolrInputDocument doc1 = new SolrInputDocument();
- doc1.addField("id", 456);
- doc1.addField("orgId", "33030300310");
- doc1.addField("name", "张三");
- doc1.addField("key", "1");
- doc1.addField("createDate", new Date());
- SolrInputDocument doc2 = new SolrInputDocument();
- doc2.addField("id", 123);
- doc2.addField("orgId", "33030300310");
- doc2.addField("name", "李四");
- doc2.addField("key", "2");
- doc2.addField("createDate", new Date());
- 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);
- }
- static void testQuery(SolrServer server) throws IOException,
- SolrServerException {
- SolrQuery query = new SolrQuery();
- query.setQuery("name:张三");
- query.setHighlight(true).setHighlightSnippets(1);
- QueryResponse ret = server.query(query);
- System.out.println(ret);
- }
如果服务端配置了ExtractingRequestHandler,可使用下面的类进行测试:
- 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;
- import org.apache.solr.common.params.ModifiableSolrParams;
- import org.apache.solr.handler.extraction.ExtractingParams;
- public class ExtractTest {
- public static void main(String[] args) {
- try {
- String urlString = "http://localhost:8393/sourcePool/";
- SolrServer solr = new CommonsHttpSolrServer(urlString);
- String fileName = "c:/slor1.doc";
- String solrId = "2";
- String resoucePoolid = "2";
- indexFilesSolrCell(fileName, solrId , resoucePoolid , solr);
- testQuery(solr);
- } catch (Exception ex) {
- System.out.println(ex.toString());
- }
- }
- /**
- * Method to index all types of files into Solr.
- * @throws IOException
- * @throws SolrServerException
- */
- public static void indexFilesSolrCell(String fileName, String solrId , String resoucePoolid , SolrServer solr)
- throws IOException, SolrServerException {
- ContentStreamUpdateRequest up
- = new ContentStreamUpdateRequest("/update/extract");
- ModifiableSolrParams p = new ModifiableSolrParams();
- p.add(ExtractingParams.LITERALS_PREFIX + "orgids" , "33010033001");
- p.add(ExtractingParams.LITERALS_PREFIX + "orgids" , "33010033002");
- p.add(ExtractingParams.LITERALS_PREFIX + "orgids" , "33010033003");
- up.setParams(p);
- up.addFile(new File(fileName));
- up.setParam(ExtractingParams.LITERALS_PREFIX + "id", solrId);
- up.setParam(ExtractingParams.LITERALS_PREFIX + "resoucepoolid", resoucePoolid);
- up.setParam(ExtractingParams.LITERALS_PREFIX + "orgid", "33010033001");
- up.setParam(ExtractingParams.LITERALS_PREFIX + "name", "33010033001");
- up.setParam(ExtractingParams.LITERALS_PREFIX + "releaseunit", "33010033001");
- up.setParam(ExtractingParams.LITERALS_PREFIX + "releasetime", "2011-02-12");
- up.setParam(ExtractingParams.UNKNOWN_FIELD_PREFIX, "attr_");
- //up.setParam("fmap.content", "filestream");
- up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
- solr.request(up);
- }
- static void testQuery(SolrServer solr) throws IOException, SolrServerException {
- String fileStream = "filestream:老婆的老公";
- String field = "orgid:33010033001";
- QueryResponse rsp = solr.query(new SolrQuery(field));
- System.out.println(rsp);
- }
- }
以上是关于solr学习三(测试类,含普通与ExtractingRequestHandler测试)的主要内容,如果未能解决你的问题,请参考以下文章