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

The following examples show how to use org.apache.solr.common.SolrDocumentList#getNumFound() . 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: ACLRDTECTest.java    From jate with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static long validate_indexing() {
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.set("q", "*:*");

    try {
        QueryResponse qResp = server.query(params);
        SolrDocumentList docList = qResp.getResults();

        long numDocs = docList.getNumFound();
        LOG.info(String.format("[%s] documents processed!", numDocs));
        return numDocs;
    } catch (SolrServerException e) {
        e.printStackTrace();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
    return 0;
}
 
Example 2
Source File: SolrTemplate.java    From dubbox with Apache License 2.0 6 votes vote down vote up
private <T> SolrResultPage<T> createSolrResultPage(Query query, Class<T> clazz, QueryResponse response,
		Map<String, Object> objectsName) {
	List<T> beans = convertQueryResponseToBeans(response, clazz);
	SolrDocumentList results = response.getResults();
	long numFound = results == null ? 0 : results.getNumFound();
	Float maxScore = results == null ? null : results.getMaxScore();

	Pageable pageRequest = query.getPageRequest();

	SolrResultPage<T> page = new SolrResultPage<T>(beans, pageRequest, numFound, maxScore);

	page.setFieldStatsResults(
			ResultHelper.convertFieldStatsInfoToFieldStatsResultMap(response.getFieldStatsInfo()));
	page.setGroupResults(
			ResultHelper.convertGroupQueryResponseToGroupResultMap(query, objectsName, response, this, clazz));

	return page;
}
 
Example 3
Source File: MCRRestAPIClassifications.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
private void filterNonEmpty(String classId, Element e) {
    SolrClient solrClient = MCRSolrClientFactory.getMainSolrClient();
    Element[] categories = e.getChildren("category").toArray(Element[]::new);
    for (Element cat : categories) {
        SolrQuery solrQquery = new SolrQuery();
        solrQquery.setQuery(
            "category:\"" + MCRSolrUtils.escapeSearchValue(classId + ":" + cat.getAttributeValue("ID")) + "\"");
        solrQquery.setRows(0);
        try {
            QueryResponse response = solrClient.query(solrQquery);
            SolrDocumentList solrResults = response.getResults();
            if (solrResults.getNumFound() == 0) {
                cat.detach();
            } else {
                filterNonEmpty(classId, cat);
            }
        } catch (SolrServerException | IOException exc) {
            LOGGER.error(exc);
        }

    }
}
 
Example 4
Source File: TestContentStreamDataSource.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testCommitWithin() throws Exception {
  DirectXmlRequest req = new DirectXmlRequest("/dataimport", xml);
  ModifiableSolrParams params = params("command", "full-import", 
      "clean", "false", UpdateParams.COMMIT, "false", 
      UpdateParams.COMMIT_WITHIN, "1000");
  req.setParams(params);
  try (HttpSolrClient solrServer = getHttpSolrClient(buildUrl(jetty.getLocalPort(), "/solr/collection1"))) {
    solrServer.request(req);
    Thread.sleep(100);
    ModifiableSolrParams queryAll = params("q", "*", "df", "desc");
    QueryResponse qres = solrServer.query(queryAll);
    SolrDocumentList results = qres.getResults();
    assertEquals(0, results.getNumFound());
    Thread.sleep(1000);
    for (int i = 0; i < 10; i++) {
      qres = solrServer.query(queryAll);
      results = qres.getResults();
      if (2 == results.getNumFound()) {
        return;
      }
      Thread.sleep(500);
    }
  }
  fail("Commit should have occurred but it did not");
}
 
Example 5
Source File: ExportTool.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
static long getDocCount(String coreName, HttpSolrClient client) throws SolrServerException, IOException {
  SolrQuery q = new SolrQuery("*:*");
  q.setRows(0);
  q.add("distrib", "false");
  GenericSolrRequest request = new GenericSolrRequest(SolrRequest.METHOD.GET,
      "/" + coreName + "/select", q);
  NamedList<Object> res = client.request(request);
  SolrDocumentList sdl = (SolrDocumentList) res.get("response");
  return sdl.getNumFound();
}
 
