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

The following examples show how to use org.apache.solr.common.SolrDocumentList#setNumFound() . 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: JavaBinCodec.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public SolrDocumentList readSolrDocumentList(DataInputInputStream dis) throws IOException {
  SolrDocumentList solrDocs = new SolrDocumentList();
  @SuppressWarnings("unchecked")
  List<Object> list = (List<Object>) readVal(dis);
  solrDocs.setNumFound((Long) list.get(0));
  solrDocs.setStart((Long) list.get(1));
  solrDocs.setMaxScore((Float) list.get(2));
  if (list.size() > 3) { //needed for back compatibility
    solrDocs.setNumFoundExact((Boolean)list.get(3));
  }

  @SuppressWarnings("unchecked")
  List<SolrDocument> l = (List<SolrDocument>) readVal(dis);
  solrDocs.addAll(l);
  return solrDocs;
}
 
Example 2
Source File: FastJavaBinDecoder.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings({"unchecked"})
public Object readObject(StreamCodec codec, EntryImpl entry) throws IOException {
  SolrDocumentList solrDocs = new SolrDocumentList();
  if(entry.metadata != null){
    @SuppressWarnings({"rawtypes"})
    List list = (List) entry.metadata;
    solrDocs.setNumFound((Long) list.get(0));
    solrDocs.setStart((Long) list.get(1));
    solrDocs.setMaxScore((Float) list.get(2));
    if (list.size() > 3) { //needed for back compatibility
      solrDocs.setNumFoundExact((Boolean)list.get(3));
    }
  }
  List<SolrDocument> l =  codec.readArray(codec.dis, entry.size);
  solrDocs.addAll(l);
  return solrDocs;
}
 
Example 3
Source File: MockSolrEntityProcessor.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private SolrDocumentList getDocs(int start, int rows) {
  SolrDocumentList docs = new SolrDocumentList();
  docs.setNumFound(docsData.size());
  docs.setStart(start);

  int endIndex = start + rows;
  int end = docsData.size() < endIndex ? docsData.size() : endIndex;
  for (int i = start; i < end; i++) {
    SolrDocument doc = new SolrDocument();
    SolrTestCaseJ4.Doc testDoc = docsData.get(i);
    doc.addField("id", testDoc.id);
    doc.addField("description", testDoc.getValues("description"));
    docs.add(doc);
  }
  return docs;
}
 
Example 4
Source File: LogWatcher.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public SolrDocumentList getHistory(long since, AtomicBoolean found) {
  if(history==null) {
    return null;
  }
  
  SolrDocumentList docs = new SolrDocumentList();
  Iterator<E> iter = history.iterator();
  while(iter.hasNext()) {
    E e = iter.next();
    long ts = getTimestamp(e);
    if(ts == since) {
      if(found!=null) {
        found.set(true);
      }
    }
    if(ts>since) {
      docs.add(toSolrDocument(e));
    }
  }
  docs.setNumFound(docs.size()); // make it not look too funny
  return docs;
}
 
Example 5
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 6
Source File: TestRandomFlRTGCloud.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** 
 * trivial helper method to deal with diff response structure between using a single 'id' param vs
 * 2 or more 'id' params (or 1 or more 'ids' params).
 *
 * @return List from response, or a synthetic one created from single response doc if 
 * <code>expectList</code> was false; May be empty; May be null if response included null list.
 */
private static SolrDocumentList getDocsFromRTGResponse(final boolean expectList, final QueryResponse rsp) {
  if (expectList) {
    return rsp.getResults();
  }
  
  // else: expect single doc, make our own list...
  
  final SolrDocumentList result = new SolrDocumentList();
  NamedList<Object> raw = rsp.getResponse();
  Object doc = raw.get("doc");
  if (null != doc) {
    result.add((SolrDocument) doc);
    result.setNumFound(1);
  }
  return result;
}
 
Example 7
Source File: SolrClientInterceptorTest.java    From skywalking with Apache License 2.0 6 votes vote down vote up
private NamedList<Object> getQueryResponse() {
    NamedList<Object> response = new NamedList<Object>();
    response.add("responseHeader", header);
    SolrDocumentList list = new SolrDocumentList();
    list.setStart(0);
    list.setNumFound(100);
    list.setMaxScore(.0f);

    for (int start = 0; start < 10; start++) {
        SolrDocument doc = new SolrDocument();
        doc.addField("id", start);
        doc.addField("_version", 1634676349644832768L);
        list.add(doc);
    }
    response.add("response", list);
    return response;
}
 
