Java Code Examples for org.apache.solr.common.SolrDocumentList#size()

The following examples show how to use org.apache.solr.common.SolrDocumentList#size() . 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: JsonQueryRequestIntegrationTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testReturnsResultsStartingAtOffset() throws Exception {
  final JsonQueryRequest originalDocsQuery = new JsonQueryRequest()
      .setQuery("*:*");
  QueryResponse originalDocsResponse = originalDocsQuery.process(cluster.getSolrClient(), COLLECTION_NAME);
  assertEquals(0, originalDocsResponse.getStatus());
  assertEquals(NUM_BOOKS_TOTAL, originalDocsResponse.getResults().size());
  final SolrDocumentList originalDocs = originalDocsResponse.getResults();

  final int offset = 2;
  final JsonQueryRequest offsetDocsQuery = new JsonQueryRequest()
      .setQuery("*:*")
      .setOffset(offset);
  QueryResponse offsetDocsResponse = offsetDocsQuery.process(cluster.getSolrClient(), COLLECTION_NAME);
  assertEquals(0, offsetDocsResponse.getStatus());
  assertEquals(NUM_BOOKS_TOTAL - offset, offsetDocsResponse.getResults().size());
  final SolrDocumentList offsetDocs = offsetDocsResponse.getResults();

  // Ensure the same docs are returned, shifted by 'offset'
  for (int i = 0; i < offsetDocs.size(); i++) {
    final String offsetId = (String) offsetDocs.get(i).getFieldValue("id");
    final String originalId = (String) originalDocs.get(i + offset).getFieldValue("id");
    assertEquals(offsetId, originalId);
  }
}
 
Example 2
Source File: EmbeddedSolrServer.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private JavaBinCodec createJavaBinCodec(final StreamingResponseCallback callback, final BinaryResponseWriter.Resolver resolver) {
  return new JavaBinCodec(resolver) {

    @Override
    public void writeSolrDocument(SolrDocument doc) {
      callback.streamSolrDocument(doc);
      //super.writeSolrDocument( doc, fields );
    }

    @Override
    public void writeSolrDocumentList(SolrDocumentList docs) throws IOException {
      if (docs.size() > 0) {
        SolrDocumentList tmp = new SolrDocumentList();
        tmp.setMaxScore(docs.getMaxScore());
        tmp.setNumFound(docs.getNumFound());
        tmp.setStart(docs.getStart());
        docs = tmp;
      }
      callback.streamDocListInfo(docs.getNumFound(), docs.getStart(), docs.getMaxScore());
      super.writeSolrDocumentList(docs);
    }

  };
}
 
Example 3
Source File: CloudMLTQParserTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultipleFields() throws Exception {

  QueryResponse queryResponse = cluster.getSolrClient().query(COLLECTION,
      new SolrQuery("{!mlt qf=lowerfilt_u,lowerfilt1_u mindf=0 mintf=1}26"));
  SolrDocumentList solrDocuments = queryResponse.getResults();
  int[] expectedIds = new int[]{3, 29, 27, 28};
  int[] actualIds = new int[solrDocuments.size()];
  int i = 0;
  for (SolrDocument solrDocument : solrDocuments) {
    actualIds[i++] = Integer.parseInt(String.valueOf(solrDocument.getFieldValue("id")));
  }

  Arrays.sort(actualIds);
  Arrays.sort(expectedIds);
  assertArrayEquals(Arrays.toString(expectedIds) + " " + Arrays.toString(actualIds), expectedIds, actualIds);

}
 
Example 4
Source File: SolrTestCaseJ4.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Fails if the number of documents in the given SolrDocumentList differs
 * from the given number of expected values, or if any of the values in the
 * given field don't match the expected values in the same order.
 */
public static void assertFieldValues(SolrDocumentList documents, String fieldName, Object... expectedValues) {
  if (documents.size() != expectedValues.length) {
    fail("Number of documents (" + documents.size()
        + ") is different from number of expected values (" + expectedValues.length);
  }
  for (int docNum = 1 ; docNum <= documents.size() ; ++docNum) {
    SolrDocument doc = documents.get(docNum - 1);
    Object expected = expectedValues[docNum - 1];
    Object actual = doc.get(fieldName);
    if ((null == expected && null != actual) ||
        (null != expected && null == actual) ||
        (null != expected && null != actual && !expected.equals(actual))) {
      fail("Unexpected " + fieldName + " field value in document #" + docNum
          + ": expected=[" + expected + "], actual=[" + actual + "]");
    }
  }
}
 
