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

org.apache.solr.client.solrj.response.schema.SchemaResponse Maven / Gradle / Ivy

There is a newer version: 9.5.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.schema;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.solr.client.solrj.request.schema.AnalyzerDefinition;
import org.apache.solr.client.solrj.request.schema.FieldTypeDefinition;
import org.apache.solr.client.solrj.response.SolrResponseBase;
import org.apache.solr.common.util.NamedList;

/**
 * This class is used to wrap the response messages retrieved from Solr Schema API.
 *
 * @see Solr Schema API
 * @since solr 5.3
 */
public class SchemaResponse extends SolrResponseBase {
  private SchemaRepresentation schemaRepresentation;

  private static  Map extractAttributeMap(NamedList namedList) {
    if (namedList == null) return null;
    LinkedHashMap result = new LinkedHashMap<>();
    for (int i = 0; i < namedList.size(); i++) {
      T val = namedList.getVal(i);
      String name = namedList.getName(i);
      if (!(val instanceof NamedList) && !(val instanceof List)) {
        result.put(name, val);
      }
    }

    return result;
  }

  @SuppressWarnings("unchecked")
  private static AnalyzerDefinition createAnalyzerDefinition(NamedList analyzerNamedList) {
    AnalyzerDefinition analyzerDefinition = new AnalyzerDefinition();
    Map analyzerAttributes = extractAttributeMap(analyzerNamedList);
    analyzerDefinition.setAttributes(analyzerAttributes);
    List> charFiltersList = (List>) analyzerNamedList.get("charFilters");
    if (charFiltersList != null) {
      List> charFiltersAttributesList = new LinkedList<>();
      for (NamedList charFilterNamedList : charFiltersList) {
        Map charFilterAttributes = extractAttributeMap(charFilterNamedList);
        charFiltersAttributesList.add(charFilterAttributes);
      }
      analyzerDefinition.setCharFilters(charFiltersAttributesList);
    }
    NamedList tokenizerNamedList = (NamedList) analyzerNamedList.get("tokenizer");
    if (tokenizerNamedList != null) {
      Map tokenizerAttributes = extractAttributeMap(tokenizerNamedList);
      analyzerDefinition.setTokenizer(tokenizerAttributes);
    }
    List> filtersList = (List>) analyzerNamedList.get("filters");
    List> filtersAttributesList = new LinkedList<>();
    if (filtersList != null) {
      for (NamedList filterNamedList : filtersList) {
        Map filterAttributes = extractAttributeMap(filterNamedList);
        filtersAttributesList.add(filterAttributes);
      }
      analyzerDefinition.setFilters(filtersAttributesList);
    }

    return analyzerDefinition;
  }

  @SuppressWarnings("unchecked")
  private static FieldTypeDefinition createFieldTypeDefinition(NamedList fieldTypeNamedList) {
    FieldTypeDefinition fieldTypeDefinition = new FieldTypeDefinition();
    fillFieldTypeDefinition(fieldTypeDefinition, fieldTypeNamedList);
    return fieldTypeDefinition;
  }

  @SuppressWarnings("unchecked")
  private static FieldTypeRepresentation createFieldTypeRepresentation(NamedList fieldTypeNamedList) {
    FieldTypeRepresentation fieldTypeRepresentation = new FieldTypeRepresentation();
    fillFieldTypeDefinition(fieldTypeRepresentation, fieldTypeNamedList);
    List fields = (List) fieldTypeNamedList.get("fields");
    if (fields != null) fieldTypeRepresentation.setFields(fields);
    List dynamicFields = (List) fieldTypeNamedList.get("dynamicFields");
    if (dynamicFields != null) fieldTypeRepresentation.setDynamicFields(dynamicFields);
    return fieldTypeRepresentation;
  }