Example 8
Source File: SolrClientInterceptorTest.java    From skywalking with Apache License 2.0 6 votes vote down vote up
private NamedList<Object> getGetResponse() {
    NamedList<Object> response = new NamedList<Object>();
    response.add("responseHeader", header);
    SolrDocumentList list = new SolrDocumentList();
    list.setStart(0);
    list.setNumFound(1);
    list.setMaxScore(.0f);

    SolrDocument doc = new SolrDocument();
    doc.addField("id", 1);
    doc.addField("_version", 1634676349644832768L);
    list.add(doc);

    response.add("response", list);
    return response;
}
 
Example 9
Source File: AutoCompleteSearchComponent.java    From solr-autocomplete with Apache License 2.0 5 votes vote down vote up
private void mergeDistributedResultsIntSingleResponse(ResponseBuilder rb, List<AcGroupResult> resultsToMerge, String responseTagName) {
  int docs = 0;
  float maxScore = 0.0f;
  
  // if groups have to be displayed in some custom order, other than the order specified in 
  // ac_grouping_field_def
  String groupingSort = rb.req.getParams().get(AC_GROUPING_SORT_PARAM_NAME);
  if (groupSorts.containsKey(groupingSort)) {
    groupSorts.get(groupingSort).sort(rb, resultsToMerge);
  }
  
  SolrDocumentList docList = new SolrDocumentList();
  // first find count of documents
  for (AcGroupResult acGroupResult : resultsToMerge) {
    // if slice contains more results than requested, take requested count; if it contains less results than
    // requested, we have to take what we got, not more
    docs += ((acGroupResult.getDistributedResultingDocs().size() > acGroupResult.getAcGroupingFieldValue().getRequestedCountOfSuggestions() == true) ? 
        acGroupResult.getAcGroupingFieldValue().getRequestedCountOfSuggestions() : acGroupResult.getDistributedResultingDocs().size());
    
    if (acGroupResult.getDistributedResultingDocs().getMaxScore() > maxScore) {
      maxScore = acGroupResult.getDistributedResultingDocs().getMaxScore();
    }
    
    docList.addAll(acGroupResult.getDistributedResultingDocs());
  }
  
  docList.setStart(0);
  docList.setNumFound(docs);

  rb.rsp.add(responseTagName, docList);
}
 
Example 10
Source File: XMLResponseParser.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected SolrDocumentList readDocuments( XMLStreamReader parser ) throws XMLStreamException
{
  SolrDocumentList docs = new SolrDocumentList();

  // Parse the attributes
  for( int i=0; i<parser.getAttributeCount(); i++ ) {
    String n = parser.getAttributeLocalName( i );
    String v = parser.getAttributeValue( i );
    if( "numFound".equals( n ) ) {
      docs.setNumFound( Long.parseLong( v ) );
    }
    else if( "start".equals( n ) ) {
      docs.setStart( Long.parseLong( v ) );
    }
    else if( "maxScore".equals( n ) ) {
      docs.setMaxScore( Float.parseFloat( v ) );
    }
  }

  // Read through each document
  int event;
  while( true ) {
    event = parser.next();
    if( XMLStreamConstants.START_ELEMENT == event ) {
      if( !"doc".equals( parser.getLocalName() ) ) {
        throw new RuntimeException( "should be doc! "+parser.getLocalName() + " :: " + parser.getLocation() );
      }
      docs.add( readDocument( parser ) );
    }
    else if ( XMLStreamConstants.END_ELEMENT == event ) {
      return docs;  // only happens once
    }
  }
}
 
Example 11
Source File: SimClusterStateProvider.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public QueryResponse simQuery(QueryRequest req) throws SolrException, InterruptedException, IOException {
  ensureNotClosed();
  String collection = req.getCollection();
  if (collection == null) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Collection not set");
  }
  ensureSystemCollection(collection);
  if (!colShardReplicaMap.containsKey(collection)) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Collection does not exist");
  }
  String query = req.getParams().get(CommonParams.Q);
  if (query == null || !query.equals("*:*")) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Only '*:*' query is supported");
  }
  ClusterState clusterState = getClusterState();
  DocCollection coll = clusterState.getCollection(collection);
  AtomicLong count = new AtomicLong();
  for (Slice s : coll.getActiveSlicesArr()) {
    Replica r = s.getLeader();
    if (r == null) {
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, collection + "/" + s.getName() + " has no leader");
    }
    ReplicaInfo ri = getReplicaInfo(r);
    Number numDocs = (Number)ri.getVariable("SEARCHER.searcher.numDocs", 0L);
    count.addAndGet(numDocs.longValue());
    AtomicLong bufferedUpdates = (AtomicLong)sliceProperties.get(collection).get(s.getName()).get(BUFFERED_UPDATES);
    if (bufferedUpdates != null) {
      count.addAndGet(bufferedUpdates.get());
    }
  }
  QueryResponse rsp = new QueryResponse();
  NamedList<Object> values = new NamedList<>();
  values.add("responseHeader", new NamedList<>());
  SolrDocumentList docs = new SolrDocumentList();
  docs.setNumFound(count.get());
  values.add("response", docs);
  rsp.setResponse(values);
  return rsp;
}
 