Example 5
Source File: JsonQueryRequestIntegrationTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testReturnsReturnsResultsWithSpecifiedSort() throws Exception {
  final JsonQueryRequest simpleQuery = new JsonQueryRequest()
      .setQuery("*:*")
      .setSort("price desc");
  QueryResponse queryResponse = simpleQuery.process(cluster.getSolrClient(), COLLECTION_NAME);

  assertEquals(0, queryResponse.getStatus());
  assertEquals(NUM_BOOKS_TOTAL, queryResponse.getResults().getNumFound());
  final SolrDocumentList docs = queryResponse.getResults();
  for (int i = 0; i < docs.size() - 1; i++) {
    final float pricierDocPrice = (Float) docs.get(i).getFieldValue("price");
    final float cheaperDocPrice = (Float) docs.get(i+1).getFieldValue("price");
    assertTrue("Expected doc at index " + i + " doc to be more expensive than doc at " + (i+1),
        pricierDocPrice >= cheaperDocPrice);
  }
}
 
Example 6
Source File: CloudMLTQParserTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnstoredAndUnanalyzedFieldsAreIgnored() throws Exception {

  // Assert that {!mlt}id does not throw an exception i.e. implicitly, only fields that are stored + have explicit
  // analyzer are used for MLT Query construction.
  QueryResponse queryResponse = cluster.getSolrClient().query(COLLECTION, new SolrQuery("{!mlt}20"));
  SolrDocumentList solrDocuments = queryResponse.getResults();
  int[] actualIds = new int[solrDocuments.size()];
  int[] expectedIds = new int[]{13, 14, 15, 16, 22, 24, 32, 18, 19, 21};
  int i = 0;
  StringBuilder sb = new StringBuilder();
  for (SolrDocument solrDocument : solrDocuments) {
    actualIds[i++] =  Integer.parseInt(String.valueOf(solrDocument.getFieldValue("id")));
    sb.append(actualIds[i-1]).append(", ");
  }
  
  Arrays.sort(actualIds);
  Arrays.sort(expectedIds);
  assertArrayEquals(expectedIds, actualIds);
}
 
Example 7
Source File: DocValuesNotIndexedTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void checkSortOrder(CloudSolrClient client, List<FieldProps> props, String sortDir, String[] order, String[] orderBool) throws IOException, SolrServerException {
  for (FieldProps prop : props) {
    final SolrQuery solrQuery = new SolrQuery("q", "*:*", "rows", "100");
    solrQuery.setSort(prop.getName(), "asc".equals(sortDir) ? SolrQuery.ORDER.asc : SolrQuery.ORDER.desc);
    solrQuery.addSort("id", SolrQuery.ORDER.asc);
    final QueryResponse rsp = client.query(COLLECTION, solrQuery);
    SolrDocumentList res = rsp.getResults();
    assertEquals("Should have exactly " + order.length + " documents returned", order.length, res.getNumFound());
    String expected;
    for (int idx = 0; idx < res.size(); ++idx) {
      if (prop.getName().startsWith("bool")) expected = orderBool[idx];
      else expected = order[idx];
      assertEquals("Documents in wrong order for field: " + prop.getName(),
          expected, res.get(idx).get("id"));
    }
  }
}
 
Example 8
Source File: AlfrescoSolrSortIT.java    From SearchServices with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void AlfrescoCollatableFieldType_emptyValuesSortingAsc__shouldBeRankedFirst() throws Exception
{
    prepareIndexSegmentWithAllNonNullFieldValues("text@s__sort@{http://www.alfresco.org/model/content/1.0}title");
    putHandleDefaults();
    // Docs with id 1, 3, 5 and 6 should be first (note that these will be sorted by indexing time).
    String[] expectedRanking = new String[]{"1","3","5","6","4","2"};
    
    QueryResponse response = query(getDefaultTestClient(), true,
            "{\"query\":\"(id:(1 2 3 4 5 6))\",\"locales\":[\"en\"], \"templates\": [{\"name\":\"t1\", \"template\":\"%cm:content\"}], \"authorities\": [\"joel\"], \"tenants\": []}",
            params("qt", "/afts", "shards.qt", "/afts", "start", "0", "rows", "100", "sort", "text@s__sort@{http://www.alfresco.org/model/content/1.0}title asc"));

    NamedList<?> res = response.getResponse();
    SolrDocumentList searchResults = (SolrDocumentList)res.get("response");
    for(int i=0;i<searchResults.size();i++){
        assertThat(searchResults.get(i).get("id"),is(expectedRanking[i]));
    }
}
 