  @SuppressWarnings("unchecked")
  private static void fillFieldTypeDefinition(FieldTypeDefinition fieldTypeDefinition, NamedList fieldTypeNamedList) {
    Map fieldTypeAttributes = extractAttributeMap(fieldTypeNamedList);
    fieldTypeDefinition.setAttributes(fieldTypeAttributes);
    NamedList analyzerNamedList = (NamedList) fieldTypeNamedList.get("analyzer");
    if (analyzerNamedList != null) {
      AnalyzerDefinition analyzerDefinition = createAnalyzerDefinition(analyzerNamedList);
      fieldTypeDefinition.setAnalyzer(analyzerDefinition);
    }
    NamedList indexAnalyzerNamedList = (NamedList) fieldTypeNamedList.get("indexAnalyzer");
    if (indexAnalyzerNamedList != null) {
      AnalyzerDefinition indexAnalyzerDefinition =
          createAnalyzerDefinition(indexAnalyzerNamedList);
      fieldTypeDefinition.setIndexAnalyzer(indexAnalyzerDefinition);
    }
    NamedList queryAnalyzerNamedList = (NamedList) fieldTypeNamedList.get("queryAnalyzer");
    if (queryAnalyzerNamedList != null) {
      AnalyzerDefinition queryAnalyzerDefinition = createAnalyzerDefinition(queryAnalyzerNamedList);
      fieldTypeDefinition.setQueryAnalyzer(queryAnalyzerDefinition);
    }
    NamedList multiTermAnalyzerNamedList = (NamedList) fieldTypeNamedList.get("multiTermAnalyzer");
    if (multiTermAnalyzerNamedList != null) {
      AnalyzerDefinition multiTermAnalyzerDefinition =
          createAnalyzerDefinition(multiTermAnalyzerNamedList);
      fieldTypeDefinition.setMultiTermAnalyzer(multiTermAnalyzerDefinition);
    }
    NamedList similarityNamedList = (NamedList) fieldTypeNamedList.get("similarity");
    if (similarityNamedList != null) {
      Map similarityAttributes = extractAttributeMap(similarityNamedList);
      fieldTypeDefinition.setSimilarity(similarityAttributes);
    }
  }

  private static SchemaRepresentation createSchemaConfiguration(Map schemaObj) {
    SchemaRepresentation schemaRepresentation = new SchemaRepresentation();
    schemaRepresentation.setName(getSchemaName(schemaObj));
    schemaRepresentation.setVersion(getSchemaVersion(schemaObj));
    schemaRepresentation.setUniqueKey(getSchemaUniqueKey(schemaObj));
    schemaRepresentation.setDefaultSearchField(getDefaultSearchField(schemaObj));
    schemaRepresentation.setDefaultOperator(getDefaultOperator(schemaObj));
    schemaRepresentation.setSimilarity(getSimilarity(schemaObj));
    schemaRepresentation.setFields(getFields(schemaObj));
    schemaRepresentation.setDynamicFields(getDynamicFields(schemaObj));
    schemaRepresentation.setFieldTypes(getFieldTypeDefinitions(schemaObj));
    schemaRepresentation.setCopyFields(getCopyFields(schemaObj));
    return schemaRepresentation;
  }

  private static String getSchemaName(Map schemaNamedList) {
    return (String) schemaNamedList.get("name");
  }

  private static Float getSchemaVersion(Map schemaNamedList) {
    return (Float) schemaNamedList.get("version");
  }

  private static String getSchemaUniqueKey(Map schemaNamedList) {
    return (String) schemaNamedList.get("uniqueKey");
  }

  private static String getDefaultSearchField(Map schemaNamedList) {
    return (String) schemaNamedList.get("defaultSearchField");
  }

  private static Map getSimilarity(Map schemaNamedList) {
    NamedList similarityNamedList = (NamedList) schemaNamedList.get("similarity");
    Map similarity = null;
    if (similarityNamedList != null) similarity = extractAttributeMap(similarityNamedList);
    return similarity;
  }