Example 6
Source File: SolrSearchResults.java    From aem-solr-search with Apache License 2.0 5 votes vote down vote up
SolrSearchResults(SolrQuery solrQuery, QueryResponse queryResponse) {
	
	this.solrQuery = solrQuery;
	this.queryResponse = queryResponse;

	if (null != queryResponse && null != queryResponse.getResponseHeader()) {
		// The NamedList thats returned by queryResponse.getResponseHeader() is not JSP friendly.
		// Convert it to a map to make it more accessible.
		final Map map = new LinkedHashMap();
		final NamedList<?> namedList = queryResponse.getResponseHeader();
		for (Map.Entry entry : namedList) {
			map.put(entry.getKey(), entry.getValue());
		}
		responseHeaderMap = Collections.unmodifiableMap(map);
	} else {			
		responseHeaderMap = Collections.emptyMap();
	}
	
	if (null != queryResponse && null != queryResponse.getResults()) {				
		final SolrDocumentList solrDocumentList = queryResponse.getResults();
		final Long numFound = solrDocumentList.getNumFound();
		final Long start = solrDocumentList.getStart();
		solrDocumentListNumFound = numFound.intValue();
		solrDocumentListStart = start.intValue();
		solrDocumentListSize = solrDocumentList.size();
	} else {
		solrDocumentListNumFound = 0;
		solrDocumentListStart = 0;
		solrDocumentListSize = 0;
	}
	
	cleanQueryAndResponseFacets();
}
 
Example 7
Source File: StandardSearchEngine.java    From document-management-software with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @see com.logicaldoc.core.searchengine.SearchEngine#getCount()
 */
@Override
public long getCount() {
	SolrQuery query = new SolrQuery();
	query.setQuery("*:*");
	try {
		QueryResponse rsp = server.query(query);
		SolrDocumentList docs = rsp.getResults();
		return docs.getNumFound();
	} catch (Throwable e) {
		log.error(e.getMessage(), e);
	}
	return 0;
}
 
Example 8
Source File: SolrSearchServer.java    From vind with Apache License 2.0 5 votes vote down vote up
@Override
public SearchResult execute(FulltextSearch search, DocumentFactory factory) {
    final SolrQuery query = buildSolrQuery(search, factory);
    //query
    try {
        solrClientLogger.debug(">>> query({})", query.toString());
        final QueryResponse response = solrClient.query(query, REQUEST_METHOD);
        if(response!=null){
            final SolrDocumentList responseResults = response.getResults();
            if (search.isSpellcheck() && responseResults.getNumFound() <=0 && !response.getSpellCheckResponse().isCorrectlySpelled()) {
                final FulltextSearch spellcheckSearch = search.copy().spellcheck(false).text(response.getSpellCheckResponse().getCollatedResult());
                return execute(spellcheckSearch, factory);
            }

            final Map<String,Integer> childCounts = SolrUtils.getChildCounts(response);

            final List<Document> documents = SolrUtils.Result.buildResultList(responseResults, childCounts, factory, search.getSearchContext());
            final FacetResults facetResults = SolrUtils.Result.buildFacetResult(response, factory, search.getChildrenFactory(), search.getFacets(),search.getSearchContext());

            switch(search.getResultSet().getType()) {
                case page:{
                    return new PageResult(responseResults.getNumFound(), response.getQTime(), documents, search, facetResults, this, factory).setElapsedTime(response.getElapsedTime());
                }
                case slice: {
                    return new SliceResult(responseResults.getNumFound(), response.getQTime(), documents, search, facetResults, this, factory).setElapsedTime(response.getElapsedTime());
                }
                default:
                    return new PageResult(responseResults.getNumFound(), response.getQTime(), documents, search, facetResults, this, factory).setElapsedTime(response.getElapsedTime());
            }
        }else {
            throw new SolrServerException("Null result from SolrClient");
        }

    } catch (SolrServerException | IOException e) {
        throw new SearchServerException("Cannot issue query", e);
    }
}
 