Example 9
Source File: ContentEditor.java    From jease with GNU General Public License v3.0 6 votes vote down vote up
public String checkDuplication() {
	try {
		String solrurl = jease.Registry.getParameter(jease.Names.JEASE_SOLR_URL, "");
		SolrClient client = new HttpSolrClient.Builder(solrurl).build();

		SolrQuery query = new SolrQuery();
		query.setQuery("*:*");
		query.setFilterQueries("jeaseid:\"" + id.getValue() + "\" ");
		query.setFilterQueries("jeasepath:\"" + getNode().getPath() + "\"");
		SolrDocumentList results = client.query(query).getResults();
		if (results.size() > 0) {
			return results.get(0).getFieldValue("id").toString();
		}
	} catch (Exception s) {
		s.printStackTrace();
	}
	return "";
}
 
Example 10
Source File: DistributedExpandComponentTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void assertExpandGroupCountAndOrder(String group, int count, Map<String, SolrDocumentList>expandedResults, String... docs) throws Exception {
  SolrDocumentList results = expandedResults.get(group);
  if(results == null) {
    throw new Exception("Group Not Found:"+group);
  }

  if(results.size() != count) {
    throw new Exception("Expected Count "+results.size()+" Not Found:"+count);
  }

  for(int i=0; i<docs.length;i++) {
    String id = docs[i];
    SolrDocument doc = results.get(i);
    if(!doc.getFieldValue("id").toString().equals(id)) {
      throw new Exception("Id not in results or out of order:"+id+"!="+doc.getFieldValue("id"));
    }
  }
}
 
Example 11
Source File: StandardSearchEngine.java    From document-management-software with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public Hit getHit(long id) {
	SolrQuery query = new SolrQuery();
	query.setQuery("id:" + id);
	query.setFields("*");
	try {
		QueryResponse rsp = server.query(query);
		SolrDocumentList docs = rsp.getResults();
		if (docs.size() < 1)
			return null;

		SolrDocument doc = docs.get(0);
		Hit hit = Hits.toHit(doc);
		hit.setContent((String) doc.getFieldValue(HitField.CONTENT.getName()));
		return hit;
	} catch (Throwable e) {
		log.error(e.getMessage());
	}
	return null;
}
 
Example 12
Source File: CloudMLTQParserTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings({"unchecked"})
public void testMinDF() throws Exception {

  QueryResponse queryResponse = cluster.getSolrClient().query(COLLECTION,
      new SolrQuery("{!mlt qf=lowerfilt_u mindf=0 mintf=1}3").setShowDebugInfo(true));
  SolrDocumentList solrDocuments = queryResponse.getResults();
  int[] expectedIds = new int[]{29, 27, 26, 28};
  int[] actualIds = new int[solrDocuments.size()];
  int i = 0;
  for (SolrDocument solrDocument : solrDocuments) {
    actualIds[i++] = Integer.parseInt(String.valueOf(solrDocument.getFieldValue("id")));
  }
  
  Arrays.sort(actualIds);
  Arrays.sort(expectedIds);
  assertArrayEquals(Arrays.toString(expectedIds) + " " + Arrays.toString(actualIds), expectedIds, actualIds);

  String[] expectedQueryStrings = new String[]{
      "+(lowerfilt_u:bmw lowerfilt_u:usa) -id:3",
      "+(lowerfilt_u:usa lowerfilt_u:bmw) -id:3"};

  String[] actualParsedQueries;
  if (queryResponse.getDebugMap().get("parsedquery") instanceof String) {
    String parsedQueryString = (String) queryResponse.getDebugMap().get("parsedquery");
    assertTrue(parsedQueryString.equals(expectedQueryStrings[0]) || parsedQueryString.equals(expectedQueryStrings[1]));
  } else {
    actualParsedQueries = ((ArrayList<String>) queryResponse
        .getDebugMap().get("parsedquery")).toArray(new String[0]);
    Arrays.sort(actualParsedQueries);
    assertArrayEquals(expectedQueryStrings, actualParsedQueries);
  }
}
 
