/tags/0.0.1-incubating-20090901/droids-solr/src/main/java/org/apache/droids/solr/SolrHandler.java

# · Java · 56 lines · 33 code · 7 blank · 16 comment · 0 complexity · e5e09384a0b727bb8e14ea08af04f034 MD5 · raw file

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.droids.solr;
  18. import java.io.IOException;
  19. import java.net.URI;
  20. import org.apache.droids.api.ContentEntity;
  21. import org.apache.droids.api.Handler;
  22. import org.apache.droids.exception.DroidsException;
  23. import org.apache.solr.client.solrj.SolrServer;
  24. import org.apache.solr.client.solrj.SolrServerException;
  25. import org.apache.solr.common.SolrInputDocument;
  26. public class SolrHandler implements Handler {
  27. private SolrServer solr;
  28. public void handle(URI uri, ContentEntity entity)
  29. throws IOException, DroidsException
  30. {
  31. SolrInputDocument doc = createSolrInputDocument(uri, entity);
  32. try {
  33. solr.add( doc );
  34. }
  35. catch (SolrServerException e) {
  36. throw new DroidsException( e );
  37. }
  38. }
  39. public SolrInputDocument createSolrInputDocument(URI url, ContentEntity entity)
  40. {
  41. SolrInputDocument doc = new SolrInputDocument();
  42. doc.setField( "id", url.getPath() );
  43. doc.setField( "name", url.toASCIIString() );
  44. doc.setField( "host", url.getHost() );
  45. doc.setField( "mime", entity.getMimeType() );
  46. doc.setField( "content", entity.getParse().getText() );
  47. return doc;
  48. }
  49. }