PageRenderTime 24ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/solrj/org/apache/solr/client/solrj/response/DocumentAnalysisResponse.java

https://github.com/adsabs/solr-affiliation-disambiguation
Java | 250 lines | 102 code | 37 blank | 111 comment | 6 complexity | 18a3f82ee06daa27a15fc9ec66fc29ab 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.solr.client.solrj.response;
  18. import org.apache.solr.common.util.NamedList;
  19. import java.util.HashMap;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. import java.util.Map;
  23. /**
  24. * A response that is returned by processing the {@link org.apache.solr.client.solrj.request.DocumentAnalysisRequest}.
  25. * Holds a map of {@link DocumentAnalysis} objects by a document id (unique key).
  26. *
  27. * @version $Id: DocumentAnalysisResponse.java 823653 2009-10-09 18:27:13Z hossman $
  28. * @since solr 1.4
  29. */
  30. public class DocumentAnalysisResponse extends AnalysisResponseBase implements Iterable<Map.Entry<String, DocumentAnalysisResponse.DocumentAnalysis>> {
  31. private final Map<String, DocumentAnalysis> documentAnalysisByKey = new HashMap<String, DocumentAnalysis>();
  32. /**
  33. * {@inheritDoc}
  34. */
  35. @Override
  36. public void setResponse(NamedList<Object> response) {
  37. super.setResponse(response);
  38. NamedList<Object> analysis = (NamedList<Object>) response.get("analysis");
  39. for (Map.Entry<String, Object> documentEntry : analysis) {
  40. DocumentAnalysis documentAnalysis = new DocumentAnalysis(documentEntry.getKey());
  41. NamedList<Object> document = (NamedList<Object>) documentEntry.getValue();
  42. for (Map.Entry<String, Object> fieldEntry : document) {
  43. FieldAnalysis fieldAnalysis = new FieldAnalysis(fieldEntry.getKey());
  44. NamedList field = (NamedList) fieldEntry.getValue();
  45. NamedList<Object> query = (NamedList<Object>) field.get("query");
  46. if (query != null) {
  47. List<AnalysisPhase> phases = buildPhases(query);
  48. fieldAnalysis.setQueryPhases(phases);
  49. }
  50. NamedList<Object> index = (NamedList<Object>) field.get("index");
  51. for (Map.Entry<String, Object> valueEntry : index) {
  52. String fieldValue = valueEntry.getKey();
  53. NamedList<Object> valueNL = (NamedList<Object>) valueEntry.getValue();
  54. List<AnalysisPhase> phases = buildPhases(valueNL);
  55. fieldAnalysis.setIndexPhases(fieldValue, phases);
  56. }
  57. documentAnalysis.addFieldAnalysis(fieldAnalysis);
  58. }
  59. documentAnalysisByKey.put(documentAnalysis.getDocumentKey(), documentAnalysis);
  60. }
  61. }
  62. /**
  63. * Returns the number of document analyses in this response.
  64. *
  65. * @return The number of document analyses in this response.
  66. */
  67. public int getDocumentAnalysesCount() {
  68. return documentAnalysisByKey.size();
  69. }
  70. /**
  71. * Returns the document analysis for the document associated with the given unique key (id), {@code null} if no such
  72. * association exists.
  73. *
  74. * @param documentKey The document unique key.
  75. *
  76. * @return The document analysis for the document associated with the given unique key (id).
  77. */
  78. public DocumentAnalysis getDocumentAnalysis(String documentKey) {
  79. return documentAnalysisByKey.get(documentKey);
  80. }
  81. /**
  82. * Returns an iterator over the document analyses map.
  83. *
  84. * @return An iterator over the document analyses map.
  85. */
  86. public Iterator<Map.Entry<String, DocumentAnalysis>> iterator() {
  87. return documentAnalysisByKey.entrySet().iterator();
  88. }
  89. //================================================= Inner Classes ==================================================
  90. /**
  91. * An analysis process breakdown of a document. Holds a map of field analyses by the field name.
  92. */
  93. public static class DocumentAnalysis implements Iterable<Map.Entry<String, FieldAnalysis>> {
  94. private final String documentKey;
  95. private Map<String, FieldAnalysis> fieldAnalysisByFieldName = new HashMap<String, FieldAnalysis>();
  96. private DocumentAnalysis(String documentKey) {
  97. this.documentKey = documentKey;
  98. }
  99. private void addFieldAnalysis(FieldAnalysis fieldAnalysis) {
  100. fieldAnalysisByFieldName.put(fieldAnalysis.getFieldName(), fieldAnalysis);
  101. }
  102. /**
  103. * Returns the unique key of the analyzed document.
  104. *
  105. * @return The unique key of the analyzed document.
  106. */
  107. public String getDocumentKey() {
  108. return documentKey;
  109. }
  110. /**
  111. * Returns the number of field analyses for the documents.
  112. *
  113. * @return The number of field analyses for the documents.
  114. */
  115. public int getFieldAnalysesCount() {
  116. return fieldAnalysisByFieldName.size();
  117. }
  118. public FieldAnalysis getFieldAnalysis(String fieldName) {
  119. return fieldAnalysisByFieldName.get(fieldName);
  120. }
  121. /**
  122. * Returns an iterator over the field analyses map.
  123. *
  124. * @return An iterator over the field analyses map.
  125. */
  126. public Iterator<Map.Entry<String, FieldAnalysis>> iterator() {
  127. return fieldAnalysisByFieldName.entrySet().iterator();
  128. }
  129. }
  130. /**
  131. * An analysis process breakdown for a specific field. Holds a list of query time analysis phases (that is, if a
  132. * query analysis was requested in the first place) and a list of index time analysis phases for each field value (a
  133. * field can be multi-valued).
  134. */
  135. public static class FieldAnalysis {
  136. private final String fieldName;
  137. private List<AnalysisPhase> queryPhases;
  138. private Map<String, List<AnalysisPhase>> indexPhasesByFieldValue = new HashMap<String, List<AnalysisPhase>>();
  139. private FieldAnalysis(String fieldName) {
  140. this.fieldName = fieldName;
  141. }
  142. public void setQueryPhases(List<AnalysisPhase> queryPhases) {
  143. this.queryPhases = queryPhases;
  144. }
  145. public void setIndexPhases(String fieldValue, List<AnalysisPhase> indexPhases) {
  146. indexPhasesByFieldValue.put(fieldValue, indexPhases);
  147. }
  148. /**
  149. * Returns the field name.
  150. *
  151. * @return The name of the field.
  152. */
  153. public String getFieldName() {
  154. return fieldName;
  155. }
  156. /**
  157. * Returns the number of query time analysis phases or {@code -1} if
  158. * this field analysis doesn't hold a query time analysis.
  159. *
  160. * @return Returns the number of query time analysis phases or {@code -1}
  161. * if this field analysis doesn't hold a query time analysis.
  162. */
  163. public int getQueryPhasesCount() {
  164. return queryPhases == null ? -1 : queryPhases.size();
  165. }
  166. /**
  167. * Returns the query time analysis phases for the field or {@code null}
  168. * if this field doesn't hold a query time analysis.
  169. *
  170. * @return Returns the query time analysis phases for the field or
  171. * {@code null} if this field doesn't hold a query time analysis.
  172. */
  173. public Iterable<AnalysisPhase> getQueryPhases() {
  174. return queryPhases;
  175. }
  176. /**
  177. * Returns the number of values the field has.
  178. *
  179. * @return The number of values the field has.
  180. */
  181. public int getValueCount() {
  182. return indexPhasesByFieldValue.entrySet().size();
  183. }
  184. /**
  185. * Returns the number of index time analysis phases the given field value has.
  186. *
  187. * @param fieldValue The field value.
  188. *
  189. * @return The number of index time analysis phases the given field value has.
  190. */
  191. public int getIndexPhasesCount(String fieldValue) {
  192. return indexPhasesByFieldValue.get(fieldValue).size();
  193. }
  194. /**
  195. * Returns the index time analysis phases for the given field value.
  196. *
  197. * @param fieldValue The field value.
  198. *
  199. * @return The index time analysis phases for the given field value.
  200. */
  201. public Iterable<AnalysisPhase> getIndexPhases(String fieldValue) {
  202. return indexPhasesByFieldValue.get(fieldValue);
  203. }
  204. /**
  205. * Returns the index time analysis phases for all field values.
  206. *
  207. * @return Returns the index time analysis phases for all field value.
  208. */
  209. public Iterable<Map.Entry<String, List<AnalysisPhase>>> getIndexPhasesByFieldValue() {
  210. return indexPhasesByFieldValue.entrySet();
  211. }
  212. }
  213. }