DocumentHelper.java 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright 2009-2014 the CodeLibs Project and the Others.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  13. * either express or implied. See the License for the specific language
  14. * governing permissions and limitations under the License.
  15. */
  16. package jp.sf.fess.helper;
  17. import javax.annotation.Resource;
  18. import jp.sf.fess.FessSystemException;
  19. import org.apache.solr.client.solrj.request.UpdateRequest;
  20. import org.apache.solr.common.SolrInputDocument;
  21. import org.codelibs.solr.lib.SolrGroup;
  22. import org.codelibs.solr.lib.SolrGroupManager;
  23. import org.codelibs.solr.lib.policy.QueryType;
  24. public class DocumentHelper {
  25. @Resource
  26. protected FieldHelper fieldHelper;
  27. @Resource
  28. protected SolrGroupManager solrGroupManager;
  29. public void update(final String docId, final String fieldName,
  30. final long num) {
  31. final SolrGroup solrGroup = solrGroupManager
  32. .getSolrGroup(QueryType.ADD);
  33. if (!solrGroup.isActive(QueryType.ADD)) {
  34. throw new FessSystemException("SolrGroup "
  35. + solrGroup.getGroupName() + " is not available.");
  36. }
  37. final SolrInputDocument doc = new SolrInputDocument();
  38. doc.setField(fieldHelper.idField, "none");
  39. doc.setField(fieldHelper.urlField, "none");
  40. doc.setField(fieldHelper.docIdField, docId);
  41. doc.setField(fieldName, num);
  42. final UpdateRequest req = new UpdateRequest();
  43. req.add(doc);
  44. req.setParam("excmd", "update");
  45. req.setParam("term", fieldHelper.docIdField);
  46. solrGroup.request(req);
  47. solrGroup.commit(false, false, true);
  48. }
  49. }