All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.solr.client.solrj.response.DocumentAnalysisResponse Maven / Gradle / Ivy

There is a newer version: 9.7.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.solr.client.solrj.response;

import org.apache.solr.common.util.NamedList;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * A response that is returned by processing the {@link org.apache.solr.client.solrj.request.DocumentAnalysisRequest}.
 * Holds a map of {@link DocumentAnalysis} objects by a document id (unique key).
 *
 *
 * @since solr 1.4
 */
public class DocumentAnalysisResponse extends AnalysisResponseBase implements Iterable> {

  private final Map documentAnalysisByKey = new HashMap<>();

  @Override
  public void setResponse(NamedList response) {
    super.setResponse(response);

    @SuppressWarnings("unchecked")
    NamedList>> analysis 
      = (NamedList>>) response.get("analysis");
    for (Map.Entry>> document : analysis) {
      DocumentAnalysis documentAnalysis = new DocumentAnalysis(document.getKey());
      for (Map.Entry> fieldEntry : document.getValue()) {
        FieldAnalysis fieldAnalysis = new FieldAnalysis(fieldEntry.getKey());

        NamedList field = fieldEntry.getValue();

        @SuppressWarnings("unchecked")
        NamedList query
          = (NamedList) field.get("query");
        if (query != null) {
          List phases = buildPhases(query);
          fieldAnalysis.setQueryPhases(phases);
        }
        
        @SuppressWarnings("unchecked")
        NamedList> index
          = (NamedList>) field.get("index");
        for (Map.Entry> valueEntry : index) {
          String fieldValue = valueEntry.getKey();
          NamedList valueNL = valueEntry.getValue();
          List phases = buildPhases(valueNL);
          fieldAnalysis.setIndexPhases(fieldValue, phases);
        }

        documentAnalysis.addFieldAnalysis(fieldAnalysis);
      }

      documentAnalysisByKey.put(documentAnalysis.getDocumentKey(), documentAnalysis);
    }
  }

  /**
   * Returns the number of document analyses in this response.
   *
   * @return The number of document analyses in this response.
   */
  public int getDocumentAnalysesCount() {
    return documentAnalysisByKey.size();
  }

  /**
   * Returns the document analysis for the document associated with the given unique key (id), {@code null} if no such
   * association exists.
   *
   * @param documentKey The document unique key.
   *
   * @return The document analysis for the document associated with the given unique key (id).
   */
  public DocumentAnalysis getDocumentAnalysis(String documentKey) {
    return documentAnalysisByKey.get(documentKey);
  }

  /**
   * Returns an iterator over the document analyses map.
   *
   * @return An iterator over the document analyses map.
   */
  @Override
  public Iterator> iterator() {
    return documentAnalysisByKey.entrySet().iterator();
  }

  //================================================= Inner Classes ==================================================

  /**
   * An analysis process breakdown of a document. Holds a map of field analyses by the field name.
   */
  public static class DocumentAnalysis implements Iterable> {

    private final String documentKey;
    private Map fieldAnalysisByFieldName = new HashMap<>();

    private DocumentAnalysis(String documentKey) {
      this.documentKey = documentKey;
    }

    private void addFieldAnalysis(FieldAnalysis fieldAnalysis) {
      fieldAnalysisByFieldName.put(fieldAnalysis.getFieldName(), fieldAnalysis);
    }

    /**
     * Returns the unique key of the analyzed document.
     *
     * @return The unique key of the analyzed document.
     */
    public String getDocumentKey() {
      return documentKey;
    }

    /**
     * Returns the number of field analyses for the documents.
     *
     * @return The number of field analyses for the documents.
     */
    public int getFieldAnalysesCount() {
      return fieldAnalysisByFieldName.size();
    }

    public FieldAnalysis getFieldAnalysis(String fieldName) {
      return fieldAnalysisByFieldName.get(fieldName);
    }

    /**
     * Returns an iterator over the field analyses map.
     *
     * @return An iterator over the field analyses map.
     */
    @Override
    public Iterator> iterator() {
      return fieldAnalysisByFieldName.entrySet().iterator();
    }
  }

  /**
   * An analysis process breakdown for a specific field. Holds a list of query time analysis phases (that is, if a
   * query analysis was requested in the first place) and a list of index time analysis phases for each field value (a
   * field can be multi-valued).
   */
  public static class FieldAnalysis {

    private final String fieldName;
    private List queryPhases;
    private Map> indexPhasesByFieldValue = new HashMap<>();

    private FieldAnalysis(String fieldName) {
      this.fieldName = fieldName;
    }

    public void setQueryPhases(List queryPhases) {
      this.queryPhases = queryPhases;
    }

    public void setIndexPhases(String fieldValue, List indexPhases) {
      indexPhasesByFieldValue.put(fieldValue, indexPhases);
    }

    /**
     * Returns the field name.
     *
     * @return The name of the field.
     */
    public String getFieldName() {
      return fieldName;
    }

    /**
     * Returns the number of query time analysis phases or {@code -1} if 
     * this field analysis doesn't hold a query time analysis.
     *
     * @return Returns the number of query time analysis phases or {@code -1} 
     *         if this field analysis doesn't hold a query time analysis.
     */
    public int getQueryPhasesCount() {
      return queryPhases == null ? -1 : queryPhases.size();
    }

    /**
     * Returns the query time analysis phases for the field or {@code null} 
     * if this field doesn't hold a query time analysis.
     *
     * @return Returns the query time analysis phases for the field or 
     *         {@code null} if this field doesn't hold a query time analysis.
     */
    public Iterable getQueryPhases() {
      return queryPhases;
    }

    /**
     * Returns the number of values the field has.
     *
     * @return The number of values the field has.
     */
    public int getValueCount() {
      return indexPhasesByFieldValue.entrySet().size();
    }

    /**
     * Returns the number of index time analysis phases the given field value has.
     *
     * @param fieldValue The field value.
     *
     * @return The number of index time analysis phases the given field value has.
     */
    public int getIndexPhasesCount(String fieldValue) {
      return indexPhasesByFieldValue.get(fieldValue).size();
    }

    /**
     * Returns the index time analysis phases for the given field value.
     *
     * @param fieldValue The field value.
     *
     * @return The index time analysis phases for the given field value.
     */
    public Iterable getIndexPhases(String fieldValue) {
      return indexPhasesByFieldValue.get(fieldValue);
    }

    /**
     * Returns the index time analysis phases for all field values.
     *
     * @return Returns the index time analysis phases for all field value.
     */
    public Iterable>> getIndexPhasesByFieldValue() {
      return indexPhasesByFieldValue.entrySet();
    }

  }

}