org.apache.solr.search.DocIterator Java Examples

The following examples show how to use org.apache.solr.search.DocIterator. 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: TestXJoinSearchComponent.java    From BioSolr with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void testGroupedSimple() {
  ModifiableSolrParams params = new ModifiableSolrParams();    
  params.add("group", "true");
  params.add("group.field", "colour");
  params.add("group.format", "simple");
  NamedList results = test(params, "xjoin");
  testXJoinResults(results, "xjoin");
  NamedList grouped = (NamedList)results.get("grouped");
  NamedList colours = (NamedList)grouped.get("colour");
  assertEquals(2, colours.get("matches"));
  DocList docs = (DocList)colours.get("doclist");
  assertEquals(docs.size(), 2);
  DocIterator it = docs.iterator();
  assertTrue(it.hasNext());
  assertEquals(1, it.nextDoc());
  assertTrue(it.hasNext());
  assertEquals(3, it.nextDoc());
  assertFalse(it.hasNext());
}
 
Example #2
Source File: TestXJoinSearchComponent.java    From BioSolr with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void testGroupedSimple() {
  ModifiableSolrParams params = new ModifiableSolrParams();    
  params.add("group", "true");
  params.add("group.field", "colour");
  params.add("group.format", "simple");
  NamedList results = test(params, "xjoin");
  testXJoinResults(results, "xjoin");
  NamedList grouped = (NamedList)results.get("grouped");
  NamedList colours = (NamedList)grouped.get("colour");
  assertEquals(2, colours.get("matches"));
  DocList docs = (DocList)colours.get("doclist");
  assertEquals(docs.size(), 2);
  DocIterator it = docs.iterator();
  assertTrue(it.hasNext());
  assertEquals(1, it.nextDoc());
  assertTrue(it.hasNext());
  assertEquals(3, it.nextDoc());
  assertFalse(it.hasNext());
}
 
Example #3
Source File: TestXJoinSearchComponent.java    From BioSolr with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void testUngrouped() {
  ModifiableSolrParams params = new ModifiableSolrParams();
  NamedList results = test(params, "xjoin");
  testXJoinResults(results, "xjoin");
  ResultContext response = (ResultContext)results.get("response");
  DocList docs = response.docs;
  assertEquals(2, docs.size());
  DocIterator it = docs.iterator();
  assertTrue(it.hasNext());
  assertEquals(1, it.nextDoc());
  assertTrue(it.hasNext());
  assertEquals(3, it.nextDoc());
  assertFalse(it.hasNext());
}
 
Example #4
Source File: TestXJoinQParserPlugin.java    From BioSolr with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiValued() throws Exception {
  Query q = parse(COMPONENT_NAME_4);
  DocSet docs = searcher.getDocSet(q);

  assertEquals(4, docs.size());
  DocIterator it = docs.iterator();
  assertTrue(it.hasNext());
  assertEquals(0, it.nextDoc());
  assertTrue(it.hasNext());
  assertEquals(1, it.nextDoc());
  assertTrue(it.hasNext());
  assertEquals(2, it.nextDoc());
  assertTrue(it.hasNext());
  assertEquals(3, it.nextDoc());
  assertFalse(it.hasNext());    
}
 
Example #5
Source File: TestXJoinQParserPlugin.java    From BioSolr with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiValued() throws Exception {
  Query q = parse(COMPONENT_NAME_4);
  DocSet docs = searcher.getDocSet(q);

  assertEquals(4, docs.size());
  DocIterator it = docs.iterator();
  assertTrue(it.hasNext());
  assertEquals(0, it.nextDoc());
  assertTrue(it.hasNext());
  assertEquals(1, it.nextDoc());
  assertTrue(it.hasNext());
  assertEquals(2, it.nextDoc());
  assertTrue(it.hasNext());
  assertEquals(3, it.nextDoc());
  assertFalse(it.hasNext());    
}
 
Example #6
Source File: TestXJoinSearchComponent.java    From BioSolr with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void testGroupedSimple() {
  ModifiableSolrParams params = new ModifiableSolrParams();    
  params.add("group", "true");
  params.add("group.field", "colour");
  params.add("group.format", "simple");
  NamedList results = test(params, "xjoin");
  testXJoinResults(results, "xjoin");
  NamedList grouped = (NamedList)results.get("grouped");
  NamedList colours = (NamedList)grouped.get("colour");
  assertEquals(2, colours.get("matches"));
  DocList docs = (DocList)colours.get("doclist");
  assertEquals(docs.size(), 2);
  DocIterator it = docs.iterator();
  assertTrue(it.hasNext());
  assertEquals(1, it.nextDoc());
  assertTrue(it.hasNext());
  assertEquals(3, it.nextDoc());
  assertFalse(it.hasNext());
}
 
