org.apache.solr.client.solrj.response.FacetField Java Examples

The following examples show how to use org.apache.solr.client.solrj.response.FacetField. 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: ManagerBase.java    From ambari-logsearch with Apache License 2.0 6 votes vote down vote up
protected List<String> getClusters(SolrDaoBase solrDaoBase, String clusterField, String event) {
  List<String> clusterResponse = Lists.newArrayList();
  SolrQuery solrQuery = new SolrQuery();
  solrQuery.setQuery("*:*");
  SolrUtil.setFacetField(solrQuery, clusterField);
  SolrUtil.setFacetSort(solrQuery, LogSearchConstants.FACET_INDEX);

  QueryResponse response = solrDaoBase.process(solrQuery, event);
  if (response == null) {
    return clusterResponse;
  }
  List<FacetField> clusterFields = response.getFacetFields();
  if (CollectionUtils.isNotEmpty(clusterFields)) {
    FacetField clusterFacets = clusterFields.get(0);
    for (FacetField.Count clusterCount : clusterFacets.getValues()) {
      clusterResponse.add(clusterCount.getName());
    }
  }
  return clusterResponse;
}
 
Example #2
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 #3
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 #4
Source File: DistributedAlfrescoSolrFacetingIT.java    From SearchServices with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void fieldFaceting_perFieldMincountSetTwo_shoulReturnFacetsMincountTwo() throws Exception
{
    String expectedContentFacetField = "{http://www.alfresco.org/model/content/1.0}content:[contenttwo (4)]";
    String expectedNameFacetField = "{http://www.alfresco.org/model/content/1.0}name:[nametwo (4), nameone (1)]";

    String jsonQuery = "{\"query\":\"(suggest:a)\",\"locales\":[\"en\"], \"templates\": [{\"name\":\"t1\", \"template\":\"%cm:content\"}], \"authorities\": [\"joel\"], \"tenants\": []}";
    putHandleDefaults();

    QueryResponse queryResponse = query(getDefaultTestClient(), true, jsonQuery,
        params("qt", "/afts", "shards.qt", "/afts", "start", "0", "rows", "0", "fl", "score,id", "facet", "true",
            "facet.field", "{http://www.alfresco.org/model/content/1.0}content",
            "facet.field", "{http://www.alfresco.org/model/content/1.0}name",
            "f.{http://www.alfresco.org/model/content/1.0}content.facet.mincount", "2",
            "f.{http://www.alfresco.org/model/content/1.0}name.facet.mincount", "0"));

    List<FacetField> facetFields = queryResponse.getFacetFields();
    FacetField contentFacetField = facetFields.get(0);
    Assert.assertThat(contentFacetField.toString(), is(expectedContentFacetField));
    FacetField nameFacetField = facetFields.get(1);
    Assert.assertThat(nameFacetField.toString(), is(expectedNameFacetField));
}
 
Example #5
Source File: DistributedAlfrescoSolrFacetingIT.java    From SearchServices with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void fieldFaceting_perFieldMincountSetZero_shoulReturnFacetsMincountOne() throws Exception
{
    String expectedContentFacetField = "{http://www.alfresco.org/model/content/1.0}content:[contenttwo (4), contentone (1)]";
    String expectedNameFacetField = "{http://www.alfresco.org/model/content/1.0}name:[nametwo (4), nameone (1)]";

    String jsonQuery = "{\"query\":\"(suggest:a)\",\"locales\":[\"en\"], \"templates\": [{\"name\":\"t1\", \"template\":\"%cm:content\"}], \"authorities\": [\"joel\"], \"tenants\": []}";
    putHandleDefaults();

    QueryResponse queryResponse = query(getDefaultTestClient(), true, jsonQuery,
        params("qt", "/afts", "shards.qt", "/afts", "start", "0", "rows", "0", "fl", "score,id", "facet", "true",
            "facet.field", "{http://www.alfresco.org/model/content/1.0}content",
            "facet.field", "{http://www.alfresco.org/model/content/1.0}name",
            "f.{http://www.alfresco.org/model/content/1.0}content.facet.mincount", "0",
            "f.{http://www.alfresco.org/model/content/1.0}name.facet.mincount", "0"));

    List<FacetField> facetFields = queryResponse.getFacetFields();
    FacetField contentFacetField = facetFields.get(0);
    Assert.assertThat(contentFacetField.toString(), is(expectedContentFacetField));
    FacetField nameFacetField = facetFields.get(1);
    Assert.assertThat(nameFacetField.toString(), is(expectedNameFacetField));
}
 
