Java Code Examples for org.apache.solr.client.solrj.SolrQuery#setFields()

The following examples show how to use org.apache.solr.client.solrj.SolrQuery#setFields() . 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: MCROAIQuerySetResolver.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
private SolrQuery getQuery() {
    SolrQuery solrQuery = new SolrQuery();
    // query
    String idQuery = getResult().stream()
        .map(getIdentifier())
        .map(MCRSolrUtils::escapeSearchValue)
        .collect(Collectors.joining(" OR ", "id:(", ")"));
    solrQuery.setQuery(idQuery);
    solrQuery.setFilterQueries(query);
    solrQuery.setFields("id");
    solrQuery.setRows(getResult().size());
    // request handler
    solrQuery.setRequestHandler(
        MCRConfiguration2.getString(getConfigPrefix() + "Search.RequestHandler").orElse("/select"));
    return solrQuery;
}
 
Example 2
Source File: SolrLookingBlurServerTest.java    From incubator-retired-blur with Apache License 2.0 6 votes vote down vote up
@Test
public void fieldsRequestsShouldTurnIntoSelectors() throws Exception,
    TException {

  String table = "fieldsRequestsShouldTurnIntoSelectors";
  SolrServer server = TestTableCreator.newTable(table)
      .withRowCount(1).withRecordsPerRow(2)
      .withRecordColumns("fam.value", "fam.mvf").create();

  SolrQuery query = new SolrQuery("value0-0");
  query.setFields("fam.value");
  QueryResponse response = server.query(query);

  assertEquals("We should get our doc back for a valid test.", 1l, response.getResults().getNumFound());

  SolrDocument docResult = response.getResults().get(0);

  assertEquals("value0-0", docResult.getFieldValue("fam.value"));
  assertNull("We shouldn't get this one back since it wasnt in our fields.", docResult.getFieldValues("fam.mvf"));

  removeTable(table);
}
 
Example 3
Source File: DistributedDebugComponentTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testTolerantSearch() throws SolrServerException, IOException {
  String badShard = DEAD_HOST_1;
  SolrQuery query = new SolrQuery();
  query.setQuery("*:*");
  query.set("debug",  "true");
  query.set("distrib", "true");
  query.setFields("id", "text");
  query.set("shards", shard1 + "," + shard2 + "," + badShard);

  // verify that the request would fail if shards.tolerant=false
  ignoreException("Server refused connection");
  expectThrows(SolrException.class, () -> collection1.query(query));

  query.set(ShardParams.SHARDS_TOLERANT, "true");
  QueryResponse response = collection1.query(query);
  assertTrue((Boolean)response.getResponseHeader().get(SolrQueryResponse.RESPONSE_HEADER_PARTIAL_RESULTS_KEY));
  @SuppressWarnings("unchecked")
  NamedList<String> badShardTrack =
          (((NamedList<NamedList<NamedList<String>>>)response.getDebugMap().get("track")).get("EXECUTE_QUERY")).get(badShard);
  assertEquals("Unexpected response size for shard", 1, badShardTrack.size());
  Entry<String, String> exception = badShardTrack.iterator().next();
  assertEquals("Expected key 'Exception' not found", "Exception", exception.getKey());
  assertNotNull("Exception message should not be null", exception.getValue());
  unIgnoreException("Server refused connection");
}
 
Example 4
Source File: DistributedVersionInfoTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
protected long getVersionFromIndex(Replica replica, String docId) throws IOException, SolrServerException {
  Long vers = null;
  String queryStr = (docId != null) ? "id:" + docId : "_version_:[0 TO *]";
  SolrQuery query = new SolrQuery(queryStr);
  query.setRows(1);
  query.setFields("id", "_version_");
  query.addSort(new SolrQuery.SortClause("_version_", SolrQuery.ORDER.desc));
  query.setParam("distrib", false);

  try (SolrClient client = getHttpSolrClient(replica.getCoreUrl())) {
    QueryResponse qr = client.query(query);
    SolrDocumentList hits = qr.getResults();
    if (hits.isEmpty())
      fail("No results returned from query: "+query);

    vers = (Long) hits.get(0).getFirstValue("_version_");
  }

  if (vers == null)
    fail("Failed to get version using query " + query + " from " + replica.getCoreUrl());

  return vers.longValue();
}
 