Example 12
Source File: TestJavaBinCodec.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({"unchecked"})
private List<Object> generateAllDataTypes() {
  List<Object> types = new ArrayList<>();

  types.add(null); //NULL
  types.add(true);
  types.add(false);
  types.add((byte) 1);
  types.add((short) 2);
  types.add((double) 3);

  types.add(-4);
  types.add(4);
  types.add(42);

  types.add((long) -5);
  types.add((long) 5);
  types.add((long) 50);

  types.add((float) 6);
  types.add(new Date(0));

  Map<Integer, Integer> map = new HashMap<>();
  map.put(1, 2);
  types.add(map);

  SolrDocument doc = new SolrDocument();
  doc.addField("foo", "bar");
  types.add(doc);

  SolrDocumentList solrDocs = new SolrDocumentList();
  solrDocs.setMaxScore(1.0f);
  solrDocs.setNumFound(1);
  solrDocs.setNumFoundExact(Boolean.TRUE);
  solrDocs.setStart(0);
  solrDocs.add(0, doc);
  types.add(solrDocs);

  types.add(new byte[] {1,2,3,4,5});

  // TODO?
  // List<String> list = new ArrayList<String>();
  // list.add("one");
  // types.add(list.iterator());

  types.add((byte) 15); //END

  SolrInputDocument idoc = new SolrInputDocument();
  idoc.addField("foo", "bar");
  types.add(idoc);

  SolrInputDocument parentDoc = new SolrInputDocument();
  parentDoc.addField("foo", "bar");
  SolrInputDocument childDoc = new SolrInputDocument();
  childDoc.addField("foo", "bar");
  parentDoc.addChildDocument(childDoc);
  types.add(parentDoc);

  types.add(new EnumFieldValue(1, "foo"));

  types.add(map.entrySet().iterator().next()); //Map.Entry

  types.add((byte) (1 << 5)); //TAG_AND_LEN

  types.add("foo");
  types.add(1);
  types.add((long) 2);

  @SuppressWarnings({"rawtypes"})
  SimpleOrderedMap simpleOrderedMap = new SimpleOrderedMap();
  simpleOrderedMap.add("bar", "barbar");
  types.add(simpleOrderedMap);

  NamedList<String> nl = new NamedList<>();
  nl.add("foo", "barbar");
  types.add(nl);

  return types;
}
 
Example 13
Source File: JSONWriterTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testJSONSolrDocument() throws Exception {
  SolrQueryRequest req = req(CommonParams.WT,"json",
                             CommonParams.FL,"id,score,_children_,path");
  SolrQueryResponse rsp = new SolrQueryResponse();
  JSONResponseWriter w = new JSONResponseWriter();

  ReturnFields returnFields = new SolrReturnFields(req);
  rsp.setReturnFields(returnFields);

  StringWriter buf = new StringWriter();

  SolrDocument childDoc = new SolrDocument();
  childDoc.addField("id", "2");
  childDoc.addField("score", "0.4");
  childDoc.addField("path", Arrays.asList("a>b", "a>b>c"));

  SolrDocumentList childList = new SolrDocumentList();
  childList.setNumFound(1);
  childList.setStart(0);
  childList.add(childDoc);

  SolrDocument solrDoc = new SolrDocument();
  solrDoc.addField("id", "1");
  solrDoc.addField("subject", "hello2");
  solrDoc.addField("title", "hello3");
  solrDoc.addField("score", "0.7");
  solrDoc.setField("_children_", childList);

  SolrDocumentList list = new SolrDocumentList();
  list.setNumFound(1);
  list.setStart(0);
  list.setMaxScore(0.7f);
  list.add(solrDoc);

  rsp.addResponse(list);

  w.write(buf, req, rsp);
  String result = buf.toString();
  assertFalse("response contains unexpected fields: " + result, 
              result.contains("hello") || 
              result.contains("\"subject\"") || 
              result.contains("\"title\""));
  assertTrue("response doesn't contain expected fields: " + result, 
             result.contains("\"id\"") &&
             result.contains("\"score\"") && result.contains("_children_"));

  String expectedResult = "{'response':{'numFound':1,'start':0,'maxScore':0.7, 'numFoundExact':true,'docs':[{'id':'1', 'score':'0.7'," +
      " '_children_':{'numFound':1,'start':0,'numFoundExact':true,'docs':[{'id':'2', 'score':'0.4', 'path':['a>b', 'a>b>c']}] }}] }}";
  String error = JSONTestUtil.match(result, "=="+expectedResult);
  assertNull("response validation failed with error: " + error, error);

  req.close();
}
 