Example #6
Source File: DistributedAlfrescoSolrFacetingIT.java    From SearchServices with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void fieldFaceting_mincountSetTwo_shouldReturnFacetsOriginalMincount() throws Exception
{
    String expectedContentFacetField = "{http://www.alfresco.org/model/content/1.0}content:[contenttwo (4)]";
    String expectedNameFacetField = "{http://www.alfresco.org/model/content/1.0}name:[nametwo (4)]";

    String jsonQuery = "{\"query\":\"(suggest:a)\",\"locales\":[\"en\"], \"templates\": [{\"name\":\"t1\", \"template\":\"%cm:content\"}], \"authorities\": [\"joel\"], \"tenants\": []}";
    putHandleDefaults();

    QueryResponse queryResponse = query(getDefaultTestClient(), true, jsonQuery,
        params("qt", "/afts", "shards.qt", "/afts", "start", "0", "rows", "0", "fl", "score,id", "facet", "true",
            "facet.field", "{http://www.alfresco.org/model/content/1.0}content",
            "facet.field", "{http://www.alfresco.org/model/content/1.0}name",
            "facet.mincount", "2"));

    List<FacetField> facetFields = queryResponse.getFacetFields();
    FacetField contentFacetField = facetFields.get(0);
    Assert.assertThat(contentFacetField.toString(), is(expectedContentFacetField));
    FacetField nameFacetField = facetFields.get(1);
    Assert.assertThat(nameFacetField.toString(), is(expectedNameFacetField));
}
 
Example #7
Source File: DistributedAlfrescoSolrFacetingIT.java    From SearchServices with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void fieldFaceting_mincountSetZero_shouldReturnFacetsMincountOne() throws Exception
{
    String expectedContentFacetField = "{http://www.alfresco.org/model/content/1.0}content:[contenttwo (4), contentone (1)]";
    String expectedNameFacetField = "{http://www.alfresco.org/model/content/1.0}name:[nametwo (4), nameone (1)]";

    String jsonQuery = "{\"query\":\"(suggest:a)\",\"locales\":[\"en\"], \"templates\": [{\"name\":\"t1\", \"template\":\"%cm:content\"}], \"authorities\": [\"joel\"], \"tenants\": []}";
    putHandleDefaults();

    QueryResponse queryResponse = query(getDefaultTestClient(), true, jsonQuery,
        params("qt", "/afts", "shards.qt", "/afts", "start", "0", "rows", "0", "fl", "score,id", "facet", "true",
            "facet.field", "{http://www.alfresco.org/model/content/1.0}content",
            "facet.field", "{http://www.alfresco.org/model/content/1.0}name",
            "facet.mincount", "0"));

    List<FacetField> facetFields = queryResponse.getFacetFields();
    FacetField contentFacetField = facetFields.get(0);
    Assert.assertThat(contentFacetField.toString(), is(expectedContentFacetField));
    FacetField nameFacetField = facetFields.get(1);
    Assert.assertThat(nameFacetField.toString(), is(expectedNameFacetField));
}
 
