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

The following examples show how to use org.apache.solr.common.util.NamedList#setVal() . 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: SolrInformationServer.java    From SearchServices with GNU Lesser General Public License v3.0 6 votes vote down vote up
private NamedList<Object> fixStats(NamedList<Object> namedList)
{
    int sz = namedList.size();

    for (int i = 0; i < sz; i++)
    {
        Object value = namedList.getVal(i);
        if (value instanceof Number)
        {
            Number number = (Number) value;
            if (Float.isInfinite(number.floatValue()) || Float.isNaN(number.floatValue())
                    || Double.isInfinite(number.doubleValue()) || Double.isNaN(number.doubleValue()))
            {
                namedList.setVal(i, null);
            }
        }
    }
    return namedList;
}
 
Example 2
Source File: PivotFacetHelper.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Merges query counts returned by a shard into global query counts.
 * Entries found only in shard's query counts will be added to global counts.
 * Entries found in both shard and global query counts will be summed.
 *
 * @param globalQueryCounts The global query counts (across all shards) in which to merge the shard query counts
 * @param shardQueryCounts  Named list from a shard response to be merged into the global counts.
 * @return NamedList containing merged values
 */
static NamedList<Number> mergeQueryCounts(
    NamedList<Number> globalQueryCounts, NamedList<Number> shardQueryCounts) {
  if (globalQueryCounts == null) {
    return shardQueryCounts;
  }
  for (Entry<String, Number> entry : shardQueryCounts) {
    int idx = globalQueryCounts.indexOf(entry.getKey(), 0);
    if (idx == -1) {
      globalQueryCounts.add(entry.getKey(), entry.getValue());
    } else {
      globalQueryCounts.setVal(idx, FacetComponent.num(globalQueryCounts.getVal(idx).longValue() + entry.getValue().longValue()));
    }
  }
  return globalQueryCounts;
}
 
Example 3
Source File: RewriteFacetCountsComponent.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void processPivot(ResponseBuilder rb, String[] fromParts, String[] toParts, Collection<NamedList<Object>> current, int level)
{
    for(NamedList<Object> entry : current)
    {
        for(int i = 0; i < entry.size(); i++)
        {
            String name = entry.getName(i);
            if(name.equals("field"))
            {
                entry.setVal(i, fromParts[level].trim());
            }
            else if(name.equals("pivot"))
            {
                Collection<NamedList<Object>> pivot = (Collection<NamedList<Object>>)entry.getVal(i);
                processPivot(rb, fromParts, toParts, pivot, level+1);
            }
            else if(name.equals("ranges"))
            {
            	SimpleOrderedMap ranges = (SimpleOrderedMap)entry.getVal(i);
            	processRanges(rb, ranges);
            }
            else
            {
                // leave alone
            }
        }
    }
}
 
Example 4
Source File: SolrInfoMBeanHandler.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * The 'avgRequestsPerSecond' field will make everything look like it changed
 */
@SuppressWarnings({"rawtypes"})
public NamedList normalize(NamedList input) {
  input.remove("avgRequestsPerSecond");
  for(int i=0; i<input.size(); i++) {
    Object v = input.getVal(i);
    if(v instanceof NamedList) {
      input.setVal(i, normalize((NamedList)v));
    }
  }
  return input;
}
 
Example 5
Source File: MtasSolrResultUtil.java    From mtas with Apache License 2.0 5 votes vote down vote up
/**
 * Decode.
 *
 * @param nl the nl
 * @return the named list
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
static NamedList<Object> decode(NamedList<Object> nl) {
  for (int i = 0; i < nl.size(); i++) {
    String key = nl.getName(i);
    Object o = nl.getVal(i);
    if (key.matches("^_encoded_.*$")) {
      if (o instanceof String) {
        Object decodedObject = decode((String) nl.getVal(i));
        String decodedKey = key.replaceFirst("^_encoded_", "");
        if (decodedKey.equals("")) {
          decodedKey = "_" + decodedObject.getClass().getSimpleName() + "_";
        }
        nl.setName(i, decodedKey);
        nl.setVal(i, decodedObject);
      } else if (o instanceof NamedList) {
        NamedList nl2 = (NamedList) o;
        for (int j = 0; j < nl2.size(); j++) {
          if (nl2.getVal(j) instanceof String) {
            nl2.setVal(j, decode((String) nl2.getVal(j)));
          }
        }
      } else {
        // System.out.println("unknown type " +
        // o.getClass().getCanonicalName());
      }
    } else {
      if (o instanceof NamedList) {
        nl.setVal(i, decode((NamedList<Object>) o));
      } else if (o instanceof ArrayList) {
        nl.setVal(i, decode((ArrayList<Object>) o));
      }
    }
  }
  return nl;
}
 
Example 6
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;
}