Java Code Examples for org.apache.solr.response.SolrQueryResponse#getValues()

The following examples show how to use org.apache.solr.response.SolrQueryResponse#getValues() . 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: AbstractXJoinTestCase.java    From BioSolr with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("rawtypes")
protected NamedList test(ModifiableSolrParams params, String componentName) {
  SolrCore core = h.getCore();

  SearchComponent sc = core.getSearchComponent(componentName);
  assertTrue("XJoinSearchComponent not found in solrconfig", sc != null);
    
  QParserPlugin qp = core.getQueryPlugin("xjoin");
  assertTrue("XJoinQParserPlugin not found in solrconfig", qp != null);
  
  params.add("q", "*:*");
  params.add("fq", "{!xjoin}" + componentName);

  SolrQueryResponse rsp = new SolrQueryResponse();
  rsp.add("responseHeader", new SimpleOrderedMap<>());
  SolrQueryRequest req = new LocalSolrQueryRequest(core, params);

  SolrRequestHandler handler = core.getRequestHandler("standard");
  handler.handleRequest(req, rsp);
  req.close();
  assertNull(rsp.getException());
    
  return rsp.getValues();
}
 
Example 2
Source File: AbstractXJoinTestCase.java    From BioSolr with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("rawtypes")
protected NamedList test(ModifiableSolrParams params, String componentName) {
  SolrCore core = h.getCore();

  SearchComponent sc = core.getSearchComponent(componentName);
  assertTrue("XJoinSearchComponent not found in solrconfig", sc != null);
    
  QParserPlugin qp = core.getQueryPlugin("xjoin");
  assertTrue("XJoinQParserPlugin not found in solrconfig", qp != null);
  
  params.add("q", "*:*");
  params.add("fq", "{!xjoin}" + componentName);

  SolrQueryResponse rsp = new SolrQueryResponse();
  rsp.add("responseHeader", new SimpleOrderedMap<>());
  SolrQueryRequest req = new LocalSolrQueryRequest(core, params);

  SolrRequestHandler handler = core.getRequestHandler("standard");
  handler.handleRequest(req, rsp);
  req.close();
  assertNull(rsp.getException());
    
  return rsp.getValues();
}
 
Example 3
Source File: ExtractingRequestHandlerTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testXPath() throws Exception {
  ExtractingRequestHandler handler = (ExtractingRequestHandler) h.getCore().getRequestHandler("/update/extract");
  assertTrue("handler is null and it shouldn't be", handler != null);
  SolrQueryResponse rsp = loadLocal("extraction/example.html",
          ExtractingParams.XPATH_EXPRESSION, "/xhtml:html/xhtml:body/xhtml:a/descendant::node()",
          ExtractingParams.EXTRACT_ONLY, "true"
  );
  assertTrue("rsp is null and it shouldn't be", rsp != null);
  @SuppressWarnings({"rawtypes"})
  NamedList list = rsp.getValues();
  String val = (String) list.get("example.html");
  assertEquals("News", val.trim()); //there is only one matching <a> tag

  loadLocal("extraction/example.html",
      "literal.id", "example1",
      "captureAttr", "true",
      "defaultField", "text",
      "capture", "div",
      "fmap.div", "foo_t",
      "boost.foo_t", "3",
      "xpath", "/xhtml:html/xhtml:body/xhtml:div//node()",
      "commit", "true"
  );
  assertQ(req("+id:example1 +foo_t:\"here is some text in a div\""), "//*[@numFound='1']");
}
 
Example 4
Source File: AbstractXJoinTestCase.java    From BioSolr with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("rawtypes")
protected NamedList test(ModifiableSolrParams params, String componentName) {
  SolrCore core = h.getCore();

  SearchComponent sc = core.getSearchComponent(componentName);
  assertTrue("XJoinSearchComponent not found in solrconfig", sc != null);
    
  QParserPlugin qp = core.getQueryPlugin("xjoin");
  assertTrue("XJoinQParserPlugin not found in solrconfig", qp != null);
  
  params.add("q", "*:*");
  params.add("fq", "{!xjoin}" + componentName);

  SolrQueryResponse rsp = new SolrQueryResponse();
  rsp.add("responseHeader", new SimpleOrderedMap<>());
  SolrQueryRequest req = new LocalSolrQueryRequest(core, params);

  SolrRequestHandler handler = core.getRequestHandler("standard");
  handler.handleRequest(req, rsp);
  req.close();
  assertNull(rsp.getException());
    
  return rsp.getValues();
}
 