Example 5
Source File: SegmentTerminateEarlyTestState.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
void queryTimestampAscendingSegmentTerminateEarlyYes(CloudSolrClient cloudSolrClient) throws Exception {
  TestSegmentSorting.assertFalse(minTimestampDocKeys.isEmpty());
  TestSegmentSorting.assertTrue("numDocs="+numDocs+" is not even", (numDocs%2)==0);
  final Long oddFieldValue = (long) (minTimestampDocKeys.iterator().next().intValue() % 2);
  final SolrQuery query = new SolrQuery(ODD_FIELD +":"+oddFieldValue);
  query.setSort(TIMESTAMP_FIELD, SolrQuery.ORDER.asc); // a sort order that is _not_ compatible with the merge sort order
  query.setFields(KEY_FIELD, ODD_FIELD, TIMESTAMP_FIELD);
  query.setRows(1);
  query.set(CommonParams.SEGMENT_TERMINATE_EARLY, true);
  final QueryResponse rsp = cloudSolrClient.query(query);
  // check correctness of the results count
  TestSegmentSorting.assertEquals("numFound", numDocs/2, rsp.getResults().getNumFound());
  // check correctness of the first result
  if (rsp.getResults().getNumFound() > 0) {
    final SolrDocument solrDocument0 = rsp.getResults().get(0);
    final Integer idAsInt = Integer.parseInt(solrDocument0.getFieldValue(KEY_FIELD).toString());
    TestSegmentSorting.assertTrue
      (KEY_FIELD +"="+idAsInt+" of ("+solrDocument0+") is not in minTimestampDocKeys("+minTimestampDocKeys+")",
       minTimestampDocKeys.contains(idAsInt));
    TestSegmentSorting.assertEquals(ODD_FIELD, oddFieldValue, solrDocument0.getFieldValue(ODD_FIELD));
  }
  // check segmentTerminatedEarly flag
  TestSegmentSorting.assertNotNull("responseHeader.segmentTerminatedEarly missing in "+rsp.getResponseHeader(),
      rsp.getResponseHeader().get(SolrQueryResponse.RESPONSE_HEADER_SEGMENT_TERMINATED_EARLY_KEY));
  // segmentTerminateEarly cannot be used with incompatible sort orders
  TestSegmentSorting.assertTrue("responseHeader.segmentTerminatedEarly missing/true in "+rsp.getResponseHeader(),
      Boolean.FALSE.equals(rsp.getResponseHeader().get(SolrQueryResponse.RESPONSE_HEADER_SEGMENT_TERMINATED_EARLY_KEY)));
}
 
Example 6
Source File: SolrDataSet.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
protected void initSolrConfiguration(JSONObject jsonConf, boolean resolveParams) {
	try {
		solrConfiguration = new SolrConfiguration();
		String address = getProp(SolrDataSetConstants.SOLR_BASE_ADDRESS, jsonConf, false, resolveParams);
		solrConfiguration.setUrl(address);
		String collection = getProp(SolrDataSetConstants.SOLR_COLLECTION, jsonConf, false, resolveParams);
		solrConfiguration.setCollection(collection);
		SolrQuery solrQuery = new SolrQuery();
		String query = getProp(SolrDataSetConstants.SOLR_QUERY, jsonConf, true, resolveParams);
		if (query == null || query.isEmpty()) {
			query = SOLR_DEFAULT_QUERY;
		}
		solrQuery.setQuery(query);
		String fieldList = getProp(SolrDataSetConstants.SOLR_FIELD_LIST, jsonConf, true, resolveParams);
		if (fieldList != null && !fieldList.trim().isEmpty()) {
			solrQuery.setFields(fieldList.split(","));
		}
		String solrFields = getProp(SolrDataSetConstants.SOLR_FIELDS, jsonConf, true, resolveParams);
		if (solrFields != null && !solrFields.trim().isEmpty()) {
			solrConfiguration.setSolrFields(solrFields);
		}

		List<Couple<String, String>> filterQueries = getListProp(SolrDataSetConstants.SOLR_FILTER_QUERY, jsonConf, true);
		if (filterQueries != null && !filterQueries.isEmpty()) {
			String[] array = new String[filterQueries.size()];
			for (int i = 0; i < array.length; i++) {
				array[i] = filterQueries.get(i).getFirst() + ":" + filterQueries.get(i).getSecond();
			}

			solrQuery.setFilterQueries(array);
		}
		solrQuery.setFacet(isFacet());
		solrConfiguration.setSolrQuery(solrQuery);
	} catch (JSONException e) {
		throw new ConfigurationException("Problems in configuration of solr query", e);
	}
}
 