Example 9
Source File: PageTool.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public PageTool(SolrQueryRequest request, SolrQueryResponse response) {
  String rows = request.getParams().get("rows");

  if (rows != null) {
    results_per_page = Integer.parseInt(rows);
  }
  //TODO: Handle group by results
  Object docs = response.getResponse();
  if (docs != null) {
    if (docs instanceof DocSlice) {
      results_found = ((DocSlice) docs).matches();
      start = ((DocSlice) docs).offset();
    } else if(docs instanceof ResultContext) {
      DocList dl = ((ResultContext) docs).getDocList();
      results_found = dl.matches();
      start = dl.offset();
    } else if(docs instanceof SolrDocumentList) {
      SolrDocumentList doc_list = (SolrDocumentList) docs;
      results_found = doc_list.getNumFound();
      start = doc_list.getStart();
    } else {
      throw new SolrException(SolrException.ErrorCode.UNKNOWN, "Unknown response type "+docs+". Expected one of DocSlice, ResultContext or SolrDocumentList");
    }
  }

  page_count = (int) Math.ceil(results_found / (double) results_per_page);
  current_page_number = (int) Math.ceil(start / (double) results_per_page) + (page_count > 0 ? 1 : 0);
}
 
Example 10
Source File: MCRRSSFeedImporter.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
private boolean isAlreadyStored(String publicationID) {
    SolrClient solrClient = MCRSolrClientFactory.getMainSolrClient();
    SolrQuery query = new SolrQuery();
    query.setQuery(field2queryID + ":" + MCRSolrUtils.escapeSearchValue(publicationID));
    query.setRows(0);
    SolrDocumentList results;
    try {
        results = solrClient.query(query).getResults();
        return (results.getNumFound() > 0);
    } catch (Exception ex) {
        throw new MCRException(ex);
    }
}
 
Example 11
Source File: MCRNormalizeMCRObjectIDsFilter.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
private String retrieveMCRDerIDfromSOLR(String mcrid, String derid) {
    String key = derid.substring(0, derid.indexOf(":"));
    String value = derid.substring(derid.indexOf(":") + 1);
    if (SEARCHKEYS_FOR_DERIVATES.contains(key)) {
        ModifiableSolrParams params = new ModifiableSolrParams();
        params.set("start", 0);
        params.set("rows", 1);
        params.set("fl", "id");
        params.set("fq", "objectKind:mycorederivate");
        params.set("fq", "returnId:" + mcrid);
        params.set("q", key + ":" + ClientUtils.escapeQueryChars(value));
        params.set("sort", "derivateOrder asc");
        QueryResponse solrResponse = null;
        try {
            solrResponse = MCRSolrClientFactory.getMainSolrClient().query(params);
        } catch (Exception e) {
            LOGGER.error("Error retrieving derivate id from SOLR", e);
        }
        if (solrResponse != null) {
            SolrDocumentList solrResults = solrResponse.getResults();
            if (solrResults.getNumFound() == 1) {
                return String.valueOf(solrResults.get(0).getFieldValue("id"));
            }
            if (solrResults.getNumFound() == 0) {
                throw new NotFoundException("No MyCoRe Derivate ID found for query " + derid);
            }
            if (solrResults.getNumFound() > 1) {
                throw new BadRequestException(
                    "The query " + derid + " does not return a unique MyCoRe Derivate ID");
            }
        }
    }
    return derid;
}
 
Example 12
Source File: MCRNormalizeMCRObjectIDsFilter.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
private String retrieveMCRObjIDfromSOLR(String mcrid) {
    String key = mcrid.substring(0, mcrid.indexOf(":"));
    String value = mcrid.substring(mcrid.indexOf(":") + 1);
    if (SEARCHKEYS_FOR_OBJECTS.contains(key)) {
        ModifiableSolrParams params = new ModifiableSolrParams();
        params.set("start", 0);
        params.set("rows", 1);
        params.set("fl", "id");
        params.set("fq", "objectKind:mycoreobject");
        params.set("q", key + ":" + ClientUtils.escapeQueryChars(value));
        QueryResponse solrResponse = null;
        try {
            solrResponse = MCRSolrClientFactory.getMainSolrClient().query(params);
        } catch (Exception e) {
            LOGGER.error("Error retrieving object id from SOLR", e);
        }
        if (solrResponse != null) {
            SolrDocumentList solrResults = solrResponse.getResults();
            if (solrResults.getNumFound() == 1) {
                return String.valueOf(solrResults.get(0).getFieldValue("id"));
            }
            if (solrResults.getNumFound() == 0) {
                throw new NotFoundException("No MyCoRe ID found for query " + mcrid);
            }
            if (solrResults.getNumFound() > 1) {
                throw new BadRequestException("The query " + mcrid + " does not return a unique MyCoRe ID");
            }
        }
    }
    return mcrid;
}
 
