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

The following examples show how to use org.apache.solr.common.util.NamedList#indexOf() . 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: CloneFieldUpdateProcessorFactory.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void init(@SuppressWarnings({"rawtypes"})NamedList args) {

  // high level (loose) check for which type of config we have.
  // 
  // individual init methods do more strict syntax checking
  if (0 <= args.indexOf(SOURCE_PARAM, 0) && 0 <= args.indexOf(DEST_PARAM, 0) ) {
    initSourceSelectorSyntax(args);
  } else if (0 <= args.indexOf(PATTERN_PARAM, 0) && 0 <= args.indexOf(REPLACEMENT_PARAM, 0)) {
    initSimpleRegexReplacement(args);
  } else {
    throw new SolrException(SERVER_ERROR, "A combination of either '" + SOURCE_PARAM + "' + '"+
                            DEST_PARAM + "', or '" + REPLACEMENT_PARAM + "' + '" +
                            PATTERN_PARAM + "' init params are mandatory");
  }
  
  if (0 < args.size()) {
    throw new SolrException(SERVER_ERROR,
        "Unexpected init param(s): '" +
            args.getName(0) + "'");
  }

  super.init(args);
}
 
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: TestRequestStatusCollectionAPI.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private int numResponsesCompleted(NamedList<Object> response) {
  int sum=0;
  for (String key: Arrays.asList("success","failure")) {
    NamedList<Object> allStatuses = (NamedList<Object>)response.get(key);
    if (allStatuses!=null) {
      for (Map.Entry<String, Object> tuple: allStatuses) {
        NamedList<Object> statusResponse = (NamedList<Object>) tuple.getValue();
        if (statusResponse.indexOf("STATUS",0)>=0) {
          sum+=1;
        }
      }
    }
  }
  return sum;
}
 
Example 4
Source File: TermVectorComponent.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void finishStage(ResponseBuilder rb) {
  if (rb.stage == ResponseBuilder.STAGE_GET_FIELDS) {
    
    NamedList<Object> termVectorsNL = new NamedList<>();

    @SuppressWarnings({"unchecked", "rawtypes"})
    Map.Entry<String, Object>[] arr = new NamedList.NamedListEntry[rb.resultIds.size()];

    for (ShardRequest sreq : rb.finished) {
      if ((sreq.purpose & ShardRequest.PURPOSE_GET_FIELDS) == 0 || !sreq.params.getBool(COMPONENT_NAME, false)) {
        continue;
      }
      for (ShardResponse srsp : sreq.responses) {
        @SuppressWarnings({"unchecked"})
        NamedList<Object> nl = (NamedList<Object>)srsp.getSolrResponse().getResponse().get(TERM_VECTORS);

        // Add metadata (that which isn't a uniqueKey value):
        Object warningsNL = nl.get(TV_KEY_WARNINGS);
        // assume if that if warnings is already present; we don't need to merge.
        if (warningsNL != null && termVectorsNL.indexOf(TV_KEY_WARNINGS, 0) < 0) {
          termVectorsNL.add(TV_KEY_WARNINGS, warningsNL);
        }

        // UniqueKey data
        SolrPluginUtils.copyNamedListIntoArrayByDocPosInResponse(nl, rb.resultIds, arr);
      }
    }
    // remove nulls in case not all docs were able to be retrieved
    SolrPluginUtils.removeNulls(arr, termVectorsNL);
    rb.rsp.add(TERM_VECTORS, termVectorsNL);
  }
}
 
Example 5
Source File: TrackingUpdateProcessorFactory.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void init(@SuppressWarnings({"rawtypes"})NamedList args) {
  if (args != null && args.indexOf("group",0) >= 0) {
    group = (String) args.get("group");
    log.debug("Init URP, group '{}'", group);
  } else {
    log.warn("TrackingUpdateProcessorFactory initialized without group configuration, using 'default' but this group is shared" +
        "across the entire VM and guaranteed to have unpredictable behavior if used by more than one test");
  }
}
 
Example 6
Source File: OpenNLPExtractNamedEntitiesUpdateProcessorFactory.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void init(@SuppressWarnings({"rawtypes"})NamedList args) {

  // high level (loose) check for which type of config we have.
  //
  // individual init methods do more strict syntax checking
  if (0 <= args.indexOf(SOURCE_PARAM, 0) && 0 <= args.indexOf(DEST_PARAM, 0) ) {
    initSourceSelectorSyntax(args);
  } else if (0 <= args.indexOf(PATTERN_PARAM, 0) && 0 <= args.indexOf(REPLACEMENT_PARAM, 0)) {
    initSimpleRegexReplacement(args);
  } else {
    throw new SolrException(SERVER_ERROR, "A combination of either '" + SOURCE_PARAM + "' + '"+
        DEST_PARAM + "', or '" + REPLACEMENT_PARAM + "' + '" +
        PATTERN_PARAM + "' init params are mandatory");
  }

  Object modelParam = args.remove(MODEL_PARAM);
  if (null == modelParam) {
    throw new SolrException(SERVER_ERROR, "Missing required init param '" + MODEL_PARAM + "'");
  }
  if ( ! (modelParam instanceof CharSequence)) {
    throw new SolrException(SERVER_ERROR, "Init param '" + MODEL_PARAM + "' must be a <str>");
  }
  modelFile = modelParam.toString();

  Object analyzerFieldTypeParam = args.remove(ANALYZER_FIELD_TYPE_PARAM);
  if (null == analyzerFieldTypeParam) {
    throw new SolrException(SERVER_ERROR, "Missing required init param '" + ANALYZER_FIELD_TYPE_PARAM + "'");
  }
  if ( ! (analyzerFieldTypeParam instanceof CharSequence)) {
    throw new SolrException(SERVER_ERROR, "Init param '" + ANALYZER_FIELD_TYPE_PARAM + "' must be a <str>");
  }
  analyzerFieldType = analyzerFieldTypeParam.toString();

  if (0 < args.size()) {
    throw new SolrException(SERVER_ERROR, "Unexpected init param(s): '" + args.getName(0) + "'");
  }

  super.init(args);
}
 