Example 5
Source File: TestManagedSchema.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void assertSchemaResource(String collection, String expectedSchemaResource) throws Exception {
  final CoreContainer cores = h.getCoreContainer();
  final CoreAdminHandler admin = new CoreAdminHandler(cores);
  SolrQueryRequest request = req(CoreAdminParams.ACTION, CoreAdminParams.CoreAdminAction.STATUS.toString());
  SolrQueryResponse response = new SolrQueryResponse();
  admin.handleRequestBody(request, response);
  assertNull("Exception on create", response.getException());
  @SuppressWarnings({"rawtypes"})
  NamedList responseValues = response.getValues();
  @SuppressWarnings({"rawtypes"})
  NamedList status = (NamedList)responseValues.get("status");
  @SuppressWarnings({"rawtypes"})
  NamedList collectionStatus = (NamedList)status.get(collection);
  String collectionSchema = (String)collectionStatus.get(CoreAdminParams.SCHEMA);
  assertEquals("Schema resource name differs from expected name", expectedSchemaResource, collectionSchema);
}
 
Example 6
Source File: SolrInformationServer.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
SolrDocument getState(SolrCore core, SolrQueryRequest request, String id)
{
    ModifiableSolrParams newParams =
            new ModifiableSolrParams(request.getParams())
                    .set(CommonParams.ID, id);
    request.setParams(newParams);

    SolrQueryResponse response = newSolrQueryResponse();
    core.getRequestHandler(REQUEST_HANDLER_GET).handleRequest(request, response);

    NamedList<?> values = response.getValues();
    return (SolrDocument)values.get(RESPONSE_DEFAULT_ID);
}
 
Example 7
Source File: HybridResponseWriter.java    From SolRDF with Apache License 2.0 5 votes vote down vote up
@Override
public void write(
		final Writer writer, 
		final SolrQueryRequest request, 
		final SolrQueryResponse response) throws IOException {		
	final NamedList<?> values = response.getValues();
	final Query query = (Query)request.getContext().get(Names.QUERY);
	final QueryExecution execution = (QueryExecution)response.getValues().get(Names.QUERY_EXECUTION);
	try {
		final boolean isHybridMode = Boolean.TRUE.equals(request.getContext().get(Names.HYBRID_MODE));
		if (isHybridMode) {
			response.add(Names.SOLR_REQUEST, request);
			response.add(Names.SOLR_RESPONSE, response);
			
			final String contentType = contentTypeRewrites.get(getContentType(request, false));
			WriterStrategy strategy = compositeWriters.get(contentType);
			strategy = strategy != null ? strategy : compositeWriters.get("text/xml");
			strategy.doWrite(values, writer, contentType);
		} else {
			if (query == null || execution == null) {
				LOGGER.error(MessageCatalog._00091_NULL_QUERY_OR_EXECUTION);
				return;
			}
			writers.get(query.getQueryType()).doWrite(values, writer, getContentType(request, false));
		}
	} finally {
		if (execution != null) {
			// CHECKSTYLE:OFF
			try { execution.close();} catch (final Exception ignore) {}
			// CHECKSTYLE:ON
		}			
	}			
}
 
Example 8
Source File: SpellCheckCollatorTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings({"unchecked", "rawtypes"})
public void testWithCursorMark() throws Exception
{
  SolrCore core = h.getCore();
  SearchComponent speller = core.getSearchComponent("spellcheck");
  assertTrue("speller is null and it shouldn't be", speller != null);
  
  ModifiableSolrParams params = new ModifiableSolrParams();   
  params.add(SpellCheckComponent.COMPONENT_NAME, "true");
  params.add(SpellCheckComponent.SPELLCHECK_BUILD, "true");
  params.add(SpellCheckComponent.SPELLCHECK_COUNT, "10");   
  params.add(SpellCheckComponent.SPELLCHECK_COLLATE, "true");
  params.add(SpellCheckComponent.SPELLCHECK_MAX_COLLATION_TRIES, "2");
  params.add(SpellCheckComponent.SPELLCHECK_MAX_COLLATIONS, "1");
  params.add(CommonParams.Q, "lowerfilt:(+fauth)");
  params.add(CommonParams.SORT, "id asc");
  params.add(CursorMarkParams.CURSOR_MARK_PARAM, CursorMarkParams.CURSOR_MARK_START);
  SolrRequestHandler handler = core.getRequestHandler("/spellCheckCompRH");
  SolrQueryResponse rsp = new SolrQueryResponse();
  rsp.addResponseHeader(new SimpleOrderedMap());
  SolrQueryRequest req = new LocalSolrQueryRequest(core, params);
  handler.handleRequest(req, rsp);
  req.close();
  NamedList values = rsp.getValues();
  NamedList spellCheck = (NamedList) values.get("spellcheck");
  NamedList collationList = (NamedList) spellCheck.get("collations");
  List<?> collations = (List<?>) collationList.getAll("collation");
  assertTrue(collations.size() == 1);
}
 