Example 7
Source File: SolrDataSet.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
protected void initSolrConfiguration(JSONObject jsonConf, boolean resolveParams, UserProfile userProfile) {
	try {
		solrConfiguration = new SolrConfiguration();
		String address = getProp(SolrDataSetConstants.SOLR_BASE_ADDRESS, jsonConf, false, resolveParams);
		solrConfiguration.setUrl(address);
		String collection = getProp(SolrDataSetConstants.SOLR_COLLECTION, jsonConf, false, resolveParams);
		solrConfiguration.setCollection(collection);
		SolrQuery solrQuery = new SolrQuery();
		String query = getProp(SolrDataSetConstants.SOLR_QUERY, jsonConf, true, resolveParams);
		if (query == null || query.isEmpty()) {
			query = SOLR_DEFAULT_QUERY;
		}
		solrQuery.setQuery(query);
		String fieldList = getProp(SolrDataSetConstants.SOLR_FIELD_LIST, jsonConf, true, resolveParams);
		if (fieldList != null && !fieldList.trim().isEmpty()) {
			solrQuery.setFields(fieldList.split(","));
		}
		String solrFields = getProp(SolrDataSetConstants.SOLR_FIELDS, jsonConf, true, resolveParams);
		if (solrFields != null && !solrFields.trim().isEmpty()) {
			solrConfiguration.setSolrFields(solrFields);
		}

		List<Couple<String, String>> filterQueries = getListProp(SolrDataSetConstants.SOLR_FILTER_QUERY, jsonConf, true, userProfile);
		if (filterQueries != null && !filterQueries.isEmpty()) {
			String[] array = new String[filterQueries.size()];
			for (int i = 0; i < array.length; i++) {
				array[i] = filterQueries.get(i).getFirst() + ":" + filterQueries.get(i).getSecond();
			}

			solrQuery.setFilterQueries(array);
		}
		solrQuery.setFacet(isFacet());
		solrConfiguration.setSolrQuery(solrQuery);
	} catch (JSONException e) {
		throw new ConfigurationException("Problems in configuration of solr query", e);
	}
}
 
Example 8
Source File: SegmentTerminateEarlyTestState.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
void queryTimestampDescendingSegmentTerminateEarlyYesGrouped(CloudSolrClient cloudSolrClient) throws Exception {
  TestSegmentSorting.assertFalse(maxTimestampDocKeys.isEmpty());
  TestSegmentSorting.assertTrue("numDocs="+numDocs+" is not even", (numDocs%2)==0);
  final Long oddFieldValue = (long) (maxTimestampDocKeys.iterator().next().intValue() % 2);
  final SolrQuery query = new SolrQuery(ODD_FIELD +":"+oddFieldValue);
  query.setSort(TIMESTAMP_FIELD, SolrQuery.ORDER.desc);
  query.setFields(KEY_FIELD, ODD_FIELD, TIMESTAMP_FIELD);
  query.setRows(1);
  query.set(CommonParams.SEGMENT_TERMINATE_EARLY, true);
  TestSegmentSorting.assertTrue("numDocs="+numDocs+" is not quad-able", (numDocs%4)==0);
  query.add("group.field", QUAD_FIELD);
  query.set("group", true);
  final QueryResponse rsp = cloudSolrClient.query(query);
  // check correctness of the results count
  TestSegmentSorting.assertEquals("matches", numDocs/2, rsp.getGroupResponse().getValues().get(0).getMatches());
  // check correctness of the first result
  if (rsp.getGroupResponse().getValues().get(0).getMatches() > 0) {
    final SolrDocument solrDocument = rsp.getGroupResponse().getValues().get(0).getValues().get(0).getResult().get(0);
    final Integer idAsInt = Integer.parseInt(solrDocument.getFieldValue(KEY_FIELD).toString());
    TestSegmentSorting.assertTrue
      (KEY_FIELD +"="+idAsInt+" of ("+solrDocument+") is not in maxTimestampDocKeys("+maxTimestampDocKeys+")",
       maxTimestampDocKeys.contains(idAsInt));
    TestSegmentSorting.assertEquals(ODD_FIELD, oddFieldValue, solrDocument.getFieldValue(ODD_FIELD));
  }
  // check segmentTerminatedEarly flag
  // at present segmentTerminateEarly cannot be used with grouped queries
  TestSegmentSorting.assertFalse("responseHeader.segmentTerminatedEarly present/true in "+rsp.getResponseHeader(),
      Boolean.TRUE.equals(rsp.getResponseHeader().get(SolrQueryResponse.RESPONSE_HEADER_SEGMENT_TERMINATED_EARLY_KEY)));
}
 