Example #7
Source File: TestXJoinSearchComponent.java    From BioSolr with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void testUngrouped() {
  ModifiableSolrParams params = new ModifiableSolrParams();
  NamedList results = test(params, "xjoin");
  testXJoinResults(results, "xjoin");
  ResultContext response = (ResultContext)results.get("response");
  DocList docs = response.getDocList();
  assertEquals(2, docs.size());
  DocIterator it = docs.iterator();
  assertTrue(it.hasNext());
  assertEquals(1, it.nextDoc());
  assertTrue(it.hasNext());
  assertEquals(3, it.nextDoc());
  assertFalse(it.hasNext());
}
 
Example #8
Source File: TestXJoinSearchComponent.java    From BioSolr with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void testUngrouped() {
  ModifiableSolrParams params = new ModifiableSolrParams();
  NamedList results = test(params, "xjoin");
  testXJoinResults(results, "xjoin");
  ResultContext response = (ResultContext)results.get("response");
  DocList docs = response.docs;
  assertEquals(2, docs.size());
  DocIterator it = docs.iterator();
  assertTrue(it.hasNext());
  assertEquals(1, it.nextDoc());
  assertTrue(it.hasNext());
  assertEquals(3, it.nextDoc());
  assertFalse(it.hasNext());
}
 
Example #9
Source File: LireRequestHandler.java    From liresolr with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns a random set of documents from the index. Mainly for testing purposes.
 *
 * @param req
 * @param rsp
 * @throws IOException
 */
private void handleRandomSearch(SolrQueryRequest req, SolrQueryResponse rsp) throws IOException {
    SolrIndexSearcher searcher = req.getSearcher();
    Query query = new MatchAllDocsQuery();
    DocList docList = searcher.getDocList(query, getFilterQueries(req), Sort.RELEVANCE, 0, numberOfCandidateResults, 0);
    int paramRows = Math.min(req.getParams().getInt("rows", defaultNumberOfResults), docList.size());
    if (docList.size() < 1) {
        rsp.add("Error", "No documents in index");
    } else {
        LinkedList list = new LinkedList();
        while (list.size() < paramRows) {
            DocList auxList = docList.subset((int) (Math.random() * docList.size()), 1);
            Document doc = null;
            for (DocIterator it = auxList.iterator(); it.hasNext(); ) {
                doc = searcher.doc(it.nextDoc());
            }
            if (!list.contains(doc)) {
                list.add(doc);
            }
        }
        rsp.addResponse(list);
    }
}
 
Example #10
Source File: SolrPluginUtils.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Generates an NamedList of Explanations for each item in a list of docs.
 *
 * @param query The Query you want explanations in the context of
 * @param docs The Documents you want explained relative that query
 */
public static NamedList<Explanation> getExplanations
  (Query query,
   DocList docs,
   SolrIndexSearcher searcher,
   IndexSchema schema) throws IOException {

  NamedList<Explanation> explainList = new SimpleOrderedMap<>();
  DocIterator iterator = docs.iterator();
  for (int i=0; i<docs.size(); i++) {
    int id = iterator.nextDoc();

    Document doc = searcher.doc(id);
    String strid = schema.printableUniqueKey(doc);

    explainList.add(strid, searcher.explain(query, id) );
  }
  return explainList;
}
 
Example #11
Source File: TestXJoinQParserPlugin.java    From BioSolr with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiValued() throws Exception {
  Query q = parse(COMPONENT_NAME_4);
  DocSet docs = searcher.getDocSet(q);

  assertEquals(4, docs.size());
  DocIterator it = docs.iterator();
  assertTrue(it.hasNext());
  assertEquals(0, it.nextDoc());
  assertTrue(it.hasNext());
  assertEquals(1, it.nextDoc());
  assertTrue(it.hasNext());
  assertEquals(2, it.nextDoc());
  assertTrue(it.hasNext());
  assertEquals(3, it.nextDoc());
  assertFalse(it.hasNext());    
}
 