Example 7
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;
}
 
Example 8
Source File: TestCloudJSONFacetSKG.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/** 
 * Recursive helper method that walks the actual facet response, comparing the SKG results to 
 * the expected output based on the equivalent filters generated from the original TermFacet.
 */
@SuppressWarnings({"unchecked"})
private void assertFacetSKGsAreCorrect(final AtomicInteger maxBucketsToCheck,
                                       final Map<String,TermFacet> expected,
                                       final SolrParams baseParams,
                                       @SuppressWarnings({"rawtypes"})final NamedList actualFacetResponse) throws SolrServerException, IOException {

  for (Map.Entry<String,TermFacet> entry : expected.entrySet()) {
    final String facetKey = entry.getKey();
    final TermFacet facet = entry.getValue();
    
    @SuppressWarnings({"rawtypes"})
    final NamedList results = (NamedList) actualFacetResponse.get(facetKey);
    assertNotNull(facetKey + " key missing from: " + actualFacetResponse, results);

    if (null != results.get("allBuckets")) {
      // if the response includes an allBuckets bucket, then there must not be an skg value
      
      // 'skg' key must not exist in th allBuckets bucket
      assertEquals(facetKey + " has skg in allBuckets: " + results.get("allBuckets"),
                   Collections.emptyList(),
                   ((NamedList)results.get("allBuckets")).getAll("skg"));
    }
    @SuppressWarnings({"rawtypes"})
    final List<NamedList> buckets = (List<NamedList>) results.get("buckets");
    assertNotNull(facetKey + " has null buckets: " + actualFacetResponse, buckets);

    if (buckets.isEmpty()) {
      // should only happen if the background query does not match any docs with field X
      final long docsWithField = getNumFound(params("_trace", "noBuckets",
                                                    "rows", "0",
                                                    "q", facet.field+":[* TO *]",
                                                    "fq", baseParams.get("back")));

      assertEquals(facetKey + " has no buckets, but docs in background exist with field: " + facet.field,
                   0, docsWithField);
    }

    // NOTE: it's important that we do this depth first -- not just because it's the easiest way to do it,
    // but because it means that our maxBucketsToCheck will ensure we do a lot of deep sub-bucket checking,
    // not just all the buckets of the top level(s) facet(s)
    for (@SuppressWarnings({"rawtypes"})NamedList bucket : buckets) {
      final String fieldVal = bucket.get("val").toString(); // int or stringified int

      verifySKGResults(facetKey, facet, baseParams, fieldVal, bucket);
      if (maxBucketsToCheck.decrementAndGet() <= 0) {
        return;
      }
      
      final SolrParams verifyParams = SolrParams.wrapAppended(baseParams,
                                                              params("fq", facet.field + ":" + fieldVal));
      
      // recursively check subFacets
      if (! facet.subFacets.isEmpty()) {
        assertFacetSKGsAreCorrect(maxBucketsToCheck, facet.subFacets, verifyParams, bucket);
      }
    }
  }
  
  { // make sure we don't have any facet keys we don't expect
    // a little hackish because subfacets have extra keys...
    @SuppressWarnings({"rawtypes"})
    final LinkedHashSet expectedKeys = new LinkedHashSet(expected.keySet());
    expectedKeys.add("count");
    if (0 <= actualFacetResponse.indexOf("val",0)) {
      expectedKeys.add("val");
      expectedKeys.add("skg");
    }
    assertEquals("Unexpected keys in facet response",
                 expectedKeys, actualFacetResponse.asShallowMap().keySet());
  }
}
 
Example 9
Source File: DocExpirationUpdateProcessorFactory.java    From lucene-solr with Apache License 2.0 3 votes vote down vote up
private String removeArgStr(@SuppressWarnings({"rawtypes"})final NamedList args,
                            final String arg, final String def,
                            final String errMsg) {

  if (args.indexOf(arg,0) < 0) return def;

  Object tmp = args.remove(arg);
  if (null == tmp) return null;

  if (tmp instanceof String) return tmp.toString();

  throw confErr(arg + " " + errMsg);
}