Java Code Examples for org.apache.solr.client.solrj.response.QueryResponse#getFacetField()

The following examples show how to use org.apache.solr.client.solrj.response.QueryResponse#getFacetField() . 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: ResponseDataGenerator.java    From ambari-logsearch with Apache License 2.0 6 votes vote down vote up
public Map<String, String> generateComponentMetadata(QueryResponse queryResponse,
                                                     String facetField, Map<String, String> componetnLabels) {
  Map<String, String> result = new HashMap<>();
  if (queryResponse == null) {
    return result;
  }
  FacetField facetFields = queryResponse.getFacetField(facetField);
  if (facetFields == null) {
    return result;
  }
  List<Count> counts = facetFields.getValues();
  if (counts == null) {
    return result;
  }
  for (Count count : counts) {
    if (count.getName() != null) {
      String label = componetnLabels.get(count.getName());
      String fallbackedLabel = labelFallbackHandler.fallbackIfRequired(count.getName(), label, true, false, true);
      result.put(count.getName(), fallbackedLabel);
    }
  }
  return result;
}
 
Example 2
Source File: CloudDatasetGraph.java    From SolRDF with Apache License 2.0 6 votes vote down vote up
@Override
public Iterator<Node> listGraphNodes() {
	try {
		final QueryResponse response = cloud.query(LIST_GRAPHS_QUERY);
		final FacetField graphFacetField = response.getFacetField(Field.C);
		if (graphFacetField != null && graphFacetField.getValueCount() > 0) {
			final List<Node> graphs = new ArrayList<Node>();				
			for (final FacetField.Count graphName : graphFacetField.getValues()) {
				graphs.add(NTriples.asURI(graphName.getName()));
			}
			return graphs.iterator();
		}
		return EMPTY_GRAPHS_ITERATOR;
	} catch (final Exception exception) {
		LOGGER.error(MessageCatalog._00113_NWS_FAILURE, exception);
		throw new SolrException(ErrorCode.SERVER_ERROR, exception);
	}	
}
 
Example 3
Source File: LuceneIndexHandler.java    From FXDesktopSearch with Apache License 2.0 6 votes vote down vote up
private void fillFacet(final String aFacetField, final String aBacklink, final QueryResponse aQueryResponse, final List<FacetDimension> aDimensions,
        final Function<String, String> aConverter) {
    final var theFacet = aQueryResponse.getFacetField(aFacetField);
    if (theFacet != null) {
        final List<Facet> theFacets = new ArrayList<>();
        for (final var theCount : theFacet.getValues()) {
            if (theCount.getCount() > 0) {
                final var theName = theCount.getName().trim();
                if (!theName.isEmpty()) {
                    theFacets.add(new Facet(aConverter.apply(theName), theCount.getCount(),
                            aBacklink + "/" + encode(
                                    FacetSearchUtils.encode(aFacetField, theCount.getName()))));
                }
            }
        }
        // Facetting only makes sense if there is more than one facet
        if (theFacets.size() > 1) {
            aDimensions.add(new FacetDimension(facetFieldToTitle.get(aFacetField), theFacets));
        }
    }
}
 
Example 4
Source File: SolrDocumentSearch.java    From BioSolr with Apache License 2.0 6 votes vote down vote up
private Map<String, List<FacetEntry>> extractFacets(QueryResponse response, FacetStyle facetStyle) {
	Map<String, List<FacetEntry>> facets = new HashMap<>();
	
	for (String name : config.getFacetFields()) {
		FacetField fld = response.getFacetField(name);
		if (fld != null && !fld.getValues().isEmpty()) {
			List<FacetEntry> facetValues = new ArrayList<>();
			for (Count count : fld.getValues()) {
				facetValues.add(new FacetEntry(count.getName(), count.getCount()));
			}
			facets.put(name, facetValues);
		}
	}
	
	// And extract the facet tree, if there is one
	if (facetStyle != FacetStyle.NONE) {
		List<Object> facetTree = findFacetTree(response, EFO_URI_FIELD);
		if (facetTree != null && !facetTree.isEmpty()) {
			facets.put(EFO_URI_FIELD + "_hierarchy", extractFacetTreeFromNamedList(facetTree));
		}
	}
	
	return facets;
}
 
