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

The following examples show how to use org.apache.solr.client.solrj.response.QueryResponse#getFacetQuery() . 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: ItemSearchServiceLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenSearchingWithFacetQuery_thenAllMatchingFacetsShouldAvialble() throws Exception {
    itemSearchService.index("hm0001", "Brand1 Washing Machine", "CategoryA", 100f);
    itemSearchService.index("hm0002", "Brand1 Refrigerator", "CategoryA", 300f);
    itemSearchService.index("hm0003", "Brand2 Ceiling Fan", "CategoryB", 200f);
    itemSearchService.index("hm0004", "Brand2 Dishwasher", "CategoryB", 250f);

    SolrQuery query = new SolrQuery();
    query.setQuery("*:*");

    query.addFacetQuery("Washing OR Refrigerator");
    query.addFacetQuery("Brand2");

    QueryResponse response = solrClient.query(query);
    Map<String, Integer> facetQueryMap = response.getFacetQuery();

    assertEquals(2, facetQueryMap.size());

    for (Map.Entry<String, Integer> entry : facetQueryMap.entrySet()) {
        String facetQuery = entry.getKey();

        if ("Washing OR Refrigerator".equals(facetQuery)) {
            assertEquals(Integer.valueOf(2), entry.getValue());
        } else if ("Brand2".equals(facetQuery)) {
            assertEquals(Integer.valueOf(2), entry.getValue());
        } else {
            fail("unexpected query");
        }

    }
}
 
Example 2
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 3
Source File: SolrExampleTests.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testPivotFacetsQueries() throws Exception {
  SolrClient client = getSolrClient();

  // Empty the database...
  client.deleteByQuery("*:*");// delete everything!
  client.commit();
  assertNumFound("*:*", 0); // make sure it got in

  int id = 1;
  ArrayList<SolrInputDocument> docs = new ArrayList<>();
  docs.add(makeTestDoc("id", id++, "features", "aaa", "cat", "a", "inStock", true, "popularity", 12, "price", .017));
  docs.add(makeTestDoc("id", id++, "features", "aaa", "cat", "a", "inStock", false, "popularity", 13, "price", 16.04));
  docs.add(makeTestDoc("id", id++, "features", "aaa", "cat", "a", "inStock", true, "popularity", 14, "price", 12.34));
  docs.add(makeTestDoc("id", id++, "features", "aaa", "cat", "b", "inStock", false, "popularity", 24, "price", 51.39));
  docs.add(makeTestDoc("id", id++, "features", "aaa", "cat", "b", "inStock", true, "popularity", 28, "price", 131.39));
  docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "a", "inStock", false, "popularity", 32));
  docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "a", "inStock", true, "popularity", 31, "price", 131.39));
  docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "b", "inStock", false, "popularity", 36));
  docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "b", "inStock", true, "popularity", 37, "price", 1.39));
  docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "b", "inStock", false, "popularity", 38, "price", 47.98));
  docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "b", "inStock", true, "popularity", -38));
  docs.add(makeTestDoc("id", id++, "cat", "b")); // something not matching all fields
  client.add(docs);
  client.commit();

  SolrQuery query = new SolrQuery("*:*");
  query.addFacetPivotField("{!query=s1}features,manu");
  query.addFacetQuery("{!key=highPrice tag=s1}price:[100 TO *]");
  query.addFacetQuery("{!tag=s1 key=lowPrice}price:[0 TO 50]");
  query.setFacetMinCount(0);
  query.setRows(0);
  QueryResponse rsp = client.query(query);

  Map<String,Integer> map = rsp.getFacetQuery();
  assertEquals(2, map.get("highPrice").intValue());
  assertEquals(5, map.get("lowPrice").intValue());
  
  NamedList<List<PivotField>> pivots = rsp.getFacetPivot();
  List<PivotField> pivotValues = pivots.get("features,manu");

  PivotField featuresBBBPivot = pivotValues.get(0);
  assertEquals("features", featuresBBBPivot.getField());
  assertEquals("bbb", featuresBBBPivot.getValue());
  assertNotNull(featuresBBBPivot.getFacetQuery());
  assertEquals(2, featuresBBBPivot.getFacetQuery().size());
  assertEquals(1, featuresBBBPivot.getFacetQuery().get("highPrice").intValue());
  assertEquals(2, featuresBBBPivot.getFacetQuery().get("lowPrice").intValue());
  
  PivotField featuresAAAPivot = pivotValues.get(1);
  assertEquals("features", featuresAAAPivot.getField());
  assertEquals("aaa", featuresAAAPivot.getValue());
  assertNotNull(featuresAAAPivot.getFacetQuery());
  assertEquals(2, featuresAAAPivot.getFacetQuery().size());
  assertEquals(1, featuresAAAPivot.getFacetQuery().get("highPrice").intValue());
  assertEquals(3, featuresAAAPivot.getFacetQuery().get("lowPrice").intValue());
}