Example #12
Source File: ParentNodeFacetTreeBuilder.java    From BioSolr with Apache License 2.0 6 votes vote down vote up
private Map<String, Set<String>> findParentIdsForNodes(SolrIndexSearcher searcher, Collection<String> nodeIds) throws IOException {
	Map<String, Set<String>> parentIds = new HashMap<>();
	
	LOGGER.debug("Looking up parents for {} nodes", nodeIds.size());
	Query filter = buildFilterQuery(getNodeField(), nodeIds);
	LOGGER.trace("Filter query: {}", filter);
	
	DocSet docs = searcher.getDocSet(filter);
	
	for (DocIterator it = docs.iterator(); it.hasNext(); ) {
		Document doc = searcher.doc(it.nextDoc(), docFields);
		String nodeId = doc.get(getNodeField());
		
		Set<String> parentIdValues = new HashSet<>(Arrays.asList(doc.getValues(parentField)));
		parentIds.put(nodeId, parentIdValues);
		
		// Record the label, if required
		if (isLabelRequired(nodeId)) {
			recordLabel(nodeId, doc.getValues(getLabelField()));
		}
	}
	
	return parentIds;
}
 
Example #13
Source File: BlockJoin.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** childInput may also contain parents (i.e. a parent or below will all roll up to that parent) */
public static DocSet toParents(DocSet childInput, BitDocSet parentList, QueryContext qcontext) throws IOException {
  FixedBitSet parentBits = parentList.getBits();
  DocSetCollector collector = new DocSetCollector(qcontext.searcher().maxDoc());
  DocIterator iter = childInput.iterator();
  int currentParent = -1;
  while (iter.hasNext()) {
    int childDoc = iter.nextDoc(); // TODO: skipping
    if (childDoc <= currentParent) { // use <= since we also allow parents in the input
      // we already visited this parent
      continue;
    }
    currentParent = parentBits.nextSetBit(childDoc);
    if (currentParent != DocIdSetIterator.NO_MORE_DOCS) {
      // only collect the parent the first time we skip to it
      collector.collect( currentParent );
    }
  }
  return collector.getDocSet();
}
 
Example #14
Source File: BlockJoin.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** acceptDocs will normally be used to avoid deleted documents from being generated as part of the answer DocSet (just use *:*)
 *  although it can be used to further constrain the generated documents.
 */
public static DocSet toChildren(DocSet parentInput, BitDocSet parentList, DocSet acceptDocs, QueryContext qcontext) throws IOException {
  FixedBitSet parentBits = parentList.getBits();
  DocSetCollector collector = new DocSetCollector(qcontext.searcher().maxDoc());
  DocIterator iter = parentInput.iterator();
  while (iter.hasNext()) {
    int parentDoc = iter.nextDoc();
    if (!parentList.exists(parentDoc) || parentDoc == 0) { // test for parentDoc==0 here to avoid passing -1 to prevSetBit later on
      // not a parent, or parent has no children
      continue;
    }
    int prevParent = parentBits.prevSetBit(parentDoc - 1);
    for (int childDoc = prevParent+1; childDoc<parentDoc; childDoc++) {
      if (acceptDocs != null && !acceptDocs.exists(childDoc)) continue;  // only select live docs
      collector.collect(childDoc);
    }
  }
  return collector.getDocSet();
}
 
Example #15
Source File: AbstractSolrCachingScorer.java    From SearchServices with GNU Lesser General Public License v3.0 6 votes vote down vote up
SolrCachingScorerDoIdSetIterator(DocSet in, LeafReaderContext context, SolrIndexSearcher searcher)
{
	  this.context = context;
      
      if (in instanceof BitDocSet)
      {
          matches = (BitDocSet) in;
      }
      else
      {
          this.matches = new BitDocSet(new FixedBitSet(searcher.maxDoc()));
          for (DocIterator it = in.iterator(); it.hasNext(); /* */)
          {
              matches.addUnique(it.nextDoc());
          }
      }
      bitSet = matches.getBits();
      
      doc = getBase() - 1;
}
 
Example #16
Source File: TestXJoinQParserPlugin.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
@Test
public void testBooleanCombination() throws Exception {
  Query q = parse(COMPONENT_NAME + " AND " + COMPONENT_NAME_2);
  DocSet docs = searcher.getDocSet(q);

  assertEquals(1, docs.size());
  DocIterator it = docs.iterator();
  assertTrue(it.hasNext());
  assertEquals(3, it.nextDoc());
  assertFalse(it.hasNext());    
}
 
