You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-commits@lucene.apache.org by Apache Wiki <wi...@apache.org> on 2010/05/28 21:04:18 UTC
[Solr Wiki] Update of "SolrExampleTests" by HossMan
Dear Wiki user,
You have subscribed to a wiki page or wiki category on "Solr Wiki" for change notification.
The "SolrExampleTests" page has been changed by HossMan.
The comment on this change is: reformating to be legible.
http://wiki.apache.org/solr/SolrExampleTests?action=diff&rev1=5&rev2=6
--------------------------------------------------
- This is the complete implementation of a class which uses [[http://wiki.apache.org/solr/ContentStreamUpdateRequest">ContentStreamUpdateRequest</a>|ContentStreamUpdateRequest.]]
+ This is the complete implementation of a class which uses !ContentStreamUpdateRequest to send data to the ExtractingRequestHandler.
- For more details about the topic, refer to http://wiki.apache.org/solr/ExtractingRequestHandler
+ {{{#!java
+ package javaapplicationsolrcell;
- ||<tablewidth="80%">package javaapplicationsolrcell;<<BR>><<BR>>import java.io.File;<<BR>>import java.io.IOException;<<BR>>import org.apache.solr.client.solrj.SolrServer;<<BR>>import org.apache.solr.client.solrj.SolrServerException;<<BR>><<BR>>import org.apache.solr.client.solrj.request.AbstractUpdateRequest;<<BR>>import org.apache.solr.client.solrj.response.QueryResponse;<<BR>>import org.apache.solr.client.solrj.SolrQuery;<<BR>>import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;<<BR>>import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;<<BR>><<BR>>/**<<BR>> *<<BR>> * @author EDaniel<<BR>> */<<BR>>public class SolrExampleTests{<<BR>><<BR>> /**<<BR>> * @param args the command line arguments<<BR>> */<<BR>> public static void main(String[] args) {<<BR>><<BR>> SolrExampleTests myFiles = new SolrExampleTests();<<BR>><<BR>> try {<<BR>><<BR>> String fileName = "c:/Sample.pdf"; //Solr cell can also index MS file (2003 version and 2007 version) types.<<BR>> String solrId = "Sample.pdf"; //this will be unique Id used by Solr to index the file contents.<<BR>><<BR>> myFiles.IndexFilesSolrCell(fileName, solrId);<<BR>><<BR>> } catch (Exception ex) {<<BR>> System.out.println(ex.toString());<<BR>> }<<BR>><<BR>> }<<BR>><<BR>> /**<<BR>> * Method to index all types of files into Solr. * @param fileName<<BR>> * @param solrId<<BR>> * @throws IOException<<BR>> * @throws SolrServerException<<BR>> */<<BR>> public void IndexFilesSolrCell(String fileName, String solrId) throws IOException, SolrServerException {<<BR>><<BR>> ContentStreamUpdateRequest up = new ContentStreamUpdateRequest("/update/extract");<<BR>><<BR>> up.addFile(new File(fileName));<<BR>><<BR>> up.setParam("literal.id", solrId);<<BR>> up.setParam("uprefix", "attr_");<<BR>> up.setParam("fmap.content", "attr_content");<<BR>><<BR>><<BR>> up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);<<BR>><<BR>> String urlString = "http://localhost:8983/solr"; SolrServer solr = new CommonsHttpSolrServer(urlString);<<BR>><<BR>> solr.request(up);<<BR>><<BR>> QueryResponse rsp = solr.query(new SolrQuery("*:*"));<<BR>><<BR>> System.out.println(rsp);<<BR>> }<<BR>>}<<BR>> ||
+ 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 EDaniel
+ */
+ 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 = "c:/Sample.pdf";
+ //this will be unique Id used by Solr to index the file contents.
+ String solrId = "Sample.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:8983/solr";
+ SolrServer solr = new CommonsHttpSolrServer(urlString);
+
+ ContentStreamUpdateRequest up
+ = new ContentStreamUpdateRequest("/update/extract");
+
+ up.addFile(new File(fileName));
+
+ up.setParam("literal.id", solrId);
+ up.setParam("uprefix", "attr_");
+ 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);
+ }
+ }
+ }}}
+