org.apache.solr.response.ResultContext Java Examples

The following examples show how to use org.apache.solr.response.ResultContext. 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: Cloud.java    From SearchServices with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
     * Returns whether or not a doc exists that satisfies the specified query
     * @param requestHandler the handler that handles the request
     * @param request the request object to put the query on
     * @param query the string that specifies the doc
     * @return <code>true</code> if the specified query returns a doc
     */
    boolean exists(SolrRequestHandler requestHandler, SolrQueryRequest request, String query)
    {
        ModifiableSolrParams params = new ModifiableSolrParams(request.getParams());
        // Sets 1 because this is effectively a boolean query to see if there exists a single match
        params.set("q", query).set("fl", "id").set("rows", "1");
        ResultContext rc = this.getResultContext(requestHandler, request, params);

        if (rc != null)
        {
// TODO Should we use rc.docs.matches() instead?
            if (rc.getDocList() != null) { return rc.getDocList().iterator().hasNext(); }
        }

        return false;
    }
 
Example #2
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 #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: 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 #5
Source File: BasicFunctionalityTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testNotLazyField() throws IOException {

  assertU(adoc("id", "7777",
               "title", "keyword",
               "test_hlt", mkstr(20000)));

  assertU(commit());
  SolrCore core = h.getCore();
 
  SolrQueryRequest req = req("q", "id:7777", "fl", "id,title,test_hlt");
  SolrQueryResponse rsp = new SolrQueryResponse();
  core.execute(core.getRequestHandler(req.getParams().get(CommonParams.QT)), req, rsp);

  DocList dl = ((ResultContext) rsp.getResponse()).getDocList();
  Document d = req.getSearcher().doc(dl.iterator().nextDoc());
  // ensure field in fl is not lazy
  assertFalse( ((Field) d.getField("test_hlt")).getClass().getSimpleName().equals("LazyField"));
  assertFalse( ((Field) d.getField("title")).getClass().getSimpleName().equals("LazyField"));
  req.close();
}
 
Example #6
Source File: ResponseLogComponent.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public void process(ResponseBuilder rb) throws IOException {
  SolrParams params = rb.req.getParams();
  if (!params.getBool(COMPONENT_NAME, false)) return;
  
  SolrIndexSearcher searcher = rb.req.getSearcher();
  IndexSchema schema = searcher.getSchema();
  if (schema.getUniqueKeyField() == null) return;

  ResultContext rc = (ResultContext) rb.rsp.getResponse();

  DocList docs = rc.getDocList();
  if (docs.hasScores()) {
    processScores(rb, docs, schema, searcher);
  } else {
    processIds(rb, docs, schema, searcher);
  }
}
 
Example #7
Source File: ValueSourceAugmenter.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings({"unchecked"})
public void setContext( ResultContext context ) {
  super.setContext(context);
  try {
    searcher = context.getSearcher();
    readerContexts = searcher.getIndexReader().leaves();
    fcontext = ValueSource.newContext(searcher);
    this.valueSource.createWeight(fcontext, searcher);
  } catch (IOException e) {
    throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e);
  }
}
 
Example #8
Source File: SolrDocumentFetcher.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void stringField(FieldInfo fieldInfo, String value) throws IOException {
  Predicate<String> readAsBytes = ResultContext.READASBYTES.get();
  if (readAsBytes != null && readAsBytes.test(fieldInfo.name)) {
    final FieldType ft = new FieldType(TextField.TYPE_STORED);
    ft.setStoreTermVectors(fieldInfo.hasVectors());
    ft.setOmitNorms(fieldInfo.omitsNorms());
    ft.setIndexOptions(fieldInfo.getIndexOptions());
    Objects.requireNonNull(value, "String value should not be null");
    doc.add(new StoredField(fieldInfo.name, value, ft));
  } else {
    super.stringField(fieldInfo, value);
  }

}
 
Example #9
Source File: QueryComponent.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void doProcessUngroupedSearch(ResponseBuilder rb, QueryCommand cmd, QueryResult result) throws IOException {

    SolrQueryRequest req = rb.req;
    SolrQueryResponse rsp = rb.rsp;

    SolrIndexSearcher searcher = req.getSearcher();

    try {
      searcher.search(result, cmd);
    } catch (FuzzyTermsEnum.FuzzyTermsException e) {
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
    }
    rb.setResult(result);

    ResultContext ctx = new BasicResultContext(rb);
    rsp.addResponse(ctx);
    rsp.getToLog().add("hits", rb.getResults()==null || rb.getResults().docList==null ? 0 : rb.getResults().docList.matches());

    if ( ! rb.req.getParams().getBool(ShardParams.IS_SHARD,false) ) {
      if (null != rb.getNextCursorMark()) {
        rb.rsp.add(CursorMarkParams.CURSOR_MARK_NEXT,
                   rb.getNextCursorMark().getSerializedTotem());
      }
    }

    if(rb.mergeFieldHandler != null) {
      rb.mergeFieldHandler.handleMergeFields(rb, searcher);
    } else {
      doFieldSortValues(rb, searcher);
    }

    doPrefetch(rb);
  }
 