Example #8
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 #9
Source File: ResponseDataGenerator.java    From ambari-logsearch with Apache License 2.0 6 votes vote down vote up
public List<Count> generateCount(QueryResponse response) {
  List<Count> counts = new ArrayList<>();
  List<FacetField> facetFields = null;
  FacetField facetField = null;
  if (response == null) {
    return counts;
  }

  facetFields = response.getFacetFields();
  if (facetFields == null) {
    return counts;
  }
  if (!facetFields.isEmpty()) {
    facetField = facetFields.get(0);
  }
  if (facetField != null) {
    counts = facetField.getValues();
  }
  return counts;
}
 
Example #10
Source File: DistributedAlfrescoSolrFacetingIT.java    From SearchServices with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void fieldFaceting_mincountMissing_shouldReturnFacetsMincountOne() throws Exception
{
    String expectedContentFacetField = "{http://www.alfresco.org/model/content/1.0}content:[contenttwo (4), contentone (1)]";
    String expectedNameFacetField = "{http://www.alfresco.org/model/content/1.0}name:[nametwo (4), nameone (1)]";

    String jsonQuery = "{\"query\":\"(suggest:a)\",\"locales\":[\"en\"], \"templates\": [{\"name\":\"t1\", \"template\":\"%cm:content\"}], \"authorities\": [\"joel\"], \"tenants\": []}";
    putHandleDefaults();

    QueryResponse queryResponse = query(getDefaultTestClient(), true, jsonQuery,
        params("qt", "/afts", "shards.qt", "/afts", "start", "0", "rows", "0", "fl", "score,id", "facet", "true",
            "facet.field", "{http://www.alfresco.org/model/content/1.0}content","facet.field", "{http://www.alfresco.org/model/content/1.0}name"));

    List<FacetField> facetFields = queryResponse.getFacetFields();
    FacetField contentFacetField = facetFields.get(0);
    Assert.assertThat(contentFacetField.toString(), is(expectedContentFacetField));
    FacetField nameFacetField = facetFields.get(1);
    Assert.assertThat(nameFacetField.toString(), is(expectedNameFacetField));
}
 
Example #11
Source File: TestDistributedSearch.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected void checkMinCountsField(List<FacetField.Count> counts, Object[] pairs) {
  assertEquals("There should be exactly " + pairs.length / 2 + " returned counts. There were: " + counts.size(), counts.size(), pairs.length / 2);
  assertTrue("Variable len param must be an even number, it was: " + pairs.length, (pairs.length % 2) == 0);
  for (int pairs_idx = 0, counts_idx = 0; pairs_idx < pairs.length; pairs_idx += 2, counts_idx++) {
    String act_name = counts.get(counts_idx).getName();
    long act_count = counts.get(counts_idx).getCount();
    String exp_name = (String) pairs[pairs_idx];
    long exp_count = (long) pairs[pairs_idx + 1];
    assertEquals("Expected ordered entry " + exp_name + " at position " + counts_idx + " got " + act_name, act_name, exp_name);
    assertEquals("Expected count for entry: " + exp_name + " at position " + counts_idx + " got " + act_count, act_count, exp_count);
  }
}
 
Example #12
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 #13
Source File: SolrSearchResults.java    From aem-solr-search with Apache License 2.0 5 votes vote down vote up
private void cleanQueryAndResponseFacets() {
		String[] filterQueries = solrQuery.getFilterQueries();
//		if (filterQueries != null) {
//			for (int i=0; i<filterQueries.length; i++)
//				filterQueries[i] = cleanFilterQuery(filterQueries[i]);
//			solrQuery.setFilterQueries(filterQueries);
//		}
		
		for (FacetField field : queryResponse.getFacetFields())
			for (FacetField.Count fieldCount : field.getValues())
				fieldCount.setName(cleanFilterQuery(fieldCount.getName()));
	}
 
