Java Code Examples for org.apache.solr.common.util.NamedList#addAll()

The following examples show how to use org.apache.solr.common.util.NamedList#addAll() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SchemaRequest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private static NamedList<Object> createAnalyzerNamedList(AnalyzerDefinition analyzerDefinition) {
  NamedList<Object> analyzerNamedList = new NamedList<>();
  Map<String, Object> analyzerAttributes = analyzerDefinition.getAttributes();
  if (analyzerAttributes != null)
    analyzerNamedList.addAll(analyzerAttributes);
  List<Map<String, Object>> charFiltersAttributes = analyzerDefinition.getCharFilters();
  if (charFiltersAttributes != null) {
    List<NamedList<Object>> charFiltersList = new LinkedList<>();
    for (Map<String, Object> charFilterAttributes : charFiltersAttributes)
      charFiltersList.add(new NamedList<>(charFilterAttributes));
    analyzerNamedList.add("charFilters", charFiltersList);
  }
  Map<String, Object> tokenizerAttributes = analyzerDefinition.getTokenizer();
  if (tokenizerAttributes != null) {
    analyzerNamedList.add("tokenizer", new NamedList<>(tokenizerAttributes));
  }
  List<Map<String, Object>> filtersAttributes = analyzerDefinition.getFilters();
  if (filtersAttributes != null) {
    List<NamedList<Object>> filtersList = new LinkedList<>();
    for (Map<String, Object> filterAttributes : filtersAttributes)
      filtersList.add(new NamedList<>(filterAttributes));
    analyzerNamedList.add("filters", filtersList);
  }
  return analyzerNamedList;
}
 
Example 2
Source File: BaseCloudSolrClient.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public RouteException(ErrorCode errorCode, NamedList<Throwable> throwables, Map<String, ? extends LBSolrClient.Req> routes){
  super(errorCode, throwables.getVal(0).getMessage(), throwables.getVal(0));
  this.throwables = throwables;
  this.routes = routes;

  // create a merged copy of the metadata from all wrapped exceptions
  NamedList<String> metadata = new NamedList<String>();
  for (int i = 0; i < throwables.size(); i++) {
    Throwable t = throwables.getVal(i);
    if (t instanceof SolrException) {
      SolrException e = (SolrException) t;
      NamedList<String> eMeta = e.getMetadata();
      if (null != eMeta) {
        metadata.addAll(eMeta);
      }
    }
  }
  if (0 < metadata.size()) {
    this.setMetadata(metadata);
  }
}
 