Example 13
Source File: SolrTestCaseJ4.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public boolean compareSolrDocumentList(Object expected, Object actual) {
  if (!(expected instanceof SolrDocumentList)  || !(actual instanceof SolrDocumentList)) {
    return false;
  }

  if (expected == actual) {
    return true;
  }

  SolrDocumentList list1 = (SolrDocumentList) expected;
  SolrDocumentList list2 = (SolrDocumentList) actual;

  if (list1.getMaxScore() == null) {
    if (list2.getMaxScore() != null) {
      return false;
    } 
  } else if (list2.getMaxScore() == null) {
    return false;
  } else {
    if (Float.compare(list1.getMaxScore(), list2.getMaxScore()) != 0 || list1.getNumFound() != list2.getNumFound() ||
        list1.getStart() != list2.getStart()) {
      return false;
    }
  }
  for(int i=0; i<list1.getNumFound(); i++) {
    if(!compareSolrDocument(list1.get(i), list2.get(i))) {
      return false;
    }
  }
  return true;
}
 
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: TestSubQueryTransformerDistrib.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("serial")
@Test
public void test() throws Exception {
  int peopleMultiplier = atLeast(1);
  int deptMultiplier = atLeast(1);
  
  createIndex(people, peopleMultiplier, depts, deptMultiplier);
  
  Random random1 = random();
  
  final ModifiableSolrParams params = params(
      new String[]{"q","name_s:dave", "indent","true",
          "fl","*,depts:[subquery "+((random1.nextBoolean() ? "" : "separator=,"))+"]",
          "rows","" + peopleMultiplier,
          "depts.q","{!terms f=dept_id_s v=$row.dept_ss_dv "+((random1.nextBoolean() ? "" : "separator=,"))+"}",
          "depts.fl","text_t"+(differentUniqueId?",id:notid":""),
          "depts.sort", "dept_id_i desc",
          "depts.indent","true",
          "depts.collection","departments",
          differentUniqueId ? "depts.distrib.singlePass":"notnecessary","true",
          "depts.rows",""+(deptMultiplier*2),
          "depts.logParamsList","q,fl,rows,row.dept_ss_dv",
          random().nextBoolean()?"depts.wt":"whatever",anyWt(),
          random().nextBoolean()?"wt":"whatever",anyWt()});

  final SolrDocumentList hits;
  {
    final QueryRequest qr = new QueryRequest(params);
    final QueryResponse  rsp = new QueryResponse();
    rsp.setResponse(cluster.getSolrClient().request(qr, people+","+depts));
    hits = rsp.getResults();
    
    assertEquals(peopleMultiplier, hits.getNumFound());
    
    int engineerCount = 0;
    int supportCount = 0;
    
    for (int res : new int [] {0, (peopleMultiplier-1) /2, peopleMultiplier-1}) {
      SolrDocument doc = hits.get(res);
      assertEquals("dave", doc.getFieldValue("name_s_dv"));
      SolrDocumentList relDepts = (SolrDocumentList) doc.getFieldValue("depts");
      assertEquals("dave works in both depts "+rsp,
          deptMultiplier * 2, relDepts.getNumFound());
      for (int deptN = 0 ; deptN < relDepts.getNumFound(); deptN++ ) {
        SolrDocument deptDoc = relDepts.get(deptN);
        String actual = (String) deptDoc.get("text_t");
        assertTrue(deptDoc + "should be either "+engineering +" or "+support,
            (engineering.equals(actual) && ++engineerCount>0) || 
                 (support.equals(actual) && ++supportCount>0));
      }
    }
    assertEquals(hits.toString(), engineerCount, supportCount); 
  }

  params.set("wt", "json");
  final URL node = new URL(cluster.getRandomJetty(random()).getBaseUrl().toString()
   +"/"+people+"/select"+params.toQueryString());

  try(final InputStream jsonResponse = node.openStream()){
    final ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
    IOUtils.copy(jsonResponse, outBuffer);

    final Object expected = ((SolrDocumentList) hits.get(0).getFieldValue("depts")).get(0).get("text_t");
    final String err = JSONTestUtil.match("/response/docs/[0]/depts/docs/[0]/text_t"
        ,outBuffer.toString(Charset.forName("UTF-8").toString()),
        "\""+expected+"\"");
    assertNull(err,err);
  }
  
}
 