Example #14
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 #15
Source File: SolrSearchDaoTest.java    From metron with Apache License 2.0 5 votes vote down vote up
@Test
public void getFacetCountsShouldProperlyReturnFacetCounts() {
  QueryResponse queryResponse = mock(QueryResponse.class);

  FacetField facetField1 = new FacetField("field1");
  facetField1.add("value1", 1);
  facetField1.add("value2", 2);
  FacetField facetField2 = new FacetField("field2");
  facetField2.add("value3", 3);
  facetField2.add("value4", 4);
  when(queryResponse.getFacetField("field1")).thenReturn(facetField1);
  when(queryResponse.getFacetField("field2")).thenReturn(facetField2);

  Map<String, Map<String, Long>> expectedFacetCounts = new HashMap<String, Map<String, Long>>() {{
    put("field1", new HashMap<String, Long>() {{
      put("value1", 1L);
      put("value2", 2L);
    }});
    put("field2", new HashMap<String, Long>() {{
      put("value3", 3L);
      put("value4", 4L);
    }});
  }};

  assertEquals(expectedFacetCounts,
      solrSearchDao.getFacetCounts(Arrays.asList("field1", "field2"), queryResponse));
}
 
Example #16
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 #17
Source File: DocValuesNotIndexedTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void doTestFacet(FieldProps props, QueryResponse rsp) {
  String name = props.getName();
  final List<FacetField.Count> counts = rsp.getFacetField(name).getValues();
  long expectedCount = props.getExpectedCount();
  long foundCount = getCount(counts);
  assertEquals("Field " + name + " should have a count of " + expectedCount, expectedCount, foundCount);

}
 
Example #18
Source File: BasicDistributedZkTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void testSortableTextFaceting() throws Exception {
  SolrQuery query = new SolrQuery("*:*");
  query.addFacetField(tsort);
  query.setFacetMissing(false);
  QueryResponse resp = queryServer(query);
  List<FacetField> ffs = resp.getFacetFields();
  for (FacetField ff : ffs) {
    if (ff.getName().equals(tsort) == false) continue;
    for (FacetField.Count count : ff.getValues()) {
      long num = count.getCount();
      switch (count.getName()) {
        case "all the kings horses and all the kings men":
        case "An eye for eye only ends up making the whole world blind.":
        case "Great works are performed, not by strength, but by perseverance.":
        case "how now brown cow":
        case "no eggs on wall, lesson learned":
        case "now is the time for all good men":
        case "this too shall pass":
        case "to come to the aid of their country.":
          assertEquals("Should have exactly one facet count for field " + ff.getName(), 1, num);
          break;
        case "the quick fox jumped over the lazy dog":
          assertEquals("Should have 5 docs for the lazy dog", 5, num);
          break;
        default:
          fail("No case for facet '" + ff.getName() + "'");

      }
    }
  }
}
 
Example #19
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 #20
Source File: SolrProductSearch.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
private static List<Map<String, Object>> prepareAndRunSorlCategoryQuery(DispatchContext dctx, Map<String, Object> context, String catalogId,
        String categoryPath, String facetPrefix, int level) throws Exception {
    // TODO: Use this method in sideDeepCategories
    Map<String, Object> query = getAvailableCategories(dctx, context, catalogId, categoryPath, null, facetPrefix, false, 0, 0);
    if (ServiceUtil.isError(query)) {
        throw new Exception(ServiceUtil.getErrorMessage(query));
    }
    QueryResponse cat = (QueryResponse) query.get("rows");
    List<Map<String, Object>> result = UtilMisc.newList();
    List<FacetField> catList = (List<FacetField>) cat.getFacetFields();
    for (Iterator<FacetField> catIterator = catList.iterator(); catIterator.hasNext();) {
        FacetField field = (FacetField) catIterator.next();
        List<Count> catL = (List<Count>) field.getValues();
        if (catL != null) {
            for (Iterator<Count> catIter = catL.iterator(); catIter.hasNext();) {
                FacetField.Count facet = (FacetField.Count) catIter.next();
                if (facet.getCount() > 0) {
                    Map<String, Object> catMap = new HashMap<>();
                    List<String> iName = new LinkedList<>();
                    iName.addAll(Arrays.asList(facet.getName().split("/")));
                    catMap.put("catId", iName.get(iName.size() - 1)); 
                    iName.remove(0); // remove first
                    String path = facet.getName();
                    catMap.put("path", path);
                    if (level > 0) {
                        iName.remove(iName.size() - 1); // remove last
                        catMap.put("parentCategory", StringUtils.join(iName, "/"));
                    } else {
                        catMap.put("parentCategory", null);
                    }
                    catMap.put("count", Long.toString(facet.getCount()));
                    result.add(catMap);
                }
            }
        }
    }
    return result;
}
 