Example 5
Source File: ServiceLogsManager.java    From ambari-logsearch with Apache License 2.0 5 votes vote down vote up
private <T extends LogData> GroupListResponse getFields(String field, String clusters, Class<T> clazz) {
  SolrQuery solrQuery = new SolrQuery();
  solrQuery.setQuery("*:*");
  SolrUtil.addListFilterToSolrQuery(solrQuery, CLUSTER, clusters);
  GroupListResponse collection = new GroupListResponse();
  SolrUtil.setFacetField(solrQuery, field);
  SolrUtil.setFacetSort(solrQuery, LogSearchConstants.FACET_INDEX);
  QueryResponse response = serviceLogsSolrDao.process(solrQuery);
  if (response == null) {
    return collection;
  }
  FacetField facetField = response.getFacetField(field);
  if (facetField == null) {
    return collection;
  }
  List<Count> fieldList = facetField.getValues();
  if (fieldList == null) {
    return collection;
  }
  SolrDocumentList docList = response.getResults();
  if (docList == null) {
    return collection;
  }
  List<LogData> groupList = new ArrayList<>(getLogDataListByFieldType(clazz, response, fieldList));

  collection.setGroupList(groupList);
  if (!docList.isEmpty()) {
    collection.setStartIndex((int) docList.getStart());
    collection.setTotalCount(docList.getNumFound());
  }
  return collection;
}
 
Example 6
Source File: ResponseDataGenerator.java    From ambari-logsearch with Apache License 2.0 5 votes vote down vote up
public BarGraphDataListResponse generateBarGraphFromFieldFacet(QueryResponse response, String facetField) {
  BarGraphDataListResponse dataList = new BarGraphDataListResponse();
  Collection<BarGraphData> vaDatas = new ArrayList<>();
  dataList.setGraphData(vaDatas);
  if (response == null) {
    return dataList;
  }
  FacetField facetFieldObj = response.getFacetField(facetField);
  if (facetFieldObj == null) {
    return dataList;
  }

  List<Count> counts = facetFieldObj.getValues();
  if (counts == null) {
    return dataList;
  }
  for (Count cnt : counts) {
    List<NameValueData> valueList = new ArrayList<>();
    BarGraphData vBarGraphData = new BarGraphData();
    vaDatas.add(vBarGraphData);
    NameValueData vNameValue = new NameValueData();
    vNameValue.setName(cnt.getName());
    vBarGraphData.setName(cnt.getName().toUpperCase());
    vNameValue.setValue("" + cnt.getCount());
    valueList.add(vNameValue);
    vBarGraphData.setDataCount(valueList);
  }
  return dataList;
}
 
Example 7
Source File: ResponseDataGenerator.java    From ambari-logsearch with Apache License 2.0 5 votes vote down vote up
public CountDataListResponse generateCountResponseByField(QueryResponse response, String field) {
  CountDataListResponse collection = new CountDataListResponse();
  List<CountData> vCounts = new ArrayList<>();
  if (response == null) {
    return collection;
  }
  FacetField facetFields = response.getFacetField(field);
  if (facetFields == null) {
    return collection;
  }
  List<Count> fieldList = facetFields.getValues();

  if (fieldList == null) {
    return collection;
  }

  for (Count cnt : fieldList) {
    if (cnt != null) {
      CountData vCount = new CountData();
      vCount.setName(cnt.getName());
      vCount.setCount(cnt.getCount());
      vCounts.add(vCount);
    }
  }
  collection.setvCounts(vCounts);
  return collection;
}
 
Example 8
Source File: DistributedFacetExistsSmallTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void assertResponse(QueryResponse rsp) {
  final FacetField facetField = rsp.getFacetField(FLD);

  assertThat(facetField.getValueCount(), is(6));
  final List<FacetField.Count> counts = facetField.getValues();
  for (FacetField.Count count : counts) {
    assertThat("Count for: " + count.getName(), count.getCount(), is(1L));
  }
  assertThat(counts.get(0).getName(), is("AAA"));
  assertThat(counts.get(1).getName(), is("B"));
  assertThat(counts.get(2).getName(), is("BB"));
}
 
Example 9
Source File: SolrSearchDao.java    From metron with Apache License 2.0 5 votes vote down vote up
protected Map<String, Map<String, Long>> getFacetCounts(List<String> fields,
    QueryResponse solrResponse) {
  Map<String, Map<String, Long>> fieldCounts = new HashMap<>();
  for (String field : fields) {
    Map<String, Long> valueCounts = new HashMap<>();
    FacetField facetField = solrResponse.getFacetField(field);
    for (Count facetCount : facetField.getValues()) {
      valueCounts.put(facetCount.getName(), facetCount.getCount());
    }
    fieldCounts.put(field, valueCounts);
  }
  return fieldCounts;
}
 