Example 9
Source File: SpellCheckCollatorTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings({"unchecked", "rawtypes"})
public void testZeroTries() throws Exception
{
  SolrCore core = h.getCore();
  SearchComponent speller = core.getSearchComponent("spellcheck");
  assertTrue("speller is null and it shouldn't be", speller != null);
  
  ModifiableSolrParams params = new ModifiableSolrParams();   
  params.add(SpellCheckComponent.COMPONENT_NAME, "true");
  params.add(SpellCheckComponent.SPELLCHECK_BUILD, "true");
  params.add(SpellCheckComponent.SPELLCHECK_COUNT, "10");   
  params.add(SpellCheckComponent.SPELLCHECK_COLLATE, "true");
  params.add(SpellCheckComponent.SPELLCHECK_MAX_COLLATION_TRIES, "0");
  params.add(SpellCheckComponent.SPELLCHECK_MAX_COLLATIONS, "2");
  params.add(CommonParams.Q, "lowerfilt:(+fauth)");
  SolrRequestHandler handler = core.getRequestHandler("/spellCheckCompRH");
  SolrQueryResponse rsp = new SolrQueryResponse();
  rsp.addResponseHeader(new SimpleOrderedMap());
  SolrQueryRequest req = new LocalSolrQueryRequest(core, params);
  handler.handleRequest(req, rsp);
  req.close();
  NamedList values = rsp.getValues();
  NamedList spellCheck = (NamedList) values.get("spellcheck");
  NamedList collationList = (NamedList) spellCheck.get("collations");
  List<?> collations = (List<?>) collationList.getAll("collation");
  assertTrue(collations.size() == 2);
}
 
Example 10
Source File: SpellCheckCollatorTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings({"unchecked", "rawtypes"})
public void testCollateWithGrouping() throws Exception
{
  SolrCore core = h.getCore();
  SearchComponent speller = core.getSearchComponent("spellcheck");
  assertTrue("speller is null and it shouldn't be", speller != null);

  ModifiableSolrParams params = new ModifiableSolrParams();
  params.add(SpellCheckComponent.COMPONENT_NAME, "true");
  params.add(SpellingParams.SPELLCHECK_BUILD, "true");
  params.add(SpellingParams.SPELLCHECK_COUNT, "10");
  params.add(SpellingParams.SPELLCHECK_COLLATE, "true");
  params.add(SpellingParams.SPELLCHECK_MAX_COLLATION_TRIES, "5");
  params.add(SpellingParams.SPELLCHECK_MAX_COLLATIONS, "1");
  params.add(CommonParams.Q, "lowerfilt:(+fauth)");
  params.add(GroupParams.GROUP, "true");
  params.add(GroupParams.GROUP_FIELD, "id");

  //Because a FilterQuery is applied which removes doc id#1 from possible hits, we would
  //not want the collations to return us "lowerfilt:(+faith +hope +loaves)" as this only matches doc id#1.
  SolrRequestHandler handler = core.getRequestHandler("/spellCheckCompRH");
  SolrQueryResponse rsp = new SolrQueryResponse();
  rsp.addResponseHeader(new SimpleOrderedMap());
  SolrQueryRequest req = new LocalSolrQueryRequest(core, params);
  handler.handleRequest(req, rsp);
  req.close();
  NamedList values = rsp.getValues();
  NamedList spellCheck = (NamedList) values.get("spellcheck");
  NamedList collationHolder = (NamedList) spellCheck.get("collations");
  List<String> collations = collationHolder.getAll("collation");
  assertTrue(collations.size() == 1);
}
 
