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

org.apache.solr.client.solrj.response.SuggesterResponse 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 java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

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

/**
 * Encapsulates responses from the Suggester Component
 */
public class SuggesterResponse {

  private static final String SUGGESTIONS_NODE_NAME = "suggestions";
  private static final String TERM_NODE_NAME = "term";
  private static final String WEIGHT_NODE_NAME = "weight";
  private static final String PAYLOAD_NODE_NAME = "payload";

  private final Map> suggestionsPerDictionary = new LinkedHashMap<>();

  public SuggesterResponse(Map> suggestInfo) {
    for (Map.Entry> entry : suggestInfo.entrySet()) {
      SimpleOrderedMap suggestionsNode = (SimpleOrderedMap) entry.getValue().getVal(0);
      List suggestionListToParse;
      List suggestionList = new LinkedList<>();
      if (suggestionsNode != null) {

        suggestionListToParse = (List) suggestionsNode.get(SUGGESTIONS_NODE_NAME);
        for (SimpleOrderedMap suggestion : suggestionListToParse) {
          String term = (String) suggestion.get(TERM_NODE_NAME);
          long weight = (long) suggestion.get(WEIGHT_NODE_NAME);
          String payload = (String) suggestion.get(PAYLOAD_NODE_NAME);

          Suggestion parsedSuggestion = new Suggestion(term, weight, payload);
          suggestionList.add(parsedSuggestion);
        }
        suggestionsPerDictionary.put(entry.getKey(), suggestionList);
      }
    }
  }

  /**
   * get the suggestions provided by each
   *
   * @return a Map dictionary name : List of Suggestion
   */
  public Map> getSuggestions() {
    return suggestionsPerDictionary;
  }

  /**
   * This getter is lazily initialized and returns a simplified map dictionary : List of suggested terms
   * This is useful for simple use cases when you simply need the suggested terms and no weight or payload
   *
   * @return a Map dictionary name : List of suggested terms
   */
  public Map> getSuggestedTerms() {
    Map> suggestedTermsPerDictionary = new LinkedHashMap<>();
    for (Map.Entry> entry : suggestionsPerDictionary.entrySet()) {
      List suggestions = entry.getValue();
      List suggestionTerms = new LinkedList();
      for (Suggestion s : suggestions) {
        suggestionTerms.add(s.getTerm());
      }
      suggestedTermsPerDictionary.put(entry.getKey(), suggestionTerms);
    }
    return suggestedTermsPerDictionary;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy