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

The following examples show how to use org.apache.solr.client.solrj.response.QueryResponse#getFacetFields() . 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: 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 3
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 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_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 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_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 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_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 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_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 8
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 9
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 10
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));
}
 
Example 11
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 12
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 13
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 14
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() );
}