Example #21
Source File: AssociativeLogicUtils.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
public static Set<Tuple> getTupleOfValues(FacetField facetField) {
	Set<Tuple> tuples = new HashSet<>(facetField.getValueCount());
	for (FacetField.Count distinctValue : facetField.getValues()) {
		Tuple tuple = new Tuple(1);
		tuple.add(distinctValue.getName());
		tuples.add(tuple);
	}
	return tuples;
}
 
Example #22
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 #23
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 #24
Source File: SolrProductSearch.java    From scipio-erp with Apache License 2.0 4 votes vote down vote up
/**
 * Return a map of the side deep categories.
 */
public static Map<String, Object> solrSideDeepCategory(DispatchContext dctx, Map<String, Object> context) {
    Map<String, Object> result;
    try {
        String catalogId = (String) context.get("catalogId");
        if (catalogId != null && catalogId.isEmpty()) catalogId = null; // TODO: REVIEW: is this necessary?
        List<String> currentTrail = UtilGenerics.checkList(context.get("currentTrail"));

        // 2016-03-22: FIXME?: I think we could call getCategoryNameWithTrail with showDepth=false,
        // instead of check in loop...
        String productCategoryId = SolrCategoryUtil.getCategoryNameWithTrail((String) context.get("productCategoryId"), catalogId, dctx, currentTrail);
        result = ServiceUtil.returnSuccess();
        Map<String, List<Map<String, Object>>> catLevel = new HashMap<>();
        if (Debug.verboseOn()) Debug.logVerbose("Solr: getSideDeepCategories: productCategoryId: " + productCategoryId, module);

        // Add toplevel categories
        String[] trailElements = productCategoryId.split("/");

        long numFound = 0;
        boolean isFirstElement = true;

        // iterate over actual results
        for (String element : trailElements) {
            if (Debug.verboseOn()) Debug.logVerbose("Solr: getSideDeepCategories: iterating element: " + element, module);
            List<Map<String, Object>> categories = new ArrayList<>();
            int level;
            // 2016-03-22: Don't make a query for the first element, which is the count,
            // but for compatibility, still make a map entry for it
            // NOTE: I think this could be skipped entirely because level 0 is replaced/taken by the
            // first category, but leaving in to play it safe
            if (isFirstElement) {
                level = 0;
                isFirstElement = false;
            } else {
                String categoryPath = SolrCategoryUtil.getCategoryNameWithTrail(element, catalogId, dctx, currentTrail);
                String[] categoryPathArray = categoryPath.split("/");
                level = Integer.parseInt(categoryPathArray[0]);
                String facetPrefix = SolrCategoryUtil.getFacetFilterForCategory(categoryPath, dctx);
                // 2016-03-22: IMPORTANT: the facetPrefix MUST end with / otherwise it will return unrelated categories!
                // solr facetPrefix is not aware of our path delimiters
                if (!facetPrefix.endsWith("/")) {
                    facetPrefix += "/";
                }
                // Debug.logInfo("categoryPath: "+categoryPath + "
                // facetPrefix: "+facetPrefix,module);
                Map<String, Object> query = getAvailableCategories(dctx, context, catalogId, categoryPath, null, facetPrefix, false, 0, 0);
                if (ServiceUtil.isError(query)) {
                    throw new Exception(ServiceUtil.getErrorMessage(query));
                }

                QueryResponse cat = (QueryResponse) query.get("rows");
                Long subNumFound = (Long) query.get("numFound");
                if (subNumFound != null) {
                    numFound += subNumFound;
                }
                List<FacetField> catList = (List<FacetField>) cat.getFacetFields();
                for (Iterator<FacetField> catIterator = catList.iterator(); catIterator.hasNext();) {
                    FacetField field = (FacetField) catIterator.next();
                    List<Count> catL = (List<Count>) field.getValues();
                    if (catL != null) {
                        for (Iterator<Count> catIter = catL.iterator(); catIter.hasNext();) {
                            FacetField.Count facet = (FacetField.Count) catIter.next();
                            if (facet.getCount() > 0) {
                                Map<String, Object> catMap = new HashMap<>();
                                List<String> iName = new LinkedList<>();
                                iName.addAll(Arrays.asList(facet.getName().split("/")));
                                // Debug.logInfo("topLevel "+topLevel,"");
                                // int l = Integer.parseInt((String)
                                // iName.getFirst());
                                catMap.put("catId", iName.get(iName.size() - 1)); // get last
                                iName.remove(0); // remove first
                                String path = facet.getName();
                                catMap.put("path", path);
                                if (level > 0) {
                                    iName.remove(iName.size() - 1); // remove last
                                    catMap.put("parentCategory", StringUtils.join(iName, "/"));
                                } else {
                                    catMap.put("parentCategory", null);
                                }
                                catMap.put("count", Long.toString(facet.getCount()));
                                categories.add(catMap);
                            }
                        }
                    }
                }
            }
            catLevel.put("menu-" + level, categories);
        }
        result.put("categories", catLevel);
        result.put("numFound", numFound);

    } catch (Exception e) {
        result = ServiceUtil.returnError(e.toString());
        result.put("numFound", (long) 0);
        return result;
    }
    return result;
}
 