Example 9
Source File: MCROAISolrSearcher.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
private SolrQuery getBaseQuery(String restrictionField) {
    String configPrefix = this.identify.getConfigPrefix();
    SolrQuery query = new SolrQuery();
    // query
    MCRConfiguration2.getString(configPrefix + "Search.Restriction")
        .ifPresent(restriction -> query.set(restrictionField, restriction));
    String[] requiredFields = Stream.concat(Stream.of("id", getModifiedField()), getRequiredFieldNames().stream())
        .toArray(String[]::new);
    query.setFields(requiredFields);
    // request handler
    query.setRequestHandler(MCRConfiguration2.getString(configPrefix + "Search.RequestHandler").orElse("/select"));
    return query;
}
 
Example 10
Source File: MCRConditionTransformer.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
public static SolrQuery getSolrQuery(@SuppressWarnings("rawtypes") MCRCondition condition, List<MCRSortBy> sortBy,
    int maxResults, List<String> returnFields) {
    String queryString = getQueryString(condition);
    SolrQuery q = applySortOptions(new SolrQuery(queryString), sortBy);
    q.setIncludeScore(true);
    q.setRows(maxResults == 0 ? Integer.MAX_VALUE : maxResults);

    if (returnFields != null) {
        q.setFields(returnFields.size() > 0 ? returnFields.stream().collect(Collectors.joining(",")) : "*");
    }
    String sort = q.getSortField();
    LOGGER.info("MyCoRe Query transformed to: {}{} {}", q.getQuery(), sort != null ? " " + sort : "",
        q.getFields());
    return q;
}
 
Example 11
Source File: TestAdapterModel.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws Exception {
  final int rows = random().nextInt(numDocs+1); // 0..numDocs
  final SolrQuery query = new SolrQuery("*:*");
  query.setRows(rows);
  query.setFields("*,score");
  query.add("rq", "{!ltr model=answerModel}");
  final String[] tests = new String[rows];
  for (int ii=0; ii<rows; ++ii) {
    tests[ii] = "/response/docs/["+ii+"]/score=="+scoreValue;
  }
  assertJQ("/query" + query.toQueryString(), tests);
}
 
Example 12
Source File: LogLevelFilterManagerSolr.java    From ambari-logsearch with Apache License 2.0 5 votes vote down vote up
@Override
public LogLevelFilterMap getLogLevelFilters(String clusterName) {
  LogLevelFilterMap logLevelFilterMap = new LogLevelFilterMap();
  TreeMap<String, LogLevelFilter> logLevelFilterTreeMap = new TreeMap<>();
  try {
    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setQuery("*:*");
    if (useClusterParam) {
      solrQuery.addFilterQuery("cluster_string:" + clusterName);
    }
    solrQuery.addFilterQuery("type:log_level_filter");
    solrQuery.setFields("value", "name");

    final QueryResponse response = solrClient.query(solrQuery);
    if (response != null) {
      final SolrDocumentList documents = response.getResults();
      if (documents != null && !documents.isEmpty()) {
        for(SolrDocument document : documents) {
          String jsons = (String) document.getFieldValue("value");
          String logId = (String) document.getFieldValue("name");
          if (jsons != null) {
            LogLevelFilter logLevelFilter = gson.fromJson(jsons, LogLevelFilter.class);
            logLevelFilterTreeMap.put(logId,logLevelFilter);
          }
        }
      }
    }
  } catch (Exception e) {
    logger.error("Error during getting log level filters: {}", e.getMessage());
  }
  logLevelFilterMap.setFilter(logLevelFilterTreeMap);
  return logLevelFilterMap;
}
 