Example 11
Source File: SpellCheckCollatorTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings({"unchecked", "rawtypes"})
public void testCollateWithFilter() throws Exception
{
  SolrCore core = h.getCore();
  SearchComponent speller = core.getSearchComponent("spellcheck");
  assertTrue("speller is null and it shouldn't be", speller != null);

  ModifiableSolrParams params = new ModifiableSolrParams();
  params.add(SpellCheckComponent.COMPONENT_NAME, "true");
  params.add(SpellingParams.SPELLCHECK_BUILD, "true");
  params.add(SpellingParams.SPELLCHECK_COUNT, "10");
  params.add(SpellingParams.SPELLCHECK_COLLATE, "true");
  params.add(SpellingParams.SPELLCHECK_MAX_COLLATION_TRIES, "10");
  params.add(SpellingParams.SPELLCHECK_MAX_COLLATIONS, "10");
  params.add(CommonParams.Q, "lowerfilt:(+fauth +home +loane)");
  params.add(CommonParams.FQ, "NOT(id:1)");

  //Because a FilterQuery is applied which removes doc id#1 from possible hits, we would
  //not want the collations to return us "lowerfilt:(+faith +hope +loaves)" as this only matches doc id#1.
  SolrRequestHandler handler = core.getRequestHandler("/spellCheckCompRH");
  SolrQueryResponse rsp = new SolrQueryResponse();
  rsp.addResponseHeader(new SimpleOrderedMap());
  SolrQueryRequest req = new LocalSolrQueryRequest(core, params);
  handler.handleRequest(req, rsp);
  req.close();
  NamedList values = rsp.getValues();
  NamedList spellCheck = (NamedList) values.get("spellcheck");
  NamedList collationHolder = (NamedList) spellCheck.get("collations");
  List<String> collations = collationHolder.getAll("collation");
  assertTrue(collations.size() > 0);
  for(String collation : collations) {
    assertTrue(!collation.equals("lowerfilt:(+faith +hope +loaves)"));
  }
}
 
Example 12
Source File: SpellCheckCollatorTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings({"unchecked", "rawtypes"})
public void testCollationWithRangeQuery() throws Exception
{
  SolrCore core = h.getCore();
  SearchComponent speller = core.getSearchComponent("spellcheck");
  assertTrue("speller is null and it shouldn't be", speller != null);
  
  ModifiableSolrParams params = new ModifiableSolrParams();   
  params.add(SpellCheckComponent.COMPONENT_NAME, "true");
  params.add(SpellingParams.SPELLCHECK_BUILD, "true");
  params.add(SpellingParams.SPELLCHECK_COUNT, "10");   
  params.add(SpellingParams.SPELLCHECK_COLLATE, "true"); 
  params.add(SpellingParams.SPELLCHECK_ALTERNATIVE_TERM_COUNT, "10"); 
  params.add(CommonParams.Q, "id:[1 TO 10] AND lowerfilt:lovw");
  {
    SolrRequestHandler handler = core.getRequestHandler("/spellCheckCompRH");
    SolrQueryResponse rsp = new SolrQueryResponse();
    rsp.addResponseHeader(new SimpleOrderedMap());
    SolrQueryRequest req = new LocalSolrQueryRequest(core, params);
    handler.handleRequest(req, rsp);
    req.close();
    NamedList values = rsp.getValues();
    NamedList spellCheck = (NamedList) values.get("spellcheck");
    NamedList collationHolder = (NamedList) spellCheck.get("collations");
    List<String> collations = collationHolder.getAll("collation");
    assertTrue(collations.size()==1); 
    String collation = collations.iterator().next();    
    System.out.println(collation);
    assertTrue("Incorrect collation: " + collation,"id:[1 TO 10] AND lowerfilt:love".equals(collation));
  }
}
 
Example 13
Source File: ZookeeperStatusHandler.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
  NamedList values = rsp.getValues();
  if (cores.isZooKeeperAware()) {
    String zkHost = cores.getZkController().getZkServerAddress();
    SolrZkClient zkClient = cores.getZkController().getZkClient();
    final ZkDynamicConfig dynConfig = ZkDynamicConfig.parseLines(zkClient.getConfig());
    values.add("zkStatus", getZkStatus(zkHost, dynConfig));
  } else {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "The Zookeeper status API is only available in Cloud mode");
  }
}
 