Example 10
Source File: SolrExampleTests.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testFaceting() throws Exception
{    
  SolrClient client = getSolrClient();
  
  // Empty the database...
  client.deleteByQuery("*:*");// delete everything!
  client.commit();
  assertNumFound( "*:*", 0 ); // make sure it got in
  
  ArrayList<SolrInputDocument> docs = new ArrayList<>(10);
  for( int i=1; i<=10; i++ ) {
    SolrInputDocument doc = new SolrInputDocument();
    doc.setField( "id", i+"" );
    if( (i%2)==0 ) {
      doc.addField( "features", "two" );
    }
    if( (i%3)==0 ) {
      doc.addField( "features", "three" );
    }
    if( (i%4)==0 ) {
      doc.addField( "features", "four" );
    }
    if( (i%5)==0 ) {
      doc.addField( "features", "five" );
    }
    docs.add( doc );
  }
  client.add(docs);
  client.commit();
  
  SolrQuery query = new SolrQuery( "*:*" );
  query.remove( FacetParams.FACET_FIELD );
  query.addFacetField( "features" );
  query.setFacetMinCount( 0 );
  query.setFacet( true );
  query.setRows( 0 );
  
  QueryResponse rsp = client.query( query );
  assertEquals(docs.size(), rsp.getResults().getNumFound());
  
  List<FacetField> facets = rsp.getFacetFields();
  assertEquals( 1, facets.size() );
  FacetField ff = facets.get( 0 );
  assertEquals( "features", ff.getName() );
  // System.out.println( "111: "+ff.getValues() );
  // check all counts
  assertEquals( "[two (5), three (3), five (2), four (2)]", ff.getValues().toString() );
  
  // should be the same facets with minCount=0
  query.setFilterQueries( "features:two" );
  rsp = client.query( query );
  ff = rsp.getFacetField( "features" );
  assertEquals("[two (5), four (2), five (1), three (1)]", ff.getValues().toString());
  
  // with minCount > 3
  query.setFacetMinCount(4);
  rsp = client.query( query );
  ff = rsp.getFacetField( "features" );
  assertEquals( "[two (5)]", ff.getValues().toString() );

  // with minCount > 3
  query.setFacetMinCount(-1);
  rsp = client.query( query );
  ff = rsp.getFacetField( "features" );
  
  // System.out.println( rsp.getResults().getNumFound() + " :::: 444: "+ff.getValues() );
}
 
Example 11
Source File: SearchITCase.java    From apache-solr-essentials with Apache License 2.0 4 votes vote down vote up
/**
 * Demonstrates how to ask for faceting and iterate over response facets.
 * 
 * @throws Exception hopefully never, otherwise the test fails.
 */
@Test
public void facets() throws Exception {
	// 1. Prepare the Query object
	// The query string can be directly injected in the constructor
	final SolrQuery query = new SolrQuery("*:*");
	query.setRequestHandler("/h1");
	
	// These settings will override the "defaults" section
	// Note that this handler sets facet to true, so the following line is
	// not actually needed
	query.setFacet(true);
	query.addFacetField("genre", "released");
	
	// We don't want highlighting here
	// Since the HL component is disabled by default, also this line is not needed.
	query.setHighlight(false);
	
	// We are only interested in facets, so skip don't include any 
	// document in the response.
	query.setRows(0);
	
	// 2. Send the query request and get the corresponding response.
	final QueryResponse response = SEARCHER.query(query);
	
	// 3. Get the result object, containing documents and metadata.
	final SolrDocumentList documents = response.getResults();
	
	// If not explicitly requested (or set in the handler) the start is set to 0
	assertEquals(0, documents.getStart());
	
	// Total number of documents found must be equals to all documents we previously indexed
	assertEquals(sampleData().size(), documents.getNumFound());
	
	// Page size must be 0, as requested
	assertEquals(0, documents.size());
	
	final FacetField genre = response.getFacetField("genre");
	assertNotNull(genre);
	 
	// This is something that should never appear within a TestCase :) 
	// however is useful to demonstrate how to iterate over facet values
	for (final Count count : genre.getValues()) {
		// e.g. Jazz : 19
		// e.g. Fusion: 11
		System.out.println(count.getName() + " : " + count.getCount());
	}
}