Example 13
Source File: AbstractLogSearchSteps.java    From ambari-logsearch with Apache License 2.0 5 votes vote down vote up
protected void waitUntilSolrHasAnyData() throws InterruptedException {
  boolean solrHasData = false;
  int maxTries = 60;
  String lastExceptionMessage = null;

  for (int tries = 1; tries < maxTries; tries++) {
    try {
      SolrClient solrClient = StoryDataRegistry.INSTANCE.getSolrClient();
      SolrQuery solrQuery = new SolrQuery();
      solrQuery.setQuery("*:*");
      QueryResponse queryResponse = solrClient.query(solrQuery);
      SolrDocumentList list = queryResponse.getResults();
      if (list.size() > 0) {
        solrHasData = true;
        break;
      } else {
        Thread.sleep(2000);
        LOG.info("Solr has no data yet. Retrying... ({} tries)", tries);
      }
    } catch (Exception e) {
      LOG.info("Error occurred during checking solr. Retrying... ({} tries)", tries);
      lastExceptionMessage = e.getMessage();
      Thread.sleep(2000);
    }
  }
  if (!solrHasData) {
    throw new IllegalStateException(String.format("Solr has no data after %d tries. Exception: %s", maxTries, lastExceptionMessage));
  }
}
 
Example 14
Source File: CloudInspectUtil.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private static String toStr(SolrDocumentList lst, int maxSz) {
  if (lst.size() <= maxSz) return lst.toString();
  
  StringBuilder sb = new StringBuilder("SolrDocumentList[sz=" + lst.size());
  if (lst.size() != lst.getNumFound()) {
    sb.append(" numFound=").append(lst.getNumFound());
  }
  sb.append("]=");
  sb.append(lst.subList(0, maxSz / 2).toString());
  sb.append(" , [...] , ");
  sb.append(lst.subList(lst.size() - maxSz / 2, lst.size()).toString());
  
  return sb.toString();
}
 
Example 15
Source File: SearchableImpl.java    From spring-content with Apache License 2.0 5 votes vote down vote up
List<Object> getIds(NamedList response) {
	List<Object> ids = new ArrayList<>();
	SolrDocumentList list = (SolrDocumentList) response.get("response");
	for (int j = 0; j < list.size(); ++j) {
		String id = list.get(j).getFieldValue("id").toString();
		id = id.substring(id.indexOf(':') + 1, id.length());
		ids.add(id);
	}

	return ids;
}
 
Example 16
Source File: MockSolrEntityProcessor.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
protected void buildIterator() {
  if (rowIterator==null || (!rowIterator.hasNext() && ((SolrDocumentListIterator)rowIterator).hasMoreRows())){
    queryCount++;
    SolrDocumentList docs = getDocs(start, rows);
    rowIterator = new SolrDocumentListIterator(docs);
    start += docs.size();
  }
}
 
Example 17
Source File: SolrSearchService.java    From spring-content with Apache License 2.0 5 votes vote down vote up
List<Object> getIds(NamedList response) {
	List<Object> ids = new ArrayList<>();
	SolrDocumentList list = (SolrDocumentList) response.get("response");
	for (int j = 0; j < list.size(); ++j) {
		String id = list.get(j).getFieldValue("id").toString();
		id = id.substring(id.indexOf(':') + 1, id.length());
		ids.add(id);
	}

	return ids;
}
 
Example 18
Source File: TextResponseWriter.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public final void writeSolrDocumentList(String name, SolrDocumentList docs, ReturnFields fields) throws IOException
{
  writeStartDocumentList(name, docs.getStart(), docs.size(), docs.getNumFound(), docs.getMaxScore(), docs.getNumFoundExact());
  for( int i=0; i<docs.size(); i++ ) {
    writeSolrDocument( null, docs.get(i), fields, i );
  }
  writeEndDocumentList();
}
 
