Java Code Examples for org.apache.solr.common.util.SimpleOrderedMap#get()

The following examples show how to use org.apache.solr.common.util.SimpleOrderedMap#get() . 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: SuggesterResponse.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"unchecked", "rawtypes"})
public SuggesterResponse(Map<String, NamedList<Object>> suggestInfo) {
  for (Map.Entry<String, NamedList<Object>> entry : suggestInfo.entrySet()) {
    SimpleOrderedMap suggestionsNode = (SimpleOrderedMap) entry.getValue().getVal(0);
    List<SimpleOrderedMap> suggestionListToParse;
    List<Suggestion> suggestionList = new LinkedList<>();
    if (suggestionsNode != null) {

      suggestionListToParse = (List<SimpleOrderedMap>) 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);
    }
  }
}
 
Example 2
Source File: TolerantUpdateProcessorTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void assertAddsSucceedWithErrors(String chain,
                                         final Collection<SolrInputDocument> docs,
                                         SolrParams requestParams, 
                                         String... idsShouldFail) throws IOException {

  SolrQueryResponse response = add(chain, requestParams, docs);
  
  @SuppressWarnings("unchecked")
  List<SimpleOrderedMap<String>> errors = (List<SimpleOrderedMap<String>>)
    response.getResponseHeader().get("errors");
  assertNotNull(errors);

  assertEquals("number of errors", idsShouldFail.length, errors.size());
  
  Set<String> addErrorIdsExpected = new HashSet<String>(Arrays.asList(idsShouldFail));

  for (SimpleOrderedMap<String> err : errors) {
    assertEquals("this method only expects 'add' errors", "ADD", err.get("type"));
    
    String id = err.get("id");
    assertNotNull("null err id", id);
    assertTrue("unexpected id", addErrorIdsExpected.contains(id));

  }
}
 
Example 3
Source File: UniqueAgg.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public void merge(Object facetResult, Context mcontext) {
  SimpleOrderedMap map = (SimpleOrderedMap)facetResult;
  long unique = ((Number)map.get(UNIQUE)).longValue();
  sumUnique += unique;

  int valsListed = 0;
  List vals = (List) map.get(VALS);
  if (vals != null) {
    if (values == null) {
      values = new HashSet<>(vals.size()*4);
    }
    values.addAll(vals);
    valsListed = vals.size();
    sumAdded += valsListed;
  }

  shardsMissingSum += unique - valsListed;
  shardsMissingMax = Math.max(shardsMissingMax, unique - valsListed);
  // TODO: somehow get & use the count in the bucket?
}
 
Example 4
Source File: FacetFieldMerger.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public void merge(Object facetResult, Context mcontext) {
  SimpleOrderedMap map = (SimpleOrderedMap)facetResult;
  long numBuckets = ((Number)map.get("numBuckets")).longValue();
  sumBuckets += numBuckets;

  List vals = (List)map.get("vals");
  if (vals != null) {
    if (values == null) {
      values = new HashSet<>(vals.size()*4);
    }
    values.addAll(vals);
    if (numBuckets > values.size()) {
      shardsTruncatedSum += numBuckets - values.size();
    }
  } else {
    shardsMissingSum += numBuckets;
  }
}
 
Example 5
Source File: SchemaTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private static void assertFailedSchemaResponse(ThrowingRunnable runnable, String expectedErrorMessage) {
  BaseHttpSolrClient.RemoteExecutionException e = expectThrows(BaseHttpSolrClient.RemoteExecutionException.class, runnable);
  @SuppressWarnings({"rawtypes"})
  SimpleOrderedMap errorMap = (SimpleOrderedMap)e.getMetaData().get("error");
  assertEquals("org.apache.solr.api.ApiBag$ExceptionWithErrObject",
      ((NamedList)errorMap.get("metadata")).get("error-class"));
  @SuppressWarnings({"rawtypes"})
  List details = (List)errorMap.get("details");
  assertTrue(((List)((Map)details.get(0)).get("errorMessages")).get(0).toString().contains(expectedErrorMessage));
}
 
Example 6
Source File: FacetRequestSortedMerger.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"unchecked", "rawtypes"})
public void mergeBucketList(List<SimpleOrderedMap> bucketList, Context mcontext) {
  for (SimpleOrderedMap bucketRes : bucketList) {
    Comparable bucketVal = (Comparable)bucketRes.get("val");
    FacetBucket bucket = buckets.get(bucketVal);
    if (bucket == null) {
      bucket = newBucket(bucketVal, mcontext);
      buckets.put(bucketVal, bucket);
    }
    bucket.mergeBucket( bucketRes , mcontext );
  }
}
 