Example 14
Source File: CloudMLTQParser.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private SolrDocument getDocument(String id) {
  SolrCore core = req.getCore();
  SolrQueryResponse rsp = new SolrQueryResponse();
  ModifiableSolrParams params = new ModifiableSolrParams();
  params.add(ID, id);

  SolrQueryRequestBase request = new SolrQueryRequestBase(core, params) {
  };

  core.getRequestHandler("/get").handleRequest(request, rsp);
  @SuppressWarnings({"rawtypes"})
  NamedList response = rsp.getValues();

  return (SolrDocument) response.get("doc");
}
 
Example 15
Source File: AbstractAlfrescoDistributedIT.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static SolrQueryResponse rangeCheck(int shard) throws Exception
{
    int maxAttemps = 10;
    for (int attemp=0; attemp < maxAttemps; ++attemp)
    {
        Collection<SolrCore> cores = getCores(solrShards);
        List<AlfrescoCoreAdminHandler> alfrescoCoreAdminHandlers = getAdminHandlers(solrShards);
        SolrCore core = cores.stream()
                .filter(solrcore -> solrcore.getName().equals("shard" + shard)).findAny().orElseThrow(RuntimeException::new);
        AlfrescoCoreAdminHandler alfrescoCoreAdminHandler = alfrescoCoreAdminHandlers.get(shard);
        SolrQueryResponse response = callHandler(alfrescoCoreAdminHandler, core, "RANGECHECK");
        NamedList<?> values = response.getValues();

        boolean isReady = !Optional.ofNullable(values.get("report"))
                    .map(Object::toString)
                    .filter(r -> r.contains("WARNING=The requested endpoint is not available on the slave"))
                    .isPresent() &&
                !Optional.ofNullable(values.get("exception"))
                    .map(Object::toString)
                    .filter(ex -> ex.contains("not initialized"))
                    .isPresent();

        if (!isReady)
        {
            Thread.sleep(1000);
        }
        else
        {
            return response;
        }
    }

    throw new Exception("impossible to perform rangeChack");
}
 
Example 16
Source File: SolrInformationServer.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
private SolrDocumentList executeQueryRequest(SolrQueryRequest request, SolrQueryResponse response, SolrRequestHandler handler)
{
    handler.handleRequest(request, response);

    NamedList<?> values = response.getValues();
    return (SolrDocumentList) values.get(RESPONSE_DEFAULT_IDS);
}
 
Example 17
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 18
Source File: SimpleTreeFacetComponentTest.java    From BioSolr with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testSingleResult() {
	SolrCore core = h.getCore();
	
	ModifiableSolrParams params = new ModifiableSolrParams();
	params.add("q", "name:nodeAAA");
	params.add("facet", "true");
	params.add("facet.tree", "true");
	params.add("facet.tree.field", "{!ftree childField=child_ids}node_id");
	
    SolrQueryResponse rsp = new SolrQueryResponse();
    rsp.add("responseHeader", new SimpleOrderedMap<>());
    SolrQueryRequest req = new LocalSolrQueryRequest(core, params);

    SolrRequestHandler handler = core.getRequestHandler(requestHandler);
    handler.handleRequest(req, rsp);
    req.close();
      
    assertNull(rsp.getException());
    
    NamedList results = rsp.getValues();
    NamedList facetTree = (NamedList) ((NamedList)(results.get("facet_counts"))).get("facet_trees");
    assertNotNull(facetTree);
    
    // Check the generated hierarchy - should be three levels deep
    List<Object> nodes = (List) facetTree.get("node_id");
    assertEquals(1, nodes.size());
    NamedList level1 = (NamedList) nodes.get(0);
    assertEquals("A", level1.get("value"));
    assertEquals(0L, level1.get("count"));
    assertEquals(1L, level1.get("total"));
    List level2Nodes = (List)level1.get("hierarchy");
    NamedList level2 = (NamedList)level2Nodes.get(0);
    assertEquals("AA", level2.get("value"));
    assertEquals(0L, level2.get("count"));
    assertEquals(1L, level2.get("total"));
    List level3Nodes = (List)level2.get("hierarchy");
    NamedList level3 = (NamedList)level3Nodes.get(0);
    assertEquals("AAA", level3.get("value"));
    assertEquals(1L, level3.get("count"));
    assertEquals(1L, level3.get("total"));
    assertNull(level3.get("hierarchy"));
}
 