Example 3
Source File: RangeFacetRequest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Removes all counts under the given minCount from the accumulated facet_ranges.
 * <p>
 * Note: this method should only be called after all shard responses have been
 * accumulated using {@link #mergeContributionFromShard(SimpleOrderedMap)}
 *
 * @param minCount the minimum allowed count for any range
 */
public void removeRangeFacetsUnderLimits(int minCount) {
  boolean replace = false;

  @SuppressWarnings("unchecked")
  NamedList<Number> vals = (NamedList<Number>) rangeFacet.get("counts");
  NamedList<Number> newList = new NamedList<>();
  for (Map.Entry<String, Number> pair : vals) {
    if (pair.getValue().longValue() >= minCount) {
      newList.add(pair.getKey(), pair.getValue());
    } else {
      replace = true;
    }
  }
  if (replace) {
    vals.clear();
    vals.addAll(newList);
  }
}
 
Example 4
Source File: SchemaRequest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private static NamedList<Object> createAddFieldTypeNamedList(FieldTypeDefinition fieldTypeDefinition) {
  final NamedList<Object> addFieldTypeNamedList = new NamedList<>();
  addFieldTypeNamedList.addAll(fieldTypeDefinition.getAttributes());
  AnalyzerDefinition analyzerDefinition = fieldTypeDefinition.getAnalyzer();
  if (analyzerDefinition != null) {
    NamedList<Object> analyzerNamedList = createAnalyzerNamedList(analyzerDefinition);
    addFieldTypeNamedList.add("analyzer", analyzerNamedList);
  }
  AnalyzerDefinition indexAnalyzerDefinition = fieldTypeDefinition.getIndexAnalyzer();
  if (indexAnalyzerDefinition != null) {
    NamedList<Object> indexAnalyzerNamedList = createAnalyzerNamedList(indexAnalyzerDefinition);
    addFieldTypeNamedList.add("indexAnalyzer", indexAnalyzerNamedList);
  }
  AnalyzerDefinition queryAnalyzerDefinition = fieldTypeDefinition.getQueryAnalyzer();
  if (queryAnalyzerDefinition != null) {
    NamedList<Object> queryAnalyzerNamedList = createAnalyzerNamedList(queryAnalyzerDefinition);
    addFieldTypeNamedList.add("queryAnalyzer", queryAnalyzerNamedList);
  }
  AnalyzerDefinition multiTermAnalyzerDefinition = fieldTypeDefinition.getMultiTermAnalyzer();
  if (multiTermAnalyzerDefinition != null) {
    NamedList<Object> multiTermAnalyzerNamedList = createAnalyzerNamedList(multiTermAnalyzerDefinition);
    addFieldTypeNamedList.add("multiTermAnalyzer", multiTermAnalyzerNamedList);
  }
  Map<String, Object> similarityAttributes = fieldTypeDefinition.getSimilarity();
  if (similarityAttributes != null && !similarityAttributes.isEmpty()) {
    addFieldTypeNamedList.add("similarity", new NamedList<>(similarityAttributes));
  }

  return addFieldTypeNamedList;
}
 
Example 5
Source File: SchemaRequest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
protected NamedList<Object> getRequestParameters() {
  NamedList<Object> multipleRequestsParameters = new NamedList<>();
  for (Update updateSchemaRequest : updateSchemaRequests) {
    multipleRequestsParameters.addAll(updateSchemaRequest.getRequestParameters());
  }
  return multipleRequestsParameters;
}
 
Example 6
Source File: FacetParser.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Helper that handles the possibility of map values being lists
 * NOTE: does *NOT* fail on map values that are sub-maps (ie: nested json objects)
 */
@SuppressWarnings({"unchecked", "rawtypes"})
public static SolrParams jsonToSolrParams(Map jsonObject) {
  // HACK, but NamedList already handles the list processing for us...
  NamedList<String> nl = new NamedList<>();
  nl.addAll(jsonObject);
  return SolrParams.toSolrParams(nl);
}
 
Example 7
Source File: AbstractSolrEventListener.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Add the {@link org.apache.solr.common.params.EventParams#EVENT} with either the {@link org.apache.solr.common.params.EventParams#NEW_SEARCHER}
 * or {@link org.apache.solr.common.params.EventParams#FIRST_SEARCHER} values depending on the value of currentSearcher.
 * <p>
 * Makes a copy of NamedList and then adds the parameters.
 *
 *
 * @param currentSearcher If null, add FIRST_SEARCHER, otherwise NEW_SEARCHER
 * @param nlst The named list to add the EVENT value to
 */
@SuppressWarnings({"unchecked", "rawtypes"})
protected NamedList addEventParms(SolrIndexSearcher currentSearcher, NamedList nlst) {
  NamedList result = new NamedList();
  result.addAll(nlst);
  if (currentSearcher != null) {
    result.add(EventParams.EVENT, EventParams.NEW_SEARCHER);
  } else {
    result.add(EventParams.EVENT, EventParams.FIRST_SEARCHER);
  }
  return result;
}
 
Example 8
Source File: AlfrescoSpellCheckComponent.java    From SearchServices with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * <b>Disclaimer</b>: The code copied from the super class (
 * {@link org.apache.solr.handler.component.SpellCheckComponent}) but only
 * replaced the collator with the AlfrescoSpellCheckCollator
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected void addCollationsToResponse(SolrParams params, SpellingResult spellingResult, ResponseBuilder rb,
            String q, NamedList response, boolean suggestionsMayOverlap)
{
    int maxCollations = params.getInt(SPELLCHECK_MAX_COLLATIONS, 1);
    int maxCollationTries = params.getInt(SPELLCHECK_MAX_COLLATION_TRIES, 0);
    int maxCollationEvaluations = params.getInt(SPELLCHECK_MAX_COLLATION_EVALUATIONS, 10000);
    boolean collationExtendedResults = params.getBool(SPELLCHECK_COLLATE_EXTENDED_RESULTS, false);
    int maxCollationCollectDocs = params.getInt(SPELLCHECK_COLLATE_MAX_COLLECT_DOCS, 0);
    // If not reporting hits counts, don't bother collecting more than 1 document per try.
    if (!collationExtendedResults)
    {
        maxCollationCollectDocs = 1;
    }
    boolean shard = params.getBool(ShardParams.IS_SHARD, false);
    AlfrescoSpellCheckCollator collator = new AlfrescoSpellCheckCollator();
    collator.setMaxCollations(maxCollations);
    collator.setMaxCollationTries(maxCollationTries);
    collator.setMaxCollationEvaluations(maxCollationEvaluations);
    collator.setSuggestionsMayOverlap(suggestionsMayOverlap);
    collator.setDocCollectionLimit(maxCollationCollectDocs);

    List<AlfrescoSpellCheckCollation> collations = collator.collate(spellingResult, q, rb);
    // by sorting here we guarantee a non-distributed request returns all
    // results in the same order as a distributed request would,
    // even in cases when the internal rank is the same.
    Collections.sort(collations);

    NamedList collationList = new NamedList();

    for (AlfrescoSpellCheckCollation collation : collations)
    {
        if (collationExtendedResults)
        {
            NamedList extendedResult = new SimpleOrderedMap();
            extendedResult.add("collationQuery", collation.getCollationQuery());
            extendedResult.add("hits", collation.getHits());
            extendedResult.add("misspellingsAndCorrections", collation.getMisspellingsAndCorrections());
            if (maxCollationTries > 0 && shard)
            {
                extendedResult.add("collationInternalRank", collation.getInternalRank());
            }
            extendedResult.add("collationQueryString", collation.getCollationQueryString());
            collationList.add("collation", extendedResult);
        }
        else
        {
            collationList.add("collation", collation.getCollationQuery());
            if (maxCollationTries > 0 && shard)
            {
               collationList.add("collationInternalRank", collation.getInternalRank());
            }
        }
    }

    //Support Solr 4 output format
    NamedList suggestions = (NamedList)response.get("suggestions");
    suggestions.addAll(collationList);

    //Support Solr distributed merge format.
    response.add("collations", collationList);
}
 
Example 9
Source File: SchemaRequest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private static NamedList<Object> createAddFieldNamedList(Map<String, Object> fieldAttributes) {
  final NamedList<Object> addFieldProps = new NamedList<>();
  if (fieldAttributes != null) addFieldProps.addAll(fieldAttributes);
  return addFieldProps;
}
 
Example 10
Source File: DebugComponent.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void process(ResponseBuilder rb) throws IOException
{
  if( rb.isDebug() ) {
    SolrQueryRequest req = rb.req;
    StatsCache statsCache = req.getSearcher().getStatsCache();
    req.getContext().put(SolrIndexSearcher.STATS_SOURCE, statsCache.get(req));
    DocList results = null;
    //some internal grouping requests won't have results value set
    if(rb.getResults() != null) {
      results = rb.getResults().docList;
    }

    @SuppressWarnings({"rawtypes"})
    NamedList stdinfo = SolrPluginUtils.doStandardDebug( rb.req,
        rb.getQueryString(), rb.wrap(rb.getQuery()), results, rb.isDebugQuery(), rb.isDebugResults());
    
    @SuppressWarnings({"rawtypes"})
    NamedList info = rb.getDebugInfo();
    if( info == null ) {
      rb.setDebugInfo( stdinfo );
      info = stdinfo;
    }
    else {
      info.addAll( stdinfo );
    }

    FacetDebugInfo fdebug = (FacetDebugInfo)(rb.req.getContext().get("FacetDebugInfo"));
    if (fdebug != null) {
      info.add("facet-trace", fdebug.getFacetDebugInfo());
    }

    fdebug = (FacetDebugInfo)(rb.req.getContext().get("FacetDebugInfo-nonJson"));
    if (fdebug != null) {
      info.add("facet-debug", fdebug.getFacetDebugInfo());
    }
    
    if (rb.req.getJSON() != null) {
      info.add(JSON, rb.req.getJSON());
    }

    if (rb.isDebugQuery() && rb.getQparser() != null) {
      rb.getQparser().addDebugInfo(rb.getDebugInfo());
    }
    
    if (null != rb.getDebugInfo() ) {
      if (rb.isDebugQuery() && null != rb.getFilters() ) {
        info.add("filter_queries",rb.req.getParams().getParams(FQ));
        List<String> fqs = new ArrayList<>(rb.getFilters().size());
        for (Query fq : rb.getFilters()) {
          fqs.add(QueryParsing.toString(fq, rb.req.getSchema()));
        }
        info.add("parsed_filter_queries",fqs);
      }
      
      // Add this directly here?
      rb.rsp.add("debug", rb.getDebugInfo() );
    }
  }
}
 
Example 11
Source File: DebugComponent.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({"unchecked", "rawtypes"})
protected Object merge(Object source, Object dest, Set<String> exclude) {
  if (source == null) return dest;
  if (dest == null) {
    if (source instanceof NamedList) {
      dest = source instanceof SimpleOrderedMap ? new SimpleOrderedMap() : new NamedList();
    } else {
      return source;
    }
  } else {

    if (dest instanceof Collection) {
      // merge as Set
      if (!(dest instanceof Set)) {
        dest = new LinkedHashSet<>((Collection<?>) dest);
      }
      if (source instanceof Collection) {
        ((Collection)dest).addAll((Collection)source);
      } else {
        ((Collection)dest).add(source);
      }
      return dest;
    } else if (source instanceof Number) {
      if (dest instanceof Number) {
        if (source instanceof Double || dest instanceof Double) {
          return ((Number)source).doubleValue() + ((Number)dest).doubleValue();
        }
        return ((Number)source).longValue() + ((Number)dest).longValue();
      }
      // fall through
    } else if (source instanceof String) {
      if (source.equals(dest)) {
        return dest;
      }
      // fall through
    }
  }


  if (source instanceof NamedList && dest instanceof NamedList) {
    NamedList<Object> tmp = new NamedList<>();
    @SuppressWarnings("unchecked")
    NamedList<Object> sl = (NamedList<Object>)source;
    @SuppressWarnings("unchecked")
    NamedList<Object> dl = (NamedList<Object>)dest;
    for (int i=0; i<sl.size(); i++) {
      String skey = sl.getName(i);
      if (exclude.contains(skey)) continue;
      Object sval = sl.getVal(i);
      int didx = -1;

      // optimize case where elements are in same position
      if (i < dl.size()) {
        String dkey = dl.getName(i);
        if (skey == dkey || (skey!=null && skey.equals(dkey))) {
          didx = i;
        }
      }

      if (didx == -1) {
        didx = dl.indexOf(skey, 0);
      }

      if (didx == -1) {
        tmp.add(skey, merge(sval, null, Collections.emptySet()));
      } else {
        dl.setVal(didx, merge(sval, dl.getVal(didx), Collections.emptySet()));
      }
    }
    dl.addAll(tmp);
    return dl;
  }

  // only add to list if JSON is different
  if (source.equals(dest)) return source;

  // merge unlike elements in a list
  List<Object> t = new ArrayList<>();
  t.add(dest);
  t.add(source);
  return t;
}