Example 13
Source File: SolrOntologySearch.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
@Override
public ResultsList<OntologyEntryBean> searchOntology(String term, List<String> filters, int start, int rows, List<String> fields) throws SearchEngineException {
	ResultsList<OntologyEntryBean> results = null;
	
	try {
		SolrQuery query = new SolrQuery(term);
		if (filters != null && !filters.isEmpty()) {
			query.addFilterQuery(filters.toArray(new String[filters.size()]));
		}
		if (fields != null && !fields.isEmpty()) {
			query.setFields(fields.toArray(new String[fields.size()]));
		}
		query.setStart(start);
		query.setRows(rows);
		query.setRequestHandler(config.getOntologyRequestHandler());
		
		LOGGER.trace("Ontology search URL: {}", getQueryUrl(query, config.getOntologyUrl()));
		
		QueryResponse response = server.query(query);
		List<OntologyEntryBean> annotations = response.getBeans(OntologyEntryBean.class);
		results = new ResultsList<>(annotations, rows, (start / rows), response.getResults().getNumFound());
	} catch (SolrServerException e) {
		throw new SearchEngineException(e);
	}
	
	return results;
}
 
Example 14
Source File: SolrMachineProducer.java    From extract with MIT License 5 votes vote down vote up
private long fetch() throws IOException, SolrServerException {
	final SolrQuery query = new SolrQuery(filter);

	query.setRows(rows);
	query.setStart((int) start);

	// Only request the fields to be copied and the ID.
	query.setFields(idField);

	if (null != fields) {
		fields.forEach(query::addField);
	}

	logger.info(String.format("Fetching up to %d documents, skipping %d.", rows, start));
	client.queryAndStreamResponse(query, this);

	final long fetched = this.fetched;

	// Stop if there are no more results.
	// Instruct transformers to stop by sending a poison pill.
	if (fetched < rows) {
		stopped = true;
	}

	// Reset for the next run.
	this.fetched = 0;
	return fetched;
}
 
Example 15
Source File: DistributedDebugComponentTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
@SuppressWarnings("resource") // Cannot close client in this loop!
public void testRandom() throws Exception {
  final int NUM_ITERS = atLeast(50);

  for (int i = 0; i < NUM_ITERS; i++) {
    final SolrClient client = random().nextBoolean() ? collection1 : collection2;

    SolrQuery q = new SolrQuery();
    q.set("distrib", "true");
    q.setFields("id", "text");

    boolean shard1Results = random().nextBoolean();
    boolean shard2Results = random().nextBoolean();

    String qs = "_query_with_no_results_";
    if (shard1Results) {
      qs += " OR batman";
    }
    if (shard2Results) {
      qs += " OR superman";
    }
    q.setQuery(qs);

    Set<String> shards = new HashSet<String>(Arrays.asList(shard1, shard2));
    if (random().nextBoolean()) {
      shards.remove(shard1);
    } else if (random().nextBoolean()) {
      shards.remove(shard2);
    }
    q.set("shards", String.join(",", shards));


    List<String> debug = new ArrayList<String>(10);

    boolean all = false;
    final boolean timing = random().nextBoolean();
    final boolean query = random().nextBoolean();
    final boolean results = random().nextBoolean();
    final boolean track = random().nextBoolean();

    if (timing) { debug.add("timing"); }
    if (query) { debug.add("query"); }
    if (results) { debug.add("results"); }
    if (track) { debug.add("track"); }
    if (debug.isEmpty()) {
      debug.add("true");
      all = true;
    }
    q.set("debug", debug.toArray(new String[debug.size()]));

    QueryResponse r = client.query(q);
    try {
      assertDebug(r, all || track, "track");
      assertDebug(r, all || query, "rawquerystring");
      assertDebug(r, all || query, "querystring");
      assertDebug(r, all || query, "parsedquery");
      assertDebug(r, all || query, "parsedquery_toString");
      assertDebug(r, all || query, "QParser");
      assertDebug(r, all || results, "explain");
      assertDebug(r, all || timing, "timing");
    } catch (AssertionError e) {
      throw new AssertionError(q.toString() + ": " + e.getMessage(), e);
    }
  }
}
 