Example #25
Source File: DocValuesNotIndexedTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private long getCount(final List<FacetField.Count> counts) {
  return counts.stream().mapToLong(FacetField.Count::getCount).sum();
}
 
Example #26
Source File: SolrProductSearch.java    From scipio-erp with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a map of the categories currently available under the root
 * element.
 */
public static Map<String, Object> solrAvailableCategories(DispatchContext dctx, Map<String, Object> context) {
    Map<String, Object> result;
    try {
        boolean displayProducts = Boolean.TRUE.equals(context.get("displayProducts"));
        int viewIndex = 0;
        int viewSize = 9;
        if (displayProducts) {
            viewIndex = (Integer) context.get("viewIndex");
            viewSize = (Integer) context.get("viewSize");
        }
        String catalogId = (String) context.get("catalogId");
        if (catalogId != null && catalogId.isEmpty()) catalogId = null; // TODO: REVIEW: is this necessary?

        List<String> currentTrail = UtilGenerics.checkList(context.get("currentTrail"));
        String productCategoryId = SolrCategoryUtil.getCategoryNameWithTrail((String) context.get("productCategoryId"),
                catalogId, dctx, currentTrail);
        String productId = (String) context.get("productId");
        if (Debug.verboseOn()) Debug.logVerbose("Solr: getAvailableCategories: productCategoryId: " + productCategoryId, module);
        Map<String, Object> query = getAvailableCategories(dctx, context, catalogId, productCategoryId, productId, null,
                displayProducts, viewIndex, viewSize);
        if (ServiceUtil.isError(query)) {
            throw new Exception(ServiceUtil.getErrorMessage(query));
        }

        QueryResponse cat = (QueryResponse) query.get("rows");
        result = ServiceUtil.returnSuccess();
        result.put("numFound", (long) 0);
        Map<String, Object> categories = new HashMap<>();
        List<FacetField> catList = (List<FacetField>) cat.getFacetFields();
        for (Iterator<FacetField> catIterator = catList.iterator(); catIterator.hasNext();) {
            FacetField field = (FacetField) catIterator.next();
            List<Count> catL = (List<Count>) field.getValues();
            if (catL != null) {
                // log.info("FacetFields = "+catL);
                for (Iterator<Count> catIter = catL.iterator(); catIter.hasNext();) {
                    FacetField.Count f = (FacetField.Count) catIter.next();
                    if (f.getCount() > 0) {
                        categories.put(f.getName(), Long.toString(f.getCount()));
                    }
                }
                result.put("categories", categories);
                result.put("numFound", cat.getResults().getNumFound());
                // log.info("The returned map is this:"+result);
            }
        }
    } catch (Exception e) {
        result = ServiceUtil.returnError(e.toString());
        result.put("numFound", (long) 0);
        return result;
    }
    return result;
}
 