Example 7
Source File: ToleratedUpdateError.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** 
 * returns a ToleratedUpdateError instance from the data in this Map 
 * @see #getSimpleMap
 */
public static ToleratedUpdateError parseMap(SimpleOrderedMap<String> data) {
  final String id = data.get(ID);
  final String message = data.get("message");
  final String t = data.get("type");
  if (null == t || null == id || null == message) {
    throw new SolrException(ErrorCode.SERVER_ERROR, "Map does not represent a ToleratedUpdateError, must contain 'type', 'id', and 'message'");
  }
  try {
    return new ToleratedUpdateError(CmdType.valueOf(t), id, message);
  } catch (IllegalArgumentException iae) {
    throw new SolrException(ErrorCode.SERVER_ERROR, "Invalid type for ToleratedUpdateError: " + t, iae);
  }
}
 
Example 8
Source File: ConfigSetsHandler.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"unchecked"})
private void handleResponse(String operation, ZkNodeProps m,
                            SolrQueryResponse rsp, long timeout) throws KeeperException, InterruptedException {
  long time = System.nanoTime();

  QueueEvent event = coreContainer.getZkController()
      .getOverseerConfigSetQueue()
      .offer(Utils.toJSON(m), timeout);
  if (event.getBytes() != null) {
    SolrResponse response = OverseerSolrResponseSerializer.deserialize(event.getBytes());
    rsp.getValues().addAll(response.getResponse());
    @SuppressWarnings({"rawtypes"})
    SimpleOrderedMap exp = (SimpleOrderedMap) response.getResponse().get("exception");
    if (exp != null) {
      Integer code = (Integer) exp.get("rspCode");
      rsp.setException(new SolrException(code != null && code != -1 ? ErrorCode.getErrorCode(code) : ErrorCode.SERVER_ERROR, (String) exp.get("msg")));
    }
  } else {
    if (System.nanoTime() - time >= TimeUnit.NANOSECONDS.convert(timeout, TimeUnit.MILLISECONDS)) {
      throw new SolrException(ErrorCode.SERVER_ERROR, operation
          + " the configset time out:" + timeout / 1000 + "s");
    } else if (event.getWatchedEvent() != null) {
      throw new SolrException(ErrorCode.SERVER_ERROR, operation
          + " the configset error [Watcher fired on path: "
          + event.getWatchedEvent().getPath() + " state: "
          + event.getWatchedEvent().getState() + " type "
          + event.getWatchedEvent().getType() + "]");
    } else {
      throw new SolrException(ErrorCode.SERVER_ERROR, operation
          + " the configset unknown case");
    }
  }
}
 
Example 9
Source File: RangeFacetRequest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Accumulates an individual facet_ranges count from a shard into global counts.
 * <p>
 * The implementation below uses the first encountered shard's
 * facet_ranges as the basis for subsequent shards' data to be merged.
 *
 * @param rangeFromShard the facet_ranges response from a shard
 */
public void mergeContributionFromShard(SimpleOrderedMap<Object> rangeFromShard) {
  if (rangeFacet == null) {
    rangeFacet = rangeFromShard;
    return;
  }

  @SuppressWarnings("unchecked")
  NamedList<Integer> shardFieldValues
      = (NamedList<Integer>) rangeFromShard.get("counts");

  @SuppressWarnings("unchecked")
  NamedList<Integer> existFieldValues
      = (NamedList<Integer>) rangeFacet.get("counts");

  for (Map.Entry<String, Integer> existPair : existFieldValues) {
    final String key = existPair.getKey();
    // can be null if inconsistencies in shards responses
    Integer newValue = shardFieldValues.get(key);
    if (null != newValue) {
      Integer oldValue = existPair.getValue();
      existPair.setValue(oldValue + newValue);
    }
  }

  // merge facet.other=before/between/after/all if they exist
  for (FacetParams.FacetRangeOther otherKey : FacetParams.FacetRangeOther.values()) {
    if (otherKey == FacetParams.FacetRangeOther.NONE) continue;

    String name = otherKey.toString();
    Integer shardValue = (Integer) rangeFromShard.get(name);
    if (shardValue != null && shardValue > 0) {
      Integer existingValue = (Integer) rangeFacet.get(name);
      // shouldn't be null
      int idx = rangeFacet.indexOf(name, 0);
      rangeFacet.setVal(idx, existingValue + shardValue);
    }
  }
}
 