Example 16
Source File: SegmentTerminateEarlyTestState.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
void queryTimestampDescendingSegmentTerminateEarlyNo(CloudSolrClient cloudSolrClient) throws Exception {
  TestSegmentSorting.assertFalse(maxTimestampDocKeys.isEmpty());
  TestSegmentSorting.assertTrue("numDocs="+numDocs+" is not even", (numDocs%2)==0);
  final Long oddFieldValue = (long) (maxTimestampDocKeys.iterator().next().intValue() % 2);
  final SolrQuery query = new SolrQuery(ODD_FIELD +":"+oddFieldValue);
  query.setSort(TIMESTAMP_FIELD, SolrQuery.ORDER.desc);
  query.setFields(KEY_FIELD, ODD_FIELD, TIMESTAMP_FIELD);
  query.setRows(1);
  final Boolean shardsInfoWanted = (rand.nextBoolean() ? null : rand.nextBoolean());
  if (shardsInfoWanted != null) {
    query.set(ShardParams.SHARDS_INFO, shardsInfoWanted.booleanValue());
  }
  query.set(CommonParams.SEGMENT_TERMINATE_EARLY, false);
  final QueryResponse rsp = cloudSolrClient.query(query);
  // check correctness of the results count
  TestSegmentSorting.assertEquals("numFound", numDocs/2, rsp.getResults().getNumFound());
  // check correctness of the first result
  if (rsp.getResults().getNumFound() > 0) {
    final SolrDocument solrDocument0 = rsp.getResults().get(0);
    final Integer idAsInt = Integer.parseInt(solrDocument0.getFieldValue(KEY_FIELD).toString());
    TestSegmentSorting.assertTrue
      (KEY_FIELD +"="+idAsInt+" of ("+solrDocument0+") is not in maxTimestampDocKeys("+maxTimestampDocKeys+")",
       maxTimestampDocKeys.contains(idAsInt));
    TestSegmentSorting.assertEquals(ODD_FIELD, oddFieldValue, rsp.getResults().get(0).getFieldValue(ODD_FIELD));
  }
  // check segmentTerminatedEarly flag
  TestSegmentSorting.assertNull("responseHeader.segmentTerminatedEarly present in "+rsp.getResponseHeader(),
      rsp.getResponseHeader().get(SolrQueryResponse.RESPONSE_HEADER_SEGMENT_TERMINATED_EARLY_KEY));
  TestSegmentSorting.assertFalse("responseHeader.segmentTerminatedEarly present/true in "+rsp.getResponseHeader(),
      Boolean.TRUE.equals(rsp.getResponseHeader().get(SolrQueryResponse.RESPONSE_HEADER_SEGMENT_TERMINATED_EARLY_KEY)));
  // check shards info
  final Object shardsInfo = rsp.getResponse().get(ShardParams.SHARDS_INFO);
  if (!Boolean.TRUE.equals(shardsInfoWanted)) {
    TestSegmentSorting.assertNull(ShardParams.SHARDS_INFO, shardsInfo);
  } else {
    TestSegmentSorting.assertNotNull(ShardParams.SHARDS_INFO, shardsInfo);
    int segmentTerminatedEarlyShardsCount = 0;
    for (Map.Entry<String, ?> si : (SimpleOrderedMap<?>)shardsInfo) {
      if (Boolean.TRUE.equals(((SimpleOrderedMap)si.getValue()).get(SolrQueryResponse.RESPONSE_HEADER_SEGMENT_TERMINATED_EARLY_KEY))) {
        segmentTerminatedEarlyShardsCount += 1;
      }
    }
    TestSegmentSorting.assertEquals("shards reporting "+SolrQueryResponse.RESPONSE_HEADER_SEGMENT_TERMINATED_EARLY_KEY,
        0, segmentTerminatedEarlyShardsCount);
  }
}
 