Example #27
Source File: SolrProductSearch.java    From scipio-erp with Apache License 2.0 4 votes vote down vote up
/**
 * Performs solr products search.
 */
public static Map<String, Object> solrProductsSearch(DispatchContext dctx, Map<String, Object> context) {
    Map<String, Object> result;
    LocalDispatcher dispatcher = dctx.getDispatcher();

    try {
        Map<String, Object> dispatchMap = dctx.makeValidContext("runSolrQuery", ModelService.IN_PARAM, context);

        if (UtilValidate.isNotEmpty(context.get("productCategoryId"))) {
            String productCategoryId = (String) context.get("productCategoryId");
            // causes erroneous results for similar-name categories
            //dispatchMap.put("query", "cat:*" + SolrUtil.escapeTermFull(productCategoryId) + "*");
            boolean includeSubCategories = !Boolean.FALSE.equals(context.get("includeSubCategories"));
            dispatchMap.put("query", SolrExprUtil.makeCategoryIdFieldQueryEscape("cat", productCategoryId, includeSubCategories));
        } else {
            return ServiceUtil.returnError("Missing productCategoryId"); // TODO: localize
        }
        Integer viewSize = (Integer) dispatchMap.get("viewSize");
        //Integer viewIndex = (Integer) dispatchMap.get("viewIndex");
        if (dispatchMap.get("facet") == null) dispatchMap.put("facet", false);
        if (dispatchMap.get("spellcheck") == null) dispatchMap.put("spellcheck", false); // 2017-09: default changed to false
        if (dispatchMap.get("highlight") == null) dispatchMap.put("highlight", false); // 2017-09: default changed to false

        List<String> queryFilters = getEnsureQueryFiltersModifiable(dispatchMap);
        SolrQueryUtil.addDefaultQueryFilters(queryFilters, context); // 2018-05-25

        Map<String, Object> searchResult = dispatcher.runSync("runSolrQuery", dispatchMap);
        if (!ServiceUtil.isSuccess(searchResult)) {
            return copySolrQueryExtraOutParams(searchResult, ServiceUtil.returnResultSysFields(searchResult));
        }
        QueryResponse queryResult = (QueryResponse) searchResult.get("queryResult");
        result = ServiceUtil.returnSuccess();

        Map<String, Integer> facetQuery = queryResult.getFacetQuery();
        Map<String, String> facetQueries = null;
        if (facetQuery != null) {
            facetQueries = new HashMap<>();
            for (String fq : facetQuery.keySet()) {
                if (facetQuery.get(fq) > 0) {
                    facetQueries.put(fq, fq.replaceAll("^.*\\u005B(.*)\\u005D", "$1") + " (" + facetQuery.get(fq).intValue() + ")");
                }
            }
        }

        List<FacetField> facets = queryResult.getFacetFields();
        Map<String, Map<String, Long>> facetFields = null;
        if (facets != null) {
            facetFields = new HashMap<>();
            for (FacetField facet : facets) {
                Map<String, Long> facetEntry = new HashMap<>();
                List<FacetField.Count> facetEntries = facet.getValues();
                if (UtilValidate.isNotEmpty(facetEntries)) {
                    for (FacetField.Count fcount : facetEntries) {
                        facetEntry.put(fcount.getName(), fcount.getCount());
                    }
                    facetFields.put(facet.getName(), facetEntry);
                }
            }
        }

        result.put("results", queryResult.getResults());
        result.put("facetFields", facetFields);
        result.put("facetQueries", facetQueries);
        result.put("listSize", queryResult.getResults().getNumFound());
        // 2016-04-01: Need to translate this
        //result.put("viewIndex", queryResult.getResults().getStart());
        result.put("start", queryResult.getResults().getStart());
        result.put("viewIndex", SolrQueryUtil.calcResultViewIndex(queryResult.getResults(), viewSize));
        result.put("viewSize", viewSize);
    } catch (Exception e) {
        Debug.logError(e, "Solr: productsSearch: " + e.getMessage(), module);
        result = ServiceUtil.returnError(e.toString());
    }
    return result;
}
 