Example 10
Source File: CustomHighlightComponentTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
protected void addHighlights(Object[] objArr, Object obj, Map<Object, ShardDoc> resultIds) {
  @SuppressWarnings({"rawtypes"})
  SimpleOrderedMap[] mapArr = (SimpleOrderedMap[])objArr;
  @SuppressWarnings({"unchecked", "rawtypes"})
  final ArrayList<SimpleOrderedMap> hlMaps = (ArrayList<SimpleOrderedMap>)obj;
  for (@SuppressWarnings({"rawtypes"})SimpleOrderedMap hlMap : hlMaps) {
    String id = (String)hlMap.get(id_key);
    ShardDoc sdoc = resultIds.get(id);
    int idx = sdoc.positionInResponse;
    mapArr[idx] = hlMap;
  }
}
 
Example 11
Source File: ServiceSolrClient.java    From ranger with Apache License 2.0 5 votes vote down vote up
public List<String> getFieldList(String collection,
		List<String> ignoreFieldList) throws Exception {
	// TODO: Best is to get the collections based on the collection value
	// which could contain wild cards
	String queryStr = "";
	if (collection != null && !collection.isEmpty()) {
		queryStr += "/" + collection;
	}
	queryStr += "/schema/fields";
	SolrQuery query = new SolrQuery();
	query.setRequestHandler(queryStr);
	QueryRequest req = new QueryRequest(query);
	String decPassword = getDecryptedPassword();
	if (username != null && decPassword != null) {
	    req.setBasicAuthCredentials(username, decPassword);
	}
	QueryResponse response = req.process(solrClient);

	List<String> fieldList = new ArrayList<String>();
	if (response != null && response.getStatus() == 0) {
		@SuppressWarnings("unchecked")
		List<SimpleOrderedMap<String>> fields = (ArrayList<SimpleOrderedMap<String>>) response
				.getResponse().get("fields");
		for (SimpleOrderedMap<String> fmap : fields) {
			String fieldName = fmap.get("name");
			if (ignoreFieldList == null
					|| !ignoreFieldList.contains(fieldName)) {
				fieldList.add(fieldName);
			}
		}
	} else {
		LOG.error("Error getting fields for collection=" + collection
				+ ", response=" + response);
	}
	return fieldList;
}
 
Example 12
Source File: TestTolerantUpdateProcessorCloud.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** Asserts that the UpdateResponse contains the specified expectedErrs and no others */
public static void assertUpdateTolerantErrors(String assertionMsgPrefix,
                                              UpdateResponse response,
                                              ExpectedErr... expectedErrs) {
  @SuppressWarnings("unchecked")
  List<SimpleOrderedMap<String>> errors = (List<SimpleOrderedMap<String>>)
    response.getResponseHeader().get("errors");
  
  assertNotNull(assertionMsgPrefix + ": Null errors: " + response.toString(), errors);
  assertEquals(assertionMsgPrefix + ": Num error ids: " + errors.toString(),
               expectedErrs.length, errors.size());

  for (SimpleOrderedMap<String> err : errors) {
    String assertErrPre = assertionMsgPrefix + ": " + err.toString();

    String id = err.get("id");
    assertNotNull(assertErrPre + " ... null id", id);
    String type = err.get("type");
    assertNotNull(assertErrPre + " ... null type", type);
    String message = err.get("message");
    assertNotNull(assertErrPre + " ... null message", message);

    // inefficient scan, but good nough for the size of sets we're dealing with
    boolean found = false;
    for (ExpectedErr expected : expectedErrs) {
      if (expected.type.equals(type) && expected.id.equals(id)
          && (null == expected.msgSubStr || message.contains(expected.msgSubStr))) {
        found = true;
        break;
      }
    }
    assertTrue(assertErrPre + " ... unexpected err in: " + response.toString(), found);

  }
}
 
Example 13
Source File: ResolveAnalyzerByNameTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void checkTokenFilterNames(SimpleOrderedMap<Object> analyzerProps, String[] names) {
  @SuppressWarnings({"unchecked"})
  List<SimpleOrderedMap<Object>> tokenFilterProps = (List<SimpleOrderedMap<Object>>)analyzerProps.get("filters");
  assertEquals(names.length, tokenFilterProps.size());
  for (int i = 0; i < names.length; i++) {
    assertNull(tokenFilterProps.get(i).get("class"));
    assertEquals(names[i], tokenFilterProps.get(i).get("name"));
  }
}
 