Example 16
Source File: AmbariInfraWithStormLogSearch.java    From streamline with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public LogSearchResult search(LogSearchCriteria logSearchCriteria) {
    SolrQuery query = new SolrQuery();

    query.setQuery(buildColumnAndValue(COLUMN_NAME_LOG_MESSAGE, buildValue(logSearchCriteria.getSearchString())));
    query.addFilterQuery(buildColumnAndValue(COLUMN_NAME_TYPE, COLUMN_VALUE_TYPE_WORKER_LOG));
    query.addFilterQuery(buildColumnAndValue(COLUMN_NAME_STREAMLINE_TOPOLOGY_ID, buildValue(logSearchCriteria.getAppId())));
    query.addFilterQuery(buildColumnAndValue(COLUMN_NAME_LOG_TIME, buildDateRangeValue(logSearchCriteria.getFrom(), logSearchCriteria.getTo())));

    List<String> componentNames = logSearchCriteria.getComponentNames();
    if (componentNames != null && !componentNames.isEmpty()) {
        query.addFilterQuery(buildColumnAndValue(COLUMN_NAME_STREAMLINE_COMPONENT_NAME, buildORValues(componentNames)));
    }

    List<String> logLevels = logSearchCriteria.getLogLevels();
    if (logLevels == null || logLevels.isEmpty()) {
        logLevels = DEFAULT_LOG_LEVELS;
    }
    query.addFilterQuery(buildColumnAndValue(COLUMN_NAME_LOG_LEVEL, buildORValues(logLevels)));

    if (logSearchCriteria.getAscending() == null || logSearchCriteria.getAscending()) {
        query.addSort(COLUMN_NAME_LOG_TIME, SolrQuery.ORDER.asc);
    } else {
        query.addSort(COLUMN_NAME_LOG_TIME, SolrQuery.ORDER.desc);
    }

    if (logSearchCriteria.getStart() != null) {
        query.setStart(logSearchCriteria.getStart());
    }
    if (logSearchCriteria.getLimit() != null) {
        query.setRows(logSearchCriteria.getLimit());
    }

    LOG.debug("Querying to Solr: query => {}", query);

    long numFound;
    List<LogSearchResult.LogDocument> results = new ArrayList<>();
    try {
        QueryResponse response = solr.query(query);

        SolrDocumentList docList = response.getResults();
        numFound = docList.getNumFound();

        for (SolrDocument document : docList) {
            String appId = (String) document.getFieldValue(COLUMN_NAME_STREAMLINE_TOPOLOGY_ID);
            String componentName = (String) document.getFieldValue(COLUMN_NAME_STREAMLINE_COMPONENT_NAME);
            String logLevel = (String) document.getFieldValue(COLUMN_NAME_LOG_LEVEL);
            String logMessage = (String) document.getFieldValue(COLUMN_NAME_LOG_MESSAGE);
            String host = (String) document.getFieldValue(COLUMN_NAME_HOST);
            String port = (String) document.getFieldValue(COLUMN_NAME_STORM_WORKER_PORT);
            Date logDate = (Date) document.getFieldValue(COLUMN_NAME_LOG_TIME);
            long timestamp = logDate.toInstant().toEpochMilli();

            LogSearchResult.LogDocument logDocument = new LogSearchResult.LogDocument(appId, componentName,
                    logLevel, logMessage, host, port != null ? Integer.parseInt(port) : null, timestamp);
            results.add(logDocument);
        }

    } catch (SolrServerException | IOException e) {
        // TODO: any fine-grained control needed?
        throw new RuntimeException(e);
    }

    return new LogSearchResult(numFound, results);
}
 
Example 17
Source File: CloudInspectUtil.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * Compares the results of the control and cloud clients.
 * 
 * @return true if the compared results are illegal.
 */