Example #17
Source File: TestXJoinQParserPlugin.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleComponent() throws Exception {
  Query q = parse(COMPONENT_NAME);
  DocSet docs = searcher.getDocSet(q);

  assertEquals(2, docs.size());
  DocIterator it = docs.iterator();
  assertTrue(it.hasNext());
  assertEquals(1, it.nextDoc());
  assertTrue(it.hasNext());
  assertEquals(3, it.nextDoc());
  assertFalse(it.hasNext());
}
 
Example #18
Source File: ChildNodeFacetTreeBuilder.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
/**
 * Fetch facets for items containing a specific set of values.
 * @param searcher the searcher for the collection being used.
 * @param facetValues the incoming values to use as filters.
 * @param filterField the item field containing the child values, which will be used
 * to filter against.
 * @return a map of node value to child values for the items.
 * @throws IOException
 */
private Map<String, Set<String>> filterEntriesByField(SolrIndexSearcher searcher, Collection<String> facetValues,
		String filterField) throws IOException {
	Map<String, Set<String>> filteredEntries = new HashMap<>();

	LOGGER.debug("Looking up {} entries in field {}", facetValues.size(), filterField);
	Query filter = buildFilterQuery(filterField, facetValues);
	LOGGER.trace("Filter query: {}", filter);

	DocSet docs = searcher.getDocSet(filter);

	for (DocIterator it = docs.iterator(); it.hasNext(); ) {
		Document doc = searcher.doc(it.nextDoc(), docFields);
		String nodeId = doc.get(getNodeField());
		
		// Get the children for the node, if necessary
		Set<String> childIds;
		if (filterField.equals(getNodeField())) {
			// Filtering on the node field - child IDs are redundant
			childIds = Collections.emptySet();
		} else {
			childIds = new HashSet<>(Arrays.asList(doc.getValues(filterField)));
			LOGGER.trace("Got {} children for node {}", childIds.size(), nodeId);
		}
		filteredEntries.put(nodeId, childIds);
		
		// Record the label, if required
		if (isLabelRequired(nodeId)) {
			recordLabel(nodeId, doc.getValues(getLabelField()));
		}
	}

	return filteredEntries;
}
 
Example #19
Source File: XJoinSearchComponent.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
private Iterator<Integer> docIterator(ResponseBuilder rb) {
  if (rb.grouping()) {
    List<Integer> docList = new ArrayList<>();
    NamedList values = rb.rsp.getValues();
    NamedList grouped = (NamedList)values.get("grouped");
    for (String field : rb.getGroupingSpec().getFields()) {
      NamedList fieldResults = (NamedList)grouped.get(field);
      if (rb.getGroupingSpec().getResponseFormat() == Grouping.Format.grouped) {
        List<NamedList> groups = (List<NamedList>)fieldResults.get("groups");
        for (NamedList group : groups) {
          for (DocIterator it = ((DocList)group.get("doclist")).iterator(); it.hasNext(); ) {
            docList.add(it.nextDoc());
          }
        }
      } else {
        for (DocIterator it = ((DocList)fieldResults.get("doclist")).iterator(); it.hasNext(); ) {
          docList.add(it.nextDoc());
        }
      }
    }
    return docList.iterator();
  } else {
    return rb.getResults().docList.iterator();
  }
  
}
 
Example #20
Source File: TestSimple.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void testSimpleXJoinResultsFactory() {
  ModifiableSolrParams params = new ModifiableSolrParams();
  params.add("fl", "*, score");
  params.add("xjoin", "false");
  params.add("xjoin5", "true");
  params.add("xjoin5.fl", "*");
  
  // score boosts using value source parser
  params.add("defType", "edismax");
  params.add("bf", "xjoin5(value)");
  
  NamedList results = test(params, "xjoin5");
  ResultContext response = (ResultContext)results.get("response");
  DocList docs = response.docs;
  assertEquals(2, docs.size());
  DocIterator it = docs.iterator();
  assertTrue(it.hasNext());
  assertEquals(0, it.nextDoc());
  double score0 = it.score();
  assertTrue(it.hasNext());
  assertEquals(2, it.nextDoc());
  double score2 = it.score();
  assertFalse(it.hasNext());
 
  // bf score boost for testid=0 only
  assertTrue(score0 > score2);
  
  List externalList = (List)((NamedList)results.get("xjoin5")).get("external");
  NamedList hit0 = (NamedList)externalList.get(0);
  assertEquals("0", hit0.get("joinId"));
  NamedList doc0 = (NamedList)hit0.get("doc");
  assertEquals("red", doc0.get("colour"));
  assertEquals(10.5, doc0.get("value"));
}
 