Example 14
Source File: ResolveAnalyzerByNameTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void checkCharFilterNames(SimpleOrderedMap<Object> analyzerProps, String[] names) {
  @SuppressWarnings({"unchecked"})
  List<SimpleOrderedMap<Object>> charFilterProps = (List<SimpleOrderedMap<Object>>)analyzerProps.get("charFilters");
  assertEquals(names.length, charFilterProps.size());
  for (int i = 0; i < names.length; i++) {
    assertNull(charFilterProps.get(i).get("class"));
    assertEquals(names[i], charFilterProps.get(i).get("name"));
  }
}
 
Example 15
Source File: FacetFieldAdapter.java    From semantic-knowledge-graph with Apache License 2.0 5 votes vote down vote up
public SimpleOrderedMap<String> getMapValue(SimpleOrderedMap<Object> bucket) {
    SimpleOrderedMap<String> result = null;
    if(facetFieldExtension != null && !facetFieldExtension.equals("")) {
        result = new SimpleOrderedMap<>();
        String value = (String) bucket.get("val");
        String[] facetFieldKeys = facetFieldExtension.split(facetFieldDelimiter);
        String[] facetFieldValues = value.split("\\"+facetFieldValueDelimiter);
        for (int i = 0; i < facetFieldKeys.length && i < facetFieldValues.length; ++i) {
            if(!facetFieldValues.equals("")) {
                result.add(facetFieldKeys[i], facetFieldValues[i]);
            }
        }
    }
    return result;
}
 
Example 16
Source File: FacetFieldAdapter.java    From semantic-knowledge-graph with Apache License 2.0 5 votes vote down vote up
public String getStringValue(SimpleOrderedMap<Object> bucket)
{
    SimpleOrderedMap<String> mapValues = getMapValue(bucket);
    if(mapValues == null)
    {
        return ((String) bucket.get("val")).replace(facetFieldValueDelimiter, " ");
    }
    return mapValues.get(this.facetFieldKey);
}
 
Example 17
Source File: SolrTarget04.java    From datacollector with Apache License 2.0 5 votes vote down vote up
private void getRequiredFieldNames() throws SolrServerException, IOException {
  QueryRequest request = new QueryRequest();
  request.setPath(SCHEMA_PATH);
  NamedList queryResponse = solrClient.request(request);

  SimpleOrderedMap simpleOrderedMap = (SimpleOrderedMap) queryResponse.get("schema");
  ArrayList<SimpleOrderedMap> fields = (ArrayList<SimpleOrderedMap>) simpleOrderedMap.get("fields");

  for (SimpleOrderedMap field : fields) {
    if (field.get(REQUIRED) != null && field.get(REQUIRED).equals(true)) {
      requiredFieldNamesMap.add(field.get(NAME).toString());
    }
  }
}
 
Example 18
Source File: QueryComponent.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
protected void returnFields(ResponseBuilder rb, ShardRequest sreq) {
  // Keep in mind that this could also be a shard in a multi-tiered system.
  // TODO: if a multi-tiered system, it seems like some requests
  // could/should bypass middlemen (like retrieving stored fields)
  // TODO: merge fsv to if requested

  if ((sreq.purpose & ShardRequest.PURPOSE_GET_FIELDS) != 0) {
    boolean returnScores = (rb.getFieldFlags() & SolrIndexSearcher.GET_SCORES) != 0;

    String keyFieldName = rb.req.getSchema().getUniqueKeyField().getName();
    boolean removeKeyField = !rb.rsp.getReturnFields().wantsField(keyFieldName);
    if (rb.rsp.getReturnFields().getFieldRenames().get(keyFieldName) != null) {
      // if id was renamed we need to use the new name
      keyFieldName = rb.rsp.getReturnFields().getFieldRenames().get(keyFieldName);
    }

    for (ShardResponse srsp : sreq.responses) {
      if (srsp.getException() != null) {
        // Don't try to get the documents if there was an exception in the shard
        if(rb.req.getParams().getBool(ShardParams.SHARDS_INFO, false)) {
          @SuppressWarnings("unchecked")
          NamedList<Object> shardInfo = (NamedList<Object>) rb.rsp.getValues().get(ShardParams.SHARDS_INFO);
          @SuppressWarnings("unchecked")
          SimpleOrderedMap<Object> nl = (SimpleOrderedMap<Object>) shardInfo.get(srsp.getShard());
          if (nl.get("error") == null) {
            // Add the error to the shards info section if it wasn't added before
            Throwable t = srsp.getException();
            if(t instanceof SolrServerException) {
              t = ((SolrServerException)t).getCause();
            }
            nl.add("error", t.toString() );
            StringWriter trace = new StringWriter();
            t.printStackTrace(new PrintWriter(trace));
            nl.add("trace", trace.toString() );
          }
        }
        
        continue;
      }
      {
        NamedList<?> responseHeader = (NamedList<?>)srsp.getSolrResponse().getResponse().get("responseHeader");
        if (Boolean.TRUE.equals(responseHeader.getBooleanArg(SolrQueryResponse.RESPONSE_HEADER_PARTIAL_RESULTS_KEY))) {
          rb.rsp.getResponseHeader().asShallowMap()
             .put(SolrQueryResponse.RESPONSE_HEADER_PARTIAL_RESULTS_KEY, Boolean.TRUE);
        }
      }
      SolrDocumentList docs = (SolrDocumentList) srsp.getSolrResponse().getResponse().get("response");
      for (SolrDocument doc : docs) {
        Object id = doc.getFieldValue(keyFieldName);
        ShardDoc sdoc = rb.resultIds.get(id.toString());
        if (sdoc != null) {
          if (returnScores) {
            doc.setField("score", sdoc.score);
          } else {
            // Score might have been added (in createMainQuery) to shard-requests (and therefore in shard-response-docs)
            // Remove score if the outer request did not ask for it returned
            doc.remove("score");
          }
          if (removeKeyField) {
            doc.removeFields(keyFieldName);
          }
          rb.getResponseDocs().set(sdoc.positionInResponse, doc);
        }
      }
    }
  }
}
 