public static boolean compareResults(SolrClient controlClient, SolrClient cloudClient, Set<String> addFails, Set<String> deleteFails)
    throws SolrServerException, IOException {
  
  SolrParams q = SolrTestCaseJ4.params("q","*:*","rows","0", "tests","checkShardConsistency(vsControl)");    // add a tag to aid in debugging via logs

  SolrDocumentList controlDocList = controlClient.query(q).getResults();
  long controlDocs = controlDocList.getNumFound();

  SolrDocumentList cloudDocList = cloudClient.query(q).getResults();
  long cloudClientDocs = cloudDocList.getNumFound();
  
  // re-execute the query getting ids
  q = SolrTestCaseJ4.params("q", "*:*", "rows", "100000", "fl", "id", "tests", "checkShardConsistency(vsControl)/getIds");    // add a tag to aid in debugging via logs
  controlDocList = controlClient.query(q).getResults();
  if (controlDocs != controlDocList.getNumFound()) {
    log.error("Something changed! control now {}", controlDocList.getNumFound());
  }

  cloudDocList = cloudClient.query(q).getResults();
  if (cloudClientDocs != cloudDocList.getNumFound()) {
    log.error("Something changed! cloudClient now {}", cloudDocList.getNumFound());
  }

  if (controlDocs != cloudClientDocs && (addFails != null || deleteFails != null)) {
    boolean legal = CloudInspectUtil.checkIfDiffIsLegal(controlDocList, cloudDocList,
        "controlDocList", "cloudDocList", addFails, deleteFails);
    if (legal) {
      return false;
    }
  }
  
  @SuppressWarnings({"rawtypes"})
  Set<Map> differences = CloudInspectUtil.showDiff(controlDocList, cloudDocList,
      "controlDocList", "cloudDocList");

  try {
    // get versions for the mismatched ids
    boolean foundId = false;

    // use filter() to allow being parsed as 'terms in set' query instead of a (weighted/scored)
    // BooleanQuery so we don't trip too many boolean clauses
    StringBuilder ids = new StringBuilder("filter(id:(");
    for (@SuppressWarnings({"rawtypes"})Map doc : differences) {
      ids.append(" ").append(doc.get("id"));
      foundId = true;
    }
    ids.append("))");

    if (foundId) {
      // get versions for those ids that don't match
      q = SolrTestCaseJ4.params("q", ids.toString(), "rows", "100000", "fl", "id,_version_",
          "sort", "id asc", "tests",
          "checkShardConsistency(vsControl)/getVers"); // add a tag to aid in
      // debugging via logs

      // use POST, the ids in the query above is constructed and could be huge
      SolrDocumentList a = controlClient.query(q, SolrRequest.METHOD.POST).getResults();
      SolrDocumentList b = cloudClient.query(q, SolrRequest.METHOD.POST).getResults();

      log.error("controlClient :{}\n\tcloudClient :{}", a, b);
    }
  } catch (Exception e) {
    // swallow any exceptions, this is just useful for producing debug output,
    // and shouldn't usurp the original issue with mismatches.
    log.error("Unable to find versions for mismatched ids", e);
  }

  return true;
}
 
Example 18
Source File: SolrInformationServer.java    From SearchServices with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public TrackerState getTrackerInitialState()
{
    try (SolrQueryRequest request = newSolrQueryRequest())
    {
        TrackerState state = new TrackerState();
        SolrRequestHandler handler = core.getRequestHandler(REQUEST_HANDLER_GET);

        ModifiableSolrParams newParams =
                new ModifiableSolrParams(request.getParams())
                        .set("ids", "TRACKER!STATE!ACLTX,TRACKER!STATE!TX");
        request.setParams(newParams);

        SolrDocumentList response = executeQueryRequest(request, newSolrQueryResponse(), handler);
        if (response == null)
        {
            LOGGER.error("Got no response from a tracker initial state request.");
            return state;
        }

        for (int i = 0; i < response.getNumFound(); i++)
        {
            SolrDocument current = response.get(i);
            // ACLTX
            if (current.getFieldValue(FIELD_S_ACLTXCOMMITTIME) != null)
            {
                if (state.getLastIndexedChangeSetCommitTime() == 0)
                {
                    state.setLastIndexedChangeSetCommitTime(getFieldValueLong(current, FIELD_S_ACLTXCOMMITTIME));
                }

                if (state.getLastIndexedChangeSetId() == 0)
                {
                    state.setLastIndexedChangeSetId(getFieldValueLong(current, FIELD_S_ACLTXID));
                }
            }

            // TX
            if (current.getFieldValue(FIELD_S_TXCOMMITTIME) != null)
            {
                if (state.getLastIndexedTxCommitTime() == 0)
                {
                    state.setLastIndexedTxCommitTime(getFieldValueLong(current, FIELD_S_TXCOMMITTIME));
                }

                if (state.getLastIndexedTxId() == 0)
                {
                    state.setLastIndexedTxId(getFieldValueLong(current, FIELD_S_TXID));
                }
            }
        }

        long startTime = System.currentTimeMillis();
        state.setLastStartTime(startTime);
        state.setTimeToStopIndexing(startTime - lag);
        state.setTimeBeforeWhichThereCanBeNoHoles(startTime - holeRetention);

        long timeBeforeWhichThereCanBeNoTxHolesInIndex = state.getLastIndexedTxCommitTime() - holeRetention;
        state.setLastGoodTxCommitTimeInIndex(timeBeforeWhichThereCanBeNoTxHolesInIndex > 0 ? timeBeforeWhichThereCanBeNoTxHolesInIndex : 0);

        long timeBeforeWhichThereCanBeNoChangeSetHolesInIndex = state.getLastIndexedChangeSetCommitTime() - holeRetention;
        state.setLastGoodChangeSetCommitTimeInIndex(timeBeforeWhichThereCanBeNoChangeSetHolesInIndex > 0 ? timeBeforeWhichThereCanBeNoChangeSetHolesInIndex : 0);

        return state;
    }
}
 