Example #21
Source File: TestSimple.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void testSimpleXJoinResultsFactory() {
  ModifiableSolrParams params = new ModifiableSolrParams();
  params.add("fl", "*, score");
  params.add("xjoin", "false");
  params.add("xjoin5", "true");
  params.add("xjoin5.fl", "*");
  
  // score boosts using value source parser
  params.add("defType", "edismax");
  params.add("bf", "xjoin5(value)");
  
  NamedList results = test(params, "xjoin5");
  ResultContext response = (ResultContext)results.get("response");
  DocList docs = response.docs;
  assertEquals(2, docs.size());
  DocIterator it = docs.iterator();
  assertTrue(it.hasNext());
  assertEquals(0, it.nextDoc());
  double score0 = it.score();
  assertTrue(it.hasNext());
  assertEquals(2, it.nextDoc());
  double score2 = it.score();
  assertFalse(it.hasNext());
 
  // bf score boost for testid=0 only
  assertTrue(score0 > score2);
  
  List externalList = (List)((NamedList)results.get("xjoin5")).get("external");
  NamedList hit0 = (NamedList)externalList.get(0);
  assertEquals("0", hit0.get("joinId"));
  NamedList doc0 = (NamedList)hit0.get("doc");
  assertEquals("red", doc0.get("colour"));
  assertEquals(10.5, doc0.get("value"));
}
 
Example #22
Source File: TestXJoinQParserPlugin.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
@Test
public void testBooleanCombination() throws Exception {
  Query q = parse(COMPONENT_NAME + " AND " + COMPONENT_NAME_2);
  DocSet docs = searcher.getDocSet(q);

  assertEquals(1, docs.size());
  DocIterator it = docs.iterator();
  assertTrue(it.hasNext());
  assertEquals(3, it.nextDoc());
  assertFalse(it.hasNext());    
}
 
Example #23
Source File: TestXJoinQParserPlugin.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleComponent() throws Exception {
  Query q = parse(COMPONENT_NAME);
  DocSet docs = searcher.getDocSet(q);

  assertEquals(2, docs.size());
  DocIterator it = docs.iterator();
  assertTrue(it.hasNext());
  assertEquals(1, it.nextDoc());
  assertTrue(it.hasNext());
  assertEquals(3, it.nextDoc());
  assertFalse(it.hasNext());
}
 
Example #24
Source File: XJoinSearchComponent.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
private Iterator<Integer> docIterator(ResponseBuilder rb) {
  if (rb.grouping()) {
    List<Integer> docList = new ArrayList<>();
    NamedList values = rb.rsp.getValues();
    NamedList grouped = (NamedList)values.get("grouped");
    for (String field : rb.getGroupingSpec().getFields()) {
      NamedList fieldResults = (NamedList)grouped.get(field);
      if (rb.getGroupingSpec().getResponseFormat() == Grouping.Format.grouped) {
        List<NamedList> groups = (List<NamedList>)fieldResults.get("groups");
        for (NamedList group : groups) {
          for (DocIterator it = ((DocList)group.get("doclist")).iterator(); it.hasNext(); ) {
            docList.add(it.nextDoc());
          }
        }
      } else {
        for (DocIterator it = ((DocList)fieldResults.get("doclist")).iterator(); it.hasNext(); ) {
          docList.add(it.nextDoc());
        }
      }
    }
    return docList.iterator();
  } else {
    return rb.getResults().docList.iterator();
  }
  
}
 
Example #25
Source File: TestSimple.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void testSimpleXJoinResultsFactory() {
  ModifiableSolrParams params = new ModifiableSolrParams();
  params.add("fl", "*, score");
  params.add("xjoin", "false");
  params.add("xjoin5", "true");
  params.add("xjoin5.fl", "*");
  
  // score boosts using value source parser
  params.add("defType", "edismax");
  params.add("bf", "xjoin5(value)");
  
  NamedList results = test(params, "xjoin5");
  ResultContext response = (ResultContext)results.get("response");
  DocList docs = response.getDocList();
  assertEquals(2, docs.size());
  DocIterator it = docs.iterator();
  assertTrue(it.hasNext());
  assertEquals(0, it.nextDoc());
  double score0 = it.score();
  assertTrue(it.hasNext());
  assertEquals(2, it.nextDoc());
  double score2 = it.score();
  assertFalse(it.hasNext());
 
  // bf score boost for testid=0 only
  assertTrue(score0 > score2);
  
  List externalList = (List)((NamedList)results.get("xjoin5")).get("external");
  NamedList hit0 = (NamedList)externalList.get(0);
  assertEquals("0", hit0.get("joinId"));
  NamedList doc0 = (NamedList)hit0.get("doc");
  assertEquals("red", doc0.get("colour"));
  assertEquals(10.5, doc0.get("value"));
}
 