Example 14
Source File: AnalysisHandler.java    From chronix.server with Apache License 2.0 4 votes vote down vote up
/**
 * Executes the user search request.
 *
 * @param req the solr query request
 * @param rsp the solr query response holding the result
 * @throws Exception if bad things happen
 */
@Override
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
    LOGGER.debug("Handling analysis request {}", req);
    //First check if the request should return documents => rows > 0
    final String rowsParam = req.getParams().get(CommonParams.ROWS, null);
    int rows = -1;
    if (rowsParam != null) {
        rows = Integer.parseInt(rowsParam);
    }

    SolrDocumentList results = new SolrDocumentList();

    final CQL cql = new CQL(TYPES, FUNCTIONS);

    //get the chronix join parameter
    final String chronixJoin = req.getParams().get(ChronixQueryParams.CHRONIX_JOIN);
    final CQLJoinFunction key = cql.parseCJ(chronixJoin);

    //Do a query and collect them on the join function
    final Map<ChronixType, Map<String, List<SolrDocument>>> collectedDocs = collectDocuments(req, key);

    //If no rows should returned, we only return the num found
    if (rows == 0) {
        results.setNumFound(collectedDocs.keySet().size());
    } else {
        //Otherwise return the analyzed time series

        final String chronixFunctions = req.getParams().get(ChronixQueryParams.CHRONIX_FUNCTION);
        final CQLCFResult result = cql.parseCF(chronixFunctions);

        final List<SolrDocument> resultDocuments = analyze(req, result, collectedDocs);
        results.addAll(resultDocuments);
        //As we have to analyze all docs in the query at once,
        // the number of documents is also the number of documents found
        results.setNumFound(resultDocuments.size());
    }
    //Add the results to the response
    rsp.add("response", results);
}
 
Example 15
Source File: SolrSearchDaoTest.java    From metron with Apache License 2.0 4 votes vote down vote up
@Test
public void buildSearchResponseShouldReturnSearchResponse() {
  SearchRequest searchRequest = new SearchRequest();
  searchRequest.setFields(Collections.singletonList("id"));
  searchRequest.setFacetFields(Collections.singletonList("facetField"));
  QueryResponse queryResponse = mock(QueryResponse.class);
  SolrDocument solrDocument1 = new SolrDocument();
  solrDocument1.setField(Constants.GUID, "id1");
  solrDocument1.setField("id", "id1");
  SolrDocument solrDocument2 = new SolrDocument();
  solrDocument2.setField(Constants.GUID, "id2");
  solrDocument2.setField("id", "id2");

  solrSearchDao = spy(new SolrSearchDao(client, accessConfig));
  SolrDocumentList solrDocumentList = new SolrDocumentList();
  solrDocumentList.add(solrDocument1);
  solrDocumentList.add(solrDocument2);
  solrDocumentList.setNumFound(100);
  when(queryResponse.getResults()).thenReturn(solrDocumentList);
  SearchResult searchResult1 = new SearchResult();
  searchResult1.setId("id1");
  HashMap<String, Object> source1 = new HashMap<>();
  source1.put("id", "id1");
  searchResult1.setSource(source1);
  SearchResult searchResult2 = new SearchResult();
  searchResult2.setId("id2");
  HashMap<String, Object> source2 = new HashMap<>();
  source2.put("id", "id2");
  searchResult2.setSource(source2);
  Map<String, Map<String, Long>> facetCounts = new HashMap<String, Map<String, Long>>() {{
    put("id", new HashMap<String, Long>() {{
      put("id1", 1L);
      put("id2", 1L);
    }});
  }};
  doReturn(facetCounts).when(solrSearchDao)
      .getFacetCounts(Collections.singletonList("facetField"), queryResponse);
  SearchResponse expectedSearchResponse = new SearchResponse();
  SearchResult expectedSearchResult1 = new SearchResult();
  expectedSearchResult1.setId("id1");
  expectedSearchResult1.setSource(source1);
  SearchResult expectedSearchResult2 = new SearchResult();
  expectedSearchResult2.setId("id2");
  expectedSearchResult2.setSource(source2);

  expectedSearchResponse.setResults(Arrays.asList(expectedSearchResult1, expectedSearchResult2));
  expectedSearchResponse.setTotal(100);
  expectedSearchResponse.setFacetCounts(facetCounts);

  assertEquals(expectedSearchResponse,
      solrSearchDao.buildSearchResponse(searchRequest, queryResponse));
}
 
Example 16
Source File: BlurResultHelper.java    From incubator-retired-blur with Apache License 2.0 4 votes vote down vote up
private static void convertMetadata(BlurResults results, SolrDocumentList docResults) {
  docResults.setNumFound(results.getTotalResults());
  docResults.setStart(results.getQuery().getStart());
}