Example #28
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());
	}
}
 
Example #29
Source File: DistributedAlfrescoSolrFacetingIT.java    From SearchServices with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Test 
public void distributedSearch_fieldFacetingRequiringRefinement_shouldReturnCorrectCounts() throws Exception
{
    /*
     * The way this index is built is to have a correct distributed faceting count only after refining happens.
     * Given these params : facet.limit=2&facet.overrequest.count=0&facet.overrequest.ration=1
     * Initially you will get these counts:
     * Shard 0
     * a(2) b(2)
     * Shard 1
     * c(3) b(2)
     * We didn't get [c] from shard0 and [a] from shard1.
     * The refinement phase will ask those shards for those counts.
     * Only if refinement works we'll see the correct results (compared to not sharded Solr)
     * */
    index(getDefaultTestClient(), 0, "id", "10", "suggest", "b", "_version_", "0",
        "content@s___t@{http://www.alfresco.org/model/content/1.0}content", "a b");
    index(getDefaultTestClient(), 0, "id", "20", "suggest", "b", "_version_", "0",
        "content@s___t@{http://www.alfresco.org/model/content/1.0}content", "a");
    index(getDefaultTestClient(), 0, "id", "30", "suggest", "b", "_version_", "0",
        "content@s___t@{http://www.alfresco.org/model/content/1.0}content", "b c");
    index(getDefaultTestClient(), 1, "id", "40", "suggest", "b", "_version_", "0",
        "content@s___t@{http://www.alfresco.org/model/content/1.0}content", "c b");
    index(getDefaultTestClient(), 1, "id", "50", "suggest", "b", "_version_", "0",
        "content@s___t@{http://www.alfresco.org/model/content/1.0}content", "c b");
    index(getDefaultTestClient(), 1, "id", "60", "suggest", "b", "_version_", "0",
        "content@s___t@{http://www.alfresco.org/model/content/1.0}content", "c");
    commit(getDefaultTestClient(), true);
    String expectedFacetField = "{http://www.alfresco.org/model/content/1.0}content:[b (4), c (4)]";

    String jsonQuery = "{\"query\":\"(suggest:b)\",\"locales\":[\"en\"], \"templates\": [{\"name\":\"t1\", \"template\":\"%cm:content\"}], \"authorities\": [\"joel\"], \"tenants\": []}";
    putHandleDefaults();

    QueryResponse queryResponse = query(getDefaultTestClient(), true, jsonQuery,
        params("qt", "/afts", "shards.qt", "/afts", "start", "0", "rows", "0", "fl", "score,id", "facet", "true",
            "facet.field", "{http://www.alfresco.org/model/content/1.0}content", "facet.limit", "2",
            "facet.overrequest.count", "0", "facet.overrequest.ratio", "1"));

    List<FacetField> facetFields = queryResponse.getFacetFields();
    FacetField facetField = facetFields.get(0);
    Assert.assertThat(facetField.toString(), is(expectedFacetField));
}