Example #26
Source File: TestXJoinQParserPlugin.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
@Test
public void testBooleanCombination() throws Exception {
  Query q = parse(COMPONENT_NAME + " AND " + COMPONENT_NAME_2);
  DocSet docs = searcher.getDocSet(q);

  assertEquals(1, docs.size());
  DocIterator it = docs.iterator();
  assertTrue(it.hasNext());
  assertEquals(3, it.nextDoc());
  assertFalse(it.hasNext());    
}
 
Example #27
Source File: TestXJoinQParserPlugin.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleComponent() throws Exception {
  Query q = parse(COMPONENT_NAME);
  DocSet docs = searcher.getDocSet(q);

  assertEquals(2, docs.size());
  DocIterator it = docs.iterator();
  assertTrue(it.hasNext());
  assertEquals(1, it.nextDoc());
  assertTrue(it.hasNext());
  assertEquals(3, it.nextDoc());
  assertFalse(it.hasNext());
}
 
Example #28
Source File: XJoinSearchComponent.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
private Iterator<Integer> docIterator(ResponseBuilder rb) {
  if (rb.grouping()) {
    List<Integer> docList = new ArrayList<>();
    NamedList values = rb.rsp.getValues();
    NamedList grouped = (NamedList)values.get("grouped");
    for (String field : rb.getGroupingSpec().getFields()) {
      NamedList fieldResults = (NamedList)grouped.get(field);
      if (rb.getGroupingSpec().getResponseFormat() == Grouping.Format.grouped) {
        List<NamedList> groups = (List<NamedList>)fieldResults.get("groups");
        for (NamedList group : groups) {
          for (DocIterator it = ((DocList)group.get("doclist")).iterator(); it.hasNext(); ) {
            docList.add(it.nextDoc());
          }
        }
      } else {
        for (DocIterator it = ((DocList)fieldResults.get("doclist")).iterator(); it.hasNext(); ) {
          docList.add(it.nextDoc());
        }
      }
    }
    return docList.iterator();
  } else {
    return rb.getResults().docList.iterator();
  }
  
}
 
Example #29
Source File: RealTimePriceComponent.java    From apache-solr-essentials with Apache License 2.0 5 votes vote down vote up
/**
 * Here we define the component core logic.
 * For each document belonging to search results, we call an external service
 * for gathering a corresponding up-to-date price.
 * 
 * @param rb The {@link org.apache.solr.handler.component.ResponseBuilder}
 * @throws IOException If there is a low-level I/O error.
 */
@Override
public void process(final ResponseBuilder builder) throws IOException {
	// Sanity check: if the component hasn't been properly initialised 
	// then it must immediately return.
	// A more ideal approach could retry the initialisation (in the prepare method).
	if (!hasBeenCorrectlyInitialised) {
		return;
	}
	
	// Get a SolrIndexSearcher reference 
	final SolrIndexSearcher searcher = builder.req.getSearcher();

	// This NamediLis will hold the component contribution (i.e. the component result).
	final NamedList<Double> contribution = new SimpleOrderedMap<Double>();
	for (final DocIterator it = builder.getResults().docList.iterator(); it.hasNext();) {

		// This is NOT the Solr ID of our records, but instead the Lucene internal document id
		// which is different
		int docId = it.nextDoc();
		final Document luceneDocument = searcher.doc(docId);
		
		// This is the Solr document Id 
		String id = luceneDocument.get("id");

		// Get the price of the item
		final Double itemPrice = getPrice(id);

		// Add the price of the item to the component contribution
		contribution.add(id, itemPrice);
	}

	// Add the component contribution to the response builder
	builder.rsp.add("prices", contribution);			
}
 
Example #30
Source File: UnifiedSolrHighlighter.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Converts solr's DocList to the int[] docIDs
 */
protected int[] toDocIDs(DocList docs) {
  int[] docIDs = new int[docs.size()];
  DocIterator iterator = docs.iterator();
  for (int i = 0; i < docIDs.length; i++) {
    if (!iterator.hasNext()) {
      throw new AssertionError();
    }
    docIDs[i] = iterator.nextDoc();
  }
  if (iterator.hasNext()) {
    throw new AssertionError();
  }
  return docIDs;
}