Example #10
Source File: TestGroupingSearch.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testGroupingSimpleFormatArrayIndexOutOfBoundsExceptionWithJavaBin() throws Exception {
  assertU(add(doc("id", "1", "nullfirst", "1")));
  assertU(add(doc("id", "2", "nullfirst", "1")));
  assertU(add(doc("id", "3", "nullfirst", "2")));
  assertU(add(doc("id", "4", "nullfirst", "2")));
  assertU(add(doc("id", "5", "nullfirst", "2")));
  assertU(add(doc("id", "6", "nullfirst", "3")));
  assertU(commit());

  SolrQueryRequest request =
      req("q", "*:*","group", "true", "group.field", "nullfirst", "group.main", "true", "wt", "javabin", "start", "4", "rows", "10");

  SolrQueryResponse response = new SolrQueryResponse();
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  try {
    SolrRequestInfo.setRequestInfo(new SolrRequestInfo(request, response));
    String handlerName = request.getParams().get(CommonParams.QT);
    h.getCore().execute(h.getCore().getRequestHandler(handlerName), request, response);
    BinaryResponseWriter responseWriter = new BinaryResponseWriter();
    responseWriter.write(out, request, response);
  } finally {
    request.close();
    SolrRequestInfo.clearRequestInfo();
  }

  assertEquals(6, ((ResultContext) response.getResponse()).getDocList().matches());
  new BinaryResponseParser().processResponse(new ByteArrayInputStream(out.toByteArray()), "");
  out.close();
}
 
Example #11
Source File: Cloud.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @param requestHandler the handler that handles the request
 * @param request the request object to put the params on
 * @param params Solr parameters
 * @return the result context from the handled request
 */
ResultContext getResultContext(SolrRequestHandler requestHandler, SolrQueryRequest request, SolrParams params)
{
    SolrQueryResponse solrRsp = getResponse(requestHandler, request, params);
    ResultContext rc = (ResultContext) solrRsp.getValues().get("response");
    return rc;
}
 
Example #12
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 #13
Source File: Cloud.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Returns the doc list resulting from running the query
 * @param requestHandler the handler that handles the request
 * @param request the request object to put the query on
 * @param query the string that specifies the docs
 * @return the docs that come back from the query
 */
DocList getDocList(SolrRequestHandler requestHandler, SolrQueryRequest request, String query)
{
    // Getting the doc list is shard-specific, and not really cloud-friendly
    
    ModifiableSolrParams params = new ModifiableSolrParams(request.getParams());
    // Sets MAX_VALUE to get all the rows
    params.set("q", query).set("fl", QueryConstants.FIELD_SOLR4_ID).set("rows", Integer.MAX_VALUE);
    ResultContext rc = this.getResultContext(requestHandler, request, params);
    return rc != null ? rc.getDocList() : null;
}
 
Example #14
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 #15
Source File: SolrInformationServer.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
public int getDocListSize(String query)
{
    try (SolrQueryRequest request = this.newSolrQueryRequest())
    {
        ModifiableSolrParams params =
                new ModifiableSolrParams(request.getParams())
                        .set(CommonParams.Q, query)
                        .set(CommonParams.ROWS, 0);
        ResultContext resultContext = cloud.getResultContext(nativeRequestHandler, request, params);
        return resultContext.getDocList().matches();
    }
}
 
Example #16
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 #17
Source File: DocTransformers.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public void setContext( ResultContext context ) {
  for( DocTransformer a : children ) {
    a.setContext( context );
  }
}
 
Example #18
Source File: DocValueDocTransformer.java    From SearchServices with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void setContext( ResultContext context ) 
{
    this.context = context;
}
 
Example #19
Source File: AlfrescoFieldMapperTransformer.java    From SearchServices with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void setContext( ResultContext context )
{
    this.context = context;
}
 
Example #20
Source File: AutocompleteResponseWriter.java    From apache-solr-essentials with Apache License 2.0 4 votes vote down vote up
/**
 * Here the writer creates its output.
 * 
 * @param writer the character stream writer.
 * @param request the current {@link SolrQueryRequest}
 * @param response the output response.
 * @throws IOException in case of I/O failure.
 */
@SuppressWarnings("rawtypes")
@Override
public void write(
		final Writer writer, 
		final SolrQueryRequest request, 
		final SolrQueryResponse response) throws IOException {
	
	// 1. Get a reference to values that compound the current response
	final NamedList elements = response.getValues();
	
	// 2. Use a StringBuilder to build the output 
	final StringBuilder builder = new StringBuilder("{")
		.append("query:'")
		.append(request.getParams().get(CommonParams.Q))
		.append("',");
	
	// 3. Get a reference to the object which hold the query result
	final Object value = elements.getVal(1);		
	if (value instanceof ResultContext)
	{
		final ResultContext context = (ResultContext) value;
	
		// The ordered list (actually the page subset) of matched documents
		final DocList ids = context.docs;
		if (ids != null)
		{
			final SolrIndexSearcher searcher = request.getSearcher();
			final DocIterator iterator = ids.iterator();
			builder.append("suggestions:[");
			
			// 4. Iterate over documents
			for (int i = 0; i < ids.size(); i++)
			{
				// 5. For each document we need to get the corresponding "label" attribute
				final Document document = searcher.doc(iterator.nextDoc(), FIELDS);
				if (i > 0)  { builder.append(","); }
				
				// 6. Append the label value to writer output
				builder
					.append("'")
					.append(((String) document.get("label")).replaceAll("'", "\\\\'").replaceAll("\"", "\\\\\""))
					.append("'");
			}
			builder.append("]").append("}");
		}
	}
	
	// 7. and finally write out the built character stream by means of output writer.
	writer.write(builder.toString());
}
 
Example #21
Source File: DocTransformer.java    From lucene-solr with Apache License 2.0 2 votes vote down vote up
/**
 * This is called before {@link #transform} to provide context for any subsequent transformations.
 *
 * @param context The {@link ResultContext} stores information about how the documents were produced.
 * @see #needsSolrIndexSearcher
 */
public void setContext( ResultContext context ) {
  this.context = context;

}