Example 17
Source File: SegmentTerminateEarlyTestState.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
void queryTimestampDescendingSegmentTerminateEarlyYes(CloudSolrClient cloudSolrClient) throws Exception {
  TestSegmentSorting.assertFalse(maxTimestampDocKeys.isEmpty());
  TestSegmentSorting.assertTrue("numDocs="+numDocs+" is not even", (numDocs%2)==0);
  final Long oddFieldValue = (long) (maxTimestampDocKeys.iterator().next().intValue() % 2);
  final SolrQuery query = new SolrQuery(ODD_FIELD +":"+oddFieldValue);
  query.setSort(TIMESTAMP_FIELD, SolrQuery.ORDER.desc);
  query.setFields(KEY_FIELD, ODD_FIELD, TIMESTAMP_FIELD);
  final int rowsWanted = 1;
  query.setRows(rowsWanted);
  final Boolean shardsInfoWanted = (rand.nextBoolean() ? null : rand.nextBoolean());
  if (shardsInfoWanted != null) {
    query.set(ShardParams.SHARDS_INFO, shardsInfoWanted.booleanValue());
  }
  query.set(CommonParams.SEGMENT_TERMINATE_EARLY, true);
  final QueryResponse rsp = cloudSolrClient.query(query);
  // check correctness of the results count
  TestSegmentSorting.assertTrue("numFound", rowsWanted <= rsp.getResults().getNumFound());
  TestSegmentSorting.assertTrue("numFound", rsp.getResults().getNumFound() <= numDocs/2);
  // check correctness of the first result
  if (rsp.getResults().getNumFound() > 0) {
    final SolrDocument solrDocument0 = rsp.getResults().get(0);
    final Integer idAsInt = Integer.parseInt(solrDocument0.getFieldValue(KEY_FIELD).toString());
    TestSegmentSorting.assertTrue
      (KEY_FIELD +"="+idAsInt+" of ("+solrDocument0+") is not in maxTimestampDocKeys("+maxTimestampDocKeys+")",
       maxTimestampDocKeys.contains(idAsInt));
    TestSegmentSorting.assertEquals(ODD_FIELD, oddFieldValue, rsp.getResults().get(0).getFieldValue(ODD_FIELD));
  }
  // check segmentTerminatedEarly flag
  TestSegmentSorting.assertNotNull("responseHeader.segmentTerminatedEarly missing in "+rsp.getResponseHeader(),
      rsp.getResponseHeader().get(SolrQueryResponse.RESPONSE_HEADER_SEGMENT_TERMINATED_EARLY_KEY));
  TestSegmentSorting.assertTrue("responseHeader.segmentTerminatedEarly missing/false in "+rsp.getResponseHeader(),
      Boolean.TRUE.equals(rsp.getResponseHeader().get(SolrQueryResponse.RESPONSE_HEADER_SEGMENT_TERMINATED_EARLY_KEY)));
  // check shards info
  final Object shardsInfo = rsp.getResponse().get(ShardParams.SHARDS_INFO);
  if (!Boolean.TRUE.equals(shardsInfoWanted)) {
    TestSegmentSorting.assertNull(ShardParams.SHARDS_INFO, shardsInfo);
  } else {
    TestSegmentSorting.assertNotNull(ShardParams.SHARDS_INFO, shardsInfo);
    int segmentTerminatedEarlyShardsCount = 0;
    for (Map.Entry<String, ?> si : (SimpleOrderedMap<?>)shardsInfo) {
      if (Boolean.TRUE.equals(((SimpleOrderedMap)si.getValue()).get(SolrQueryResponse.RESPONSE_HEADER_SEGMENT_TERMINATED_EARLY_KEY))) {
        segmentTerminatedEarlyShardsCount += 1;
      }
    }
    // check segmentTerminatedEarly flag within shards info
    TestSegmentSorting.assertTrue(segmentTerminatedEarlyShardsCount+" shards reported "+SolrQueryResponse.RESPONSE_HEADER_SEGMENT_TERMINATED_EARLY_KEY,
        (0<segmentTerminatedEarlyShardsCount));
  }
}
 