  @SuppressWarnings("unchecked")
  private static String getDefaultOperator(Map schemaNamedList) {
    String defaultOperator = null;
    NamedList solrQueryParserProperties = (NamedList) schemaNamedList.get("solrQueryParser");
    if (solrQueryParserProperties != null) defaultOperator = (String) solrQueryParserProperties.get("defaultOperator");
    return defaultOperator;
  }

  @SuppressWarnings("unchecked")
  private static List> getFields(Map schemaNamedList) {
    List> fieldsAttributes = new LinkedList<>();
    List> fieldsResponse = (List>) schemaNamedList.get("fields");
    for (NamedList fieldNamedList : fieldsResponse) {
      Map fieldAttributes = new LinkedHashMap<>();
      fieldAttributes.putAll(extractAttributeMap(fieldNamedList));
      fieldsAttributes.add(fieldAttributes);
    }

    return fieldsAttributes;
  }

  @SuppressWarnings("unchecked")
  private static List> getDynamicFields(Map schemaNamedList) {
    List> dynamicFieldsAttributes = new LinkedList<>();
    List> dynamicFieldsResponse = (List>) schemaNamedList.get("dynamicFields");
    for (NamedList fieldNamedList : dynamicFieldsResponse) {
      Map dynamicFieldAttributes = new LinkedHashMap<>();
      dynamicFieldAttributes.putAll(extractAttributeMap(fieldNamedList));
      dynamicFieldsAttributes.add(dynamicFieldAttributes);
    }

    return dynamicFieldsAttributes;
  }

  @SuppressWarnings("unchecked")
  private static List> getCopyFields(Map schemaNamedList) {
    List> copyFieldsAttributes = new LinkedList<>();
    List> copyFieldsResponse = (List>) schemaNamedList.get("copyFields");
    for (NamedList copyFieldNamedList : copyFieldsResponse) {
      Map copyFieldAttributes = new LinkedHashMap<>();
      copyFieldAttributes.putAll(extractAttributeMap(copyFieldNamedList));
      copyFieldsAttributes.add(copyFieldAttributes);
    }

    return copyFieldsAttributes;
  }

  @SuppressWarnings("unchecked")
  private static List getFieldTypeDefinitions(Map schemaNamedList) {
    List fieldTypeDefinitions = new LinkedList<>();
    List> fieldsResponse = (List>) schemaNamedList.get("fieldTypes");
    for (NamedList fieldNamedList : fieldsResponse) {
      FieldTypeDefinition fieldTypeDefinition = createFieldTypeDefinition(fieldNamedList);
      fieldTypeDefinitions.add(fieldTypeDefinition);
    }

    return fieldTypeDefinitions;
  }