Example 19
Source File: SolrAccessAuditsService.java    From ranger with Apache License 2.0 4 votes vote down vote up
public VXAccessAuditList searchXAccessAudits(SearchCriteria searchCriteria) {

		// Make call to Solr
		SolrClient solrClient = solrMgr.getSolrClient();
		final boolean hiveQueryVisibility = PropertiesUtil.getBooleanProperty("ranger.audit.hive.query.visibility", true);
		if (solrClient == null) {
			LOGGER.warn("Solr client is null, so not running the query.");
			throw restErrorUtil.createRESTException(
					"Error connecting to search engine",
					MessageEnums.ERROR_SYSTEM);
		}
		List<VXAccessAudit> xAccessAuditList = new ArrayList<VXAccessAudit>();

		Map<String, Object> paramList = searchCriteria.getParamList();
		updateUserExclusion(paramList);

		QueryResponse response = solrUtil.searchResources(searchCriteria,
				searchFields, sortFields, solrClient);
		SolrDocumentList docs = response.getResults();
		for (int i = 0; i < docs.size(); i++) {
			SolrDocument doc = docs.get(i);
			VXAccessAudit vXAccessAudit = populateViewBean(doc);
                        if (vXAccessAudit != null) {
                                if (!hiveQueryVisibility && "hive".equalsIgnoreCase(vXAccessAudit.getServiceType())) {
                                        vXAccessAudit.setRequestData(null);
                                }
                                else if("hive".equalsIgnoreCase(vXAccessAudit.getServiceType()) && ("grant".equalsIgnoreCase(vXAccessAudit.getAccessType()) || "revoke".equalsIgnoreCase(vXAccessAudit.getAccessType()))){
                                        try {
                                            if (vXAccessAudit.getRequestData() != null) {
                                                vXAccessAudit.setRequestData(java.net.URLDecoder.decode(vXAccessAudit.getRequestData(), "UTF-8"));
                                            } else {
                                                LOGGER.warn("Error in request data of audit from solr. AuditData: "  + vXAccessAudit.toString());
                                            }
                                        } catch (UnsupportedEncodingException e) {
                                                LOGGER.warn("Error while encoding request data");
                                        }
                                }
                        }
                        xAccessAuditList.add(vXAccessAudit);
		}

		VXAccessAuditList returnList = new VXAccessAuditList();
		returnList.setPageSize(searchCriteria.getMaxRows());
		returnList.setResultSize(docs.size());
		returnList.setTotalCount((int) docs.getNumFound());
		returnList.setStartIndex((int) docs.getStart());
		returnList.setVXAccessAudits(xAccessAuditList);
		return returnList;
	}
 
Example 20
Source File: DistribCursorPagingTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * <p>
 * Given a set of params, executes a cursor query using {@link CursorMarkParams#CURSOR_MARK_START} 
 * and then continuously walks the results using {@link CursorMarkParams#CURSOR_MARK_START} as long 
 * as a non-0 number of docs ar returned.  This method records the the set of all id's
 * (must be positive ints) encountered and throws an assertion failure if any id is 
 * encountered more then once, or if the set grows above maxSize
 * </p>
 *
 * <p>
 * Note that this method explicitly uses the "cloudClient" for executing the queries, 
 * instead of relying on the test infrastructure to execute the queries redundently
 * against both the cloud client as well as a control client.  This is because term stat 
 * differences in a sharded setup can result in different scores for documents compared 
 * to the control index -- which can affect the sorting in some cases and cause false 
 * negatives in the response comparisons (even if we don't include "score" in the "fl")
 * </p>
 */
public SentinelIntSet assertFullWalkNoDups(int maxSize, SolrParams params) throws Exception {
  SentinelIntSet ids = new SentinelIntSet(maxSize, -1);
  String cursorMark = CURSOR_MARK_START;
  int docsOnThisPage = Integer.MAX_VALUE;
  while (0 < docsOnThisPage) {
    final SolrParams p = p(params, CURSOR_MARK_PARAM, cursorMark);
    QueryResponse rsp = cloudClient.query(p);
    String nextCursorMark = assertHashNextCursorMark(rsp);
    SolrDocumentList docs = extractDocList(rsp);
    docsOnThisPage = docs.size();
    if (null != params.getInt(CommonParams.ROWS)) {
      int rows = params.getInt(CommonParams.ROWS);
      assertTrue("Too many docs on this page: " + rows + " < " + docsOnThisPage,
                 docsOnThisPage <= rows);
    }
    if (0 == docsOnThisPage) {
      assertEquals("no more docs, but "+CURSOR_MARK_NEXT+" isn't same",
                   cursorMark, nextCursorMark);
    }

    for (SolrDocument doc : docs) {
      int id = Integer.parseInt(doc.getFieldValue("id").toString());
      if (ids.exists(id)) {
        String msg = "(" + p + ") walk already seen: " + id;
        try {
          queryAndCompareShards(params("distrib","false",
                                       "q","id:"+id));
        } catch (AssertionError ae) {
          throw new AssertionError(msg + ", found shard inconsistency that would explain it...", ae);
        }
        rsp = cloudClient.query(params("q","id:"+id));
        throw new AssertionError(msg + ", don't know why; q=id:"+id+" gives: " + rsp.toString());
      }
      ids.put(id);
      assertFalse("id set bigger then max allowed ("+maxSize+"): " + ids.size(),
                  maxSize < ids.size());
    }
    cursorMark = nextCursorMark;
  }
  return ids;
}