Example 19
Source File: FeaturesRequestHandlerTest.java    From ltr4l with Apache License 2.0 4 votes vote down vote up
@Test
public void test() throws Exception {
  assertU(adoc("id", "1", "title", "this is title", "body", "this is body"));
  assertU(commit());

  /*
   * command=extract
   */
  SolrQueryRequest req = req("command", "extract", "conf", "ltr_features.conf");
  FeaturesRequestHandler handler = new FeaturesRequestHandler();
  SimpleOrderedMap<Object> results = new SimpleOrderedMap<Object>();
  handler.handleExtract(req,
          makeStream("{\n" +
                  "  \"idField\": \"id\",\n" +
                  "  \"queries\": [\n" +
                  "    {\n" +
                  "      \"qid\": 101,\n" +
                  "      \"query\": \"this\",\n" +
                  "      \"docs\": [ \"1\" ]\n" +
                  "    },\n" +
                  "    {\n" +
                  "      \"qid\": 102,\n" +
                  "      \"query\": \"title\",\n" +
                  "      \"docs\": [ \"1\" ]\n" +
                  "    },\n" +
                  "    {\n" +
                  "      \"qid\": 103,\n" +
                  "      \"query\": \"body\",\n" +
                  "      \"docs\": [ \"1\" ]\n" +
                  "    }\n" +
                  "  ]\n" +
                  "}\n"), results);

  long procId = (Long)results.get("procId");

  req.close();

  /*
   * command=progress
   */
  SolrQueryRequest req2 = req("command", "progress", "procId", Long.toString(procId));
  results = new SimpleOrderedMap<Object>();
  handler.handleProgress(req2, results);

  long procId2 = (Long)results.get("procId");
  assertEquals(procId, procId2);
  assertNotNull(results.get("done?"));
  assertNotNull(results.get("progress"));

  req2.close();

  /*
   * command=delete
   */
  SolrQueryRequest req3 = req("command", "delete", "procId", Long.toString(procId));
  results = new SimpleOrderedMap<Object>();
  handler.handleDelete(req3, results);

  long procId3 = (Long)results.get("procId");
  assertEquals(procId, procId3);
  assertNotNull(results.get("result"));

  req3.close();

  /*
   * command=progress => procId is no longer valid
   */
  SolrQueryRequest req4 = req("command", "progress", "procId", Long.toString(procId));
  results = new SimpleOrderedMap<Object>();
  try {
    handler.handleProgress(req4, results);
    fail("this method should fail because the process is no longer valid");
  }
  catch (SolrException expected){}
  finally {
    req4.close();
  }
}
 
Example 20
Source File: ResolveAnalyzerByNameTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private void checkTokenizerName(SimpleOrderedMap<Object> analyzerProps, String name) {
  @SuppressWarnings({"unchecked"})
  SimpleOrderedMap<Object> tokenizerProps = (SimpleOrderedMap<Object>)analyzerProps.get("tokenizer");
  assertNull(tokenizerProps.get("class"));
  assertEquals(name, tokenizerProps.get("name"));
}