  @SuppressWarnings("unchecked")
  private static List getFieldTypeRepresentations(Map schemaNamedList) {
    List fieldTypeRepresentations = new LinkedList<>();
    List> fieldsResponse = (List>) schemaNamedList.get("fieldTypes");
    for (NamedList fieldNamedList : fieldsResponse) {
      FieldTypeRepresentation fieldTypeRepresentation = createFieldTypeRepresentation(fieldNamedList);
      fieldTypeRepresentations.add(fieldTypeRepresentation);
    }

    return fieldTypeRepresentations;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  @SuppressWarnings("unchecked")
  public void setResponse(NamedList response) {
    super.setResponse(response);

    Map schemaObj = (Map) response.get("schema");
    schemaRepresentation = createSchemaConfiguration(schemaObj);
  }

  public SchemaRepresentation getSchemaRepresentation() {
    return schemaRepresentation;
  }

  public static class SchemaNameResponse extends SolrResponseBase {
    private String schemaName;

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings("unchecked")
    public void setResponse(NamedList response) {
      super.setResponse(response);

      schemaName = SchemaResponse.getSchemaName(response.asShallowMap());
    }

    public String getSchemaName() {
      return schemaName;
    }

  }

  public static class SchemaVersionResponse extends SolrResponseBase {
    private float schemaVersion;

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings("unchecked")
    public void setResponse(NamedList response) {
      super.setResponse(response);

      schemaVersion = SchemaResponse.getSchemaVersion(response.asShallowMap());
    }

    public float getSchemaVersion() {
      return schemaVersion;
    }

  }

  public static class FieldResponse extends SolrResponseBase {
    Map field = new LinkedHashMap<>();

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings("unchecked")
    public void setResponse(NamedList response) {
      super.setResponse(response);

      NamedList fieldResponse = (NamedList) response.get("field");
      field.putAll(extractAttributeMap(fieldResponse));
    }

    public Map getField() {
      return field;
    }

  }

  public static class FieldsResponse extends SolrResponseBase {
    List> fields;

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings("unchecked")
    public void setResponse(NamedList response) {
      super.setResponse(response);

      fields = SchemaResponse.getFields(response.asShallowMap());
    }

    public List> getFields() {
      return fields;
    }
  }

  public static class DynamicFieldResponse extends SolrResponseBase {
    Map dynamicField = new LinkedHashMap<>();

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings("unchecked")
    public void setResponse(NamedList response) {
      super.setResponse(response);

      NamedList dynamicFieldResponse = (NamedList) response.get("dynamicField");
      dynamicField.putAll(extractAttributeMap(dynamicFieldResponse));
    }

    public Map getDynamicField() {
      return dynamicField;
    }

  }

  public static class DynamicFieldsResponse extends SolrResponseBase {
    List> dynamicFields;

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings("unchecked")
    public void setResponse(NamedList response) {
      super.setResponse(response);

      dynamicFields = SchemaResponse.getDynamicFields(response.asMap(3));
    }

    public List> getDynamicFields() {
      return dynamicFields;
    }
  }

  public static class UniqueKeyResponse extends SolrResponseBase {
    private String uniqueKey;

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings("unchecked")
    public void setResponse(NamedList response) {
      super.setResponse(response);

      uniqueKey = SchemaResponse.getSchemaUniqueKey(response.asShallowMap());
    }

    public String getUniqueKey() {
      return uniqueKey;
    }
  }

  public static class GlobalSimilarityResponse extends SolrResponseBase {
    Map similarity;

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings("unchecked")
    public void setResponse(NamedList response) {
      super.setResponse(response);

      similarity = SchemaResponse.getSimilarity(response.asShallowMap());
    }

    public Map getSimilarity() {
      return similarity;
    }

  }

  public static class DefaultQueryOperatorResponse extends SolrResponseBase {
    private String defaultOperator;

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings("unchecked")
    public void setResponse(NamedList response) {
      super.setResponse(response);

      defaultOperator = (String) response.get("defaultOperator");
    }

    public String getDefaultOperator() {
      return defaultOperator;
    }
  }

  public static class CopyFieldsResponse extends SolrResponseBase {
    List> copyFields;

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings("unchecked")
    public void setResponse(NamedList response) {
      super.setResponse(response);

      copyFields = SchemaResponse.getCopyFields(response.asShallowMap());
    }

    public List> getCopyFields() {
      return copyFields;
    }
  }

  public static class FieldTypeResponse extends SolrResponseBase {
    private FieldTypeRepresentation fieldType;

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings("unchecked")
    public void setResponse(NamedList response) {
      super.setResponse(response);

      NamedList fieldTypeNamedList = (NamedList) response.get("fieldType");
      fieldType = createFieldTypeRepresentation(fieldTypeNamedList);
    }

    public FieldTypeRepresentation getFieldType() {
      return fieldType;
    }
  }


  public static class FieldTypesResponse extends SolrResponseBase {
    List fieldTypes;

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings("unchecked")
    public void setResponse(NamedList response) {
      super.setResponse(response);

      fieldTypes = SchemaResponse.getFieldTypeRepresentations(response.asShallowMap());
    }

    public List getFieldTypes() {
      return fieldTypes;
    }
  }

  public static class UpdateResponse extends SolrResponseBase {
    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings("unchecked")
    public void setResponse(NamedList response) {
      super.setResponse(response);
    }
  }
}