Example 19
Source File: SimpleTreeFacetComponentTest.java    From BioSolr with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testMultipleResults() {
	SolrCore core = h.getCore();
	
	ModifiableSolrParams params = new ModifiableSolrParams();
	params.add("q", "name:nodeA*");
	params.add("facet", "true");
	params.add("facet.tree", "true");
	params.add("facet.tree.field", "{!ftree childField=child_ids}node_id");
	
    SolrQueryResponse rsp = new SolrQueryResponse();
    rsp.add("responseHeader", new SimpleOrderedMap<>());
    SolrQueryRequest req = new LocalSolrQueryRequest(core, params);

    SolrRequestHandler handler = core.getRequestHandler(requestHandler);
    handler.handleRequest(req, rsp);
    req.close();
      
    assertNull(rsp.getException());
    
    NamedList results = rsp.getValues();
    NamedList facetTree = (NamedList) ((NamedList)(results.get("facet_counts"))).get("facet_trees");
    assertNotNull(facetTree);
    
    // Check the generated hierarchy - should be three levels deep
    List<Object> nodes = (List) facetTree.get("node_id");
    assertEquals(1, nodes.size());
    NamedList level1 = (NamedList) nodes.get(0);
    assertEquals("A", level1.get("value"));
    assertEquals(1L, level1.get("count"));
    assertEquals(6L, level1.get("total"));
    List level2Nodes = (List)level1.get("hierarchy");
    assertEquals(3, level2Nodes.size());
    NamedList level2 = (NamedList)level2Nodes.get(0);
    assertEquals(1L, level2.get("count"));
    assertEquals(3L, level2.get("total"));
    List level3Nodes = (List)level2.get("hierarchy");
    assertEquals(2, level3Nodes.size());
    NamedList level3 = (NamedList)level3Nodes.get(0);
    assertEquals(1L, level3.get("count"));
    assertEquals(1L, level3.get("total"));
    assertNull(level3.get("hierarchy"));
}
 
Example 20
Source File: SimpleTreeFacetComponentTest.java    From BioSolr with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testResultsWithOtherFacets() {
	SolrCore core = h.getCore();
	
	ModifiableSolrParams params = new ModifiableSolrParams();
	params.add("q", "name:nodeA*");
	params.add("facet", "true");
	params.add("facet.tree", "true");
	params.add("facet.tree.field", "{!ftree childField=child_ids}node_id");
	params.add("facet.field", "label");
	params.add("facet.field", "node_id");
	
    SolrQueryResponse rsp = new SolrQueryResponse();
    rsp.add("responseHeader", new SimpleOrderedMap<>());
    SolrQueryRequest req = new LocalSolrQueryRequest(core, params);

    SolrRequestHandler handler = core.getRequestHandler(requestHandler);
    handler.handleRequest(req, rsp);
    req.close();
      
    assertNull(rsp.getException());
    
    NamedList results = rsp.getValues();
    
    NamedList facetFields = (NamedList)((NamedList)(results.get("facet_counts"))).get("facet_fields");
    assertNotNull(facetFields);
    assertNotNull(facetFields.get("label"));
    assertNotNull(facetFields.get("node_id"));
    
    NamedList facetTree = (NamedList) ((NamedList)(results.get("facet_counts"))).get("facet_trees");
    assertNotNull(facetTree);
    
    // Check the generated hierarchy - should be three levels deep
    List<Object> nodes = (List) facetTree.get("node_id");
    assertEquals(1, nodes.size());
    NamedList level1 = (NamedList) nodes.get(0);
    assertEquals("A", level1.get("value"));
    assertEquals(1L, level1.get("count"));
    assertEquals(6L, level1.get("total"));
    List level2Nodes = (List)level1.get("hierarchy");
    assertEquals(3, level2Nodes.size());
    NamedList level2 = (NamedList)level2Nodes.get(0);
    assertEquals(1L, level2.get("count"));
    assertEquals(3L, level2.get("total"));
    List level3Nodes = (List)level2.get("hierarchy");
    assertEquals(2, level3Nodes.size());
    NamedList level3 = (NamedList)level3Nodes.get(0);
    assertEquals(1L, level3.get("count"));
    assertEquals(1L, level3.get("total"));
    assertNull(level3.get("hierarchy"));
}