Example 19
Source File: SolrControllerTest.java    From Spring-Boot-Book with Apache License 2.0 4 votes vote down vote up
@Test
    public void queryAll() throws IOException, SolrServerException {


        //第二种方式
        SolrQuery solrQuery = new SolrQuery();
        // 设置默认搜索域
        solrQuery.setQuery("*:*");
//        solrQuery.addField("*");
        solrQuery.set("q", "知然");
        solrQuery.add("q", "name:然");
        // 设置返回结果的排序规则
        solrQuery.setSort("id", SolrQuery.ORDER.asc);
        //设置查询的条数
        solrQuery.setRows(50);
        //设置查询的开始
        solrQuery.setStart(0);
        // 设置分页参数
        solrQuery.setStart(0);
        solrQuery.setRows(20);
        //设置高亮
        solrQuery.setHighlight(true);
        //设置高亮的字段
        solrQuery.addHighlightField("name");
        //设置高亮的样式
        solrQuery.setHighlightSimplePre("<font color='red'>");
        solrQuery.setHighlightSimplePost("</font>");
        System.out.println(solrQuery);
        QueryResponse response = solrClient.query(solrQuery);
        //返回高亮显示结果
        Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
        //response.getResults();查询返回的结果

        SolrDocumentList documentList = response.getResults();
        long numFound = documentList.getNumFound();
        System.out.println("总共查询到的文档数量: " + numFound);
        for (SolrDocument solrDocument : documentList) {
            System.out.println(solrDocument);
            System.out.println(solrDocument.get("name"));
        }
        System.out.println(highlighting);
    }
 
Example 20
Source File: SolrController.java    From Spring-Boot-Book with Apache License 2.0 4 votes vote down vote up
@RequestMapping("/queryAll")
    public Object queryAll() throws IOException, SolrServerException {


        //第二种方式
        SolrQuery solrQuery = new SolrQuery();
        // 设置默认搜索域
        solrQuery.setQuery("*:*");
//        solrQuery.addField("*");
        solrQuery.set("q", "知然");
        solrQuery.add("q", "name:然");
        // 设置返回结果的排序规则
        solrQuery.setSort("id", SolrQuery.ORDER.asc);
        //设置查询的条数
        solrQuery.setRows(50);
        //设置查询的开始
        solrQuery.setStart(0);
        // 设置分页参数
        solrQuery.setStart(0);
        solrQuery.setRows(20);
        //设置高亮
        solrQuery.setHighlight(true);
        //设置高亮的字段
        solrQuery.addHighlightField("name");
        //设置高亮的样式
        solrQuery.setHighlightSimplePre("<font color='red'>");
        solrQuery.setHighlightSimplePost("</font>");
        System.out.println(solrQuery);
        QueryResponse response = solrClient.query(solrQuery);
        //返回高亮显示结果
        Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
        //response.getResults();查询返回的结果

        SolrDocumentList documentList = response.getResults();
        long numFound = documentList.getNumFound();
        System.out.println("总共查询到的文档数量: " + numFound);
        for (SolrDocument solrDocument : documentList) {
            System.out.println("solrDocument==============" + solrDocument);
            System.out.println("solrDocument==============" + solrDocument.get("name"));
        }
        return highlighting;
    }