Example 18
Source File: DistributedDebugComponentTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testSimpleSearch() throws Exception {
  SolrQuery query = new SolrQuery();
  query.setQuery("*:*");
  query.set("debug",  "track");
  query.set("distrib", "true");
  query.setFields("id", "text");
  query.set("shards", shard1 + "," + shard2);
  
  if (random().nextBoolean()) {
    query.add("omitHeader", Boolean.toString(random().nextBoolean()));
  }
  QueryResponse response = collection1.query(query);
  NamedList<Object> track = (NamedList<Object>) response.getDebugMap().get("track");
  assertNotNull(track);
  assertNotNull(track.get("rid"));
  assertNotNull(track.get("EXECUTE_QUERY"));
  assertNotNull(((NamedList<Object>)track.get("EXECUTE_QUERY")).get(shard1));
  assertNotNull(((NamedList<Object>)track.get("EXECUTE_QUERY")).get(shard2));
  
  assertNotNull(((NamedList<Object>)track.get("GET_FIELDS")).get(shard1));
  assertNotNull(((NamedList<Object>)track.get("GET_FIELDS")).get(shard2));
  
  assertElementsPresent((NamedList<String>)((NamedList<Object>)track.get("EXECUTE_QUERY")).get(shard1), 
      "QTime", "ElapsedTime", "RequestPurpose", "NumFound", "Response");
  assertElementsPresent((NamedList<String>)((NamedList<Object>)track.get("EXECUTE_QUERY")).get(shard2), 
      "QTime", "ElapsedTime", "RequestPurpose", "NumFound", "Response");
  
  assertElementsPresent((NamedList<String>)((NamedList<Object>)track.get("GET_FIELDS")).get(shard1), 
      "QTime", "ElapsedTime", "RequestPurpose", "NumFound", "Response");
  assertElementsPresent((NamedList<String>)((NamedList<Object>)track.get("GET_FIELDS")).get(shard2), 
      "QTime", "ElapsedTime", "RequestPurpose", "NumFound", "Response");
  
  query.setQuery("id:1");
  response = collection1.query(query);
  track = (NamedList<Object>) response.getDebugMap().get("track");
  assertNotNull(((NamedList<Object>)track.get("EXECUTE_QUERY")).get(shard1));
  assertNotNull(((NamedList<Object>)track.get("EXECUTE_QUERY")).get(shard2));
  
  assertNotNull(((NamedList<Object>)track.get("GET_FIELDS")).get(shard1));
  // This test is invalid, as GET_FIELDS should not be executed in shard 2
  assertNull(((NamedList<Object>)track.get("GET_FIELDS")).get(shard2));
}
 
Example 19
Source File: MergingSolrSpewer.java    From extract with MIT License 4 votes vote down vote up
private void merge(final TikaDocument tikaDocument, final SolrInputDocument inputDocument) throws IOException,
		SolrServerException {
	final SolrDocument existingDocument;
	final SolrQuery params = new SolrQuery();
	final String resourceNameKey = fields.forMetadata(Metadata.RESOURCE_NAME_KEY);

	// The tikaDocument must be retrieved from the real-time-get (RTG) handler, otherwise we'd have to commit every
	// time a tikaDocument is added.
	params.setRequestHandler("/get");

	// Request only the fields which must be merged, not the entire tikaDocument.
	params.setFields(fields.forPath(), fields.forParentPath(), fields.forVersion(), resourceNameKey);
	existingDocument = client.getById(tikaDocument.getId(), params);

	// Since we're updating the path and parent path values of an existing tikaDocument, set the version field to
	// avoid conflicts. Note that child documents don't have a version field.
	if (null != existingDocument) {
		final Object version = existingDocument.getFieldValue(fields.forVersion());
		if (null != version) {
			inputDocument.setField(fields.forVersion(), version);
		}

	} else {
		inputDocument.setField(fields.forVersion(), "-1");
	}

	// Set the path field.
	if (null != fields.forPath()) {
		mergeField(fields.forPath(), tikaDocument.getPath().toString(), existingDocument, inputDocument);
	}

	// Set the parent path field.
	if (null != fields.forParentPath() && tikaDocument.getPath().getNameCount() > 1) {
		mergeField(fields.forParentPath(), tikaDocument.getPath().getParent().toString(), existingDocument,
				inputDocument);
	}

	// Merge the resource name field.
	if (tikaDocument.getMetadata() != null) {
		mergeField(resourceNameKey, tikaDocument.getMetadata().get(Metadata.RESOURCE_NAME_KEY), existingDocument,
				inputDocument);
	}
}
 
Example 20
Source File: BlurQueryHelperTest.java    From incubator-retired-blur with Apache License 2.0 4 votes vote down vote up
@Test
public void fieldsShouldTranslateToSelector() {
  SolrQuery p = new SolrQuery();

  p.setFields("fam1.col1", "fam1.col2", "fam2.col1");

  BlurQuery query = BlurQueryHelper.from(p);

  Map<String, Set<String>> columns = query.getSelector().getColumnsToFetch();

  assertTrue("Should have fam1 defined.", columns.containsKey("fam1"));
  assertTrue("Should have fam2 defined.", columns.containsKey("fam2"));

  Set<String> fam1 = columns.get("fam1");

  assertEquals("Should get all columns back.", 2, fam1.size());
  assertTrue("Should contain our column", fam1.contains("col1"));
  assertTrue("Should contain our column", fam1.contains("col2"));

  Set<String> fam2 = columns.get("fam2");
  assertEquals("Should get all columns back.", 1, fam2.size());
  assertTrue("Should contain our column", fam2.contains("col1"));

}