Example usage for org.apache.solr.client.solrj.response QueryResponse getFacetPivot

List of usage examples for org.apache.solr.client.solrj.response QueryResponse getFacetPivot

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj.response QueryResponse getFacetPivot.

Prototype

public NamedList<List<PivotField>> getFacetPivot() 

Source Link

Usage

From source file:au.org.ala.biocache.dao.SearchDAOImpl.java

License:Open Source License

/**
 * @see au.org.ala.biocache.dao.SearchDAO#searchPivot(au.org.ala.biocache.dto.SpatialSearchRequestParams)
 *///from  ww  w  .  j  a  v a  2  s  .c o  m
public List<FacetPivotResultDTO> searchPivot(SpatialSearchRequestParams searchParams) throws Exception {
    String pivot = StringUtils.join(searchParams.getFacets(), ",");
    searchParams.setFacets(new String[] {});

    queryFormatUtils.formatSearchQuery(searchParams);
    String queryString = searchParams.getFormattedQuery();
    searchParams.setFacet(true);

    //get facet group counts
    SolrQuery query = initSolrQuery(searchParams, false, null);
    query.setQuery(queryString);
    query.setFields(null);
    //now use the supplied facets to add groups to the query
    query.add("facet.pivot", pivot);
    query.add("facet.pivot.mincount", "1");
    query.add("facet.missing", "true");
    query.setRows(0);
    searchParams.setPageSize(0);
    QueryResponse response = runSolrQuery(query, searchParams);
    NamedList<List<PivotField>> result = response.getFacetPivot();

    List<FacetPivotResultDTO> output = new ArrayList();
    for (Entry<String, List<PivotField>> pfl : result) {
        List<PivotField> list = pfl.getValue();
        if (list != null && list.size() > 0) {
            output.add(new FacetPivotResultDTO(list.get(0).getField(), getFacetPivotResults(list), null,
                    (int) response.getResults().getNumFound()));
        }

        //should only be one result
        break;
    }

    return output;
}

From source file:com.frank.search.solr.core.ResultHelper.java

License:Apache License

static Map<com.frank.search.solr.core.query.PivotField, List<FacetPivotFieldEntry>> convertFacetQueryResponseToFacetPivotMap(
        FacetQuery query, QueryResponse response) {

    if (VersionUtil.isSolr3XAvailable()) {
        // pivot facets are a solr 4+ Feature
        return Collections.emptyMap();
    }/*from ww w. j a  v  a2 s. c  om*/

    Map<com.frank.search.solr.core.query.PivotField, List<FacetPivotFieldEntry>> facetResult = new HashMap<com.frank.search.solr.core.query.PivotField, List<FacetPivotFieldEntry>>();
    NamedList<List<PivotField>> facetPivot = response.getFacetPivot();
    if (facetPivot != null && facetPivot.size() > 0) {
        for (int i = 0; i < facetPivot.size(); i++) {
            String name = facetPivot.getName(i);
            List<PivotField> pivotResult = facetPivot.get(name);
            facetResult.put(new SimplePivotField(name), convertPivotResult(pivotResult));
        }
    }

    return facetResult;
}

From source file:com.nridge.ds.solr.SolrResponseBuilder.java

License:Open Source License

private void populateFacetPivot(QueryResponse aQueryResponse) {
    DataField schemaField, dataField;/*from   ww  w  . j  a v  a  2s .c o  m*/
    Logger appLogger = mAppMgr.getLogger(this, "populateFacetPivot");

    appLogger.trace(mAppMgr.LOGMSG_TRACE_ENTER);

    NamedList<List<PivotField>> facetPivotFields = aQueryResponse.getFacetPivot();
    if (facetPivotFields != null) {
        mDocument.addRelationship(Solr.RESPONSE_FACET_PIVOT, createFacetPivotBag());
        Relationship facetRelationship = mDocument.getFirstRelationship(Solr.RESPONSE_FACET_PIVOT);
        if (facetRelationship != null) {
            facetRelationship.getDocuments().clear();
            DataBag facetBag = new DataBag(facetRelationship.getBag());
            facetBag.setAssignedFlagAll(false);
            DataTable facetTable = new DataTable(facetBag);
            facetTable.add(new DataIntegerField("id", "Id"));
            facetTable.add(new DataIntegerField("parent_id", "Parent Id"));
            facetBag = facetTable.getColumnBag();
            facetBag.setTitle("Facet Pivot Table");
            DataBag resultBag = mBag;

            String[] facetNames = null;
            for (Map.Entry<String, List<PivotField>> entry : facetPivotFields) {
                facetNames = entry.getKey().split(",");
                for (String facetName : facetNames) {
                    schemaField = resultBag.getFieldByName(facetName);
                    if (schemaField == null)
                        dataField = new DataTextField(facetName, Field.nameToTitle(facetName));
                    else
                        dataField = new DataTextField(facetName, schemaField.getTitle());
                    dataField.setMultiValueFlag(true);
                    facetBag.add(dataField);
                }
            }
            if ((facetNames != null) && (facetNames.length > 0)) {
                int rowId = 0;
                int parentId = rowId;
                for (Map.Entry<String, List<PivotField>> entry : facetPivotFields) {
                    for (PivotField pivotField : entry.getValue()) {
                        rowId = populateFacetPivot(facetTable, facetNames, rowId, parentId, pivotField);
                        parentId = rowId;
                    }
                }
            }

            Document facetDocument = new Document(Solr.RESPONSE_FACET_PIVOT, facetTable);
            facetRelationship.add(facetDocument);
        }
    }

    appLogger.trace(mAppMgr.LOGMSG_TRACE_DEPART);
}

From source file:ddf.catalog.source.solr.SolrCatalogProvider.java

License:Open Source License

@Override
public Set<ContentType> getContentTypes() {

    Set<ContentType> finalSet = new HashSet<>();

    String contentTypeField = resolver.getField(Metacard.CONTENT_TYPE, AttributeFormat.STRING, true);
    String contentTypeVersionField = resolver.getField(Metacard.CONTENT_TYPE_VERSION, AttributeFormat.STRING,
            true);/*from w  ww. ja v a  2 s  . c  o  m*/

    /*
     * If we didn't find the field, it most likely means it does not exist. If it does not
     * exist, then we can safely say that no content types are in this catalog provider
     */
    if (contentTypeField == null || contentTypeVersionField == null) {
        return finalSet;
    }

    SolrQuery query = new SolrQuery(contentTypeField + ":[* TO *]");
    query.setFacet(true);
    query.addFacetField(contentTypeField);
    query.addFacetPivotField(contentTypeField + "," + contentTypeVersionField);

    try {
        QueryResponse solrResponse = server.query(query, METHOD.POST);
        List<FacetField> facetFields = solrResponse.getFacetFields();
        for (Entry<String, List<PivotField>> entry : solrResponse.getFacetPivot()) {

            // if no content types have an associated version, the list of pivot fields will be
            // empty.
            // however, the content type names can still be obtained via the facet fields.
            if (CollectionUtils.isEmpty(entry.getValue())) {
                LOGGER.debug("No content type versions found associated with any available content types.");

                if (CollectionUtils.isNotEmpty(facetFields)) {
                    // Only one facet field was added. That facet field may contain multiple
                    // values (content type names).
                    for (FacetField.Count currContentType : facetFields.get(0).getValues()) {
                        // unknown version, so setting it to null
                        ContentTypeImpl contentType = new ContentTypeImpl(currContentType.getName(), null);

                        finalSet.add(contentType);
                    }
                }
            } else {
                for (PivotField pf : entry.getValue()) {

                    String contentTypeName = pf.getValue().toString();
                    LOGGER.debug("contentTypeName:{}", contentTypeName);

                    if (CollectionUtils.isEmpty(pf.getPivot())) {
                        // if there are no sub-pivots, that means that there are no content type
                        // versions
                        // associated with this content type name
                        LOGGER.debug("Content type does not have associated contentTypeVersion: {}",
                                contentTypeName);
                        ContentTypeImpl contentType = new ContentTypeImpl(contentTypeName, null);

                        finalSet.add(contentType);

                    } else {
                        for (PivotField innerPf : pf.getPivot()) {

                            LOGGER.debug("contentTypeVersion:{}. For contentTypeName: {}", innerPf.getValue(),
                                    contentTypeName);

                            ContentTypeImpl contentType = new ContentTypeImpl(contentTypeName,
                                    innerPf.getValue().toString());

                            finalSet.add(contentType);
                        }
                    }
                }
            }
        }

    } catch (SolrServerException e) {
        LOGGER.info("SOLR server exception getting content types", e);
    }

    return finalSet;
}

From source file:ddf.catalog.source.solr.SolrMetacardClientImpl.java

License:Open Source License

@Override
public Set<ContentType> getContentTypes() {
    Set<ContentType> finalSet = new HashSet<>();

    String contentTypeField = resolver.getField(Metacard.CONTENT_TYPE, AttributeType.AttributeFormat.STRING,
            true);//from ww w.  j av  a 2 s  .co  m
    String contentTypeVersionField = resolver.getField(Metacard.CONTENT_TYPE_VERSION,
            AttributeType.AttributeFormat.STRING, true);

    /*
     * If we didn't find the field, it most likely means it does not exist. If it does not
     * exist, then we can safely say that no content types are in this catalog provider
     */
    if (contentTypeField == null || contentTypeVersionField == null) {
        return finalSet;
    }

    SolrQuery query = new SolrQuery(contentTypeField + ":[* TO *]");
    query.setFacet(true);
    query.addFacetField(contentTypeField);
    query.addFacetPivotField(contentTypeField + "," + contentTypeVersionField);

    try {
        QueryResponse solrResponse = client.query(query, SolrRequest.METHOD.POST);
        List<FacetField> facetFields = solrResponse.getFacetFields();
        for (Map.Entry<String, List<PivotField>> entry : solrResponse.getFacetPivot()) {

            // if no content types have an associated version, the list of pivot fields will be
            // empty.
            // however, the content type names can still be obtained via the facet fields.
            if (CollectionUtils.isEmpty(entry.getValue())) {
                LOGGER.debug("No content type versions found associated with any available content types.");

                if (CollectionUtils.isNotEmpty(facetFields)) {
                    // Only one facet field was added. That facet field may contain multiple
                    // values (content type names).
                    for (FacetField.Count currContentType : facetFields.get(0).getValues()) {
                        // unknown version, so setting it to null
                        ContentType contentType = new ContentTypeImpl(currContentType.getName(), null);

                        finalSet.add(contentType);
                    }
                }
            } else {
                for (PivotField pf : entry.getValue()) {

                    String contentTypeName = pf.getValue().toString();
                    LOGGER.debug("contentTypeName: {}", contentTypeName);

                    if (CollectionUtils.isEmpty(pf.getPivot())) {
                        // if there are no sub-pivots, that means that there are no content type
                        // versions
                        // associated with this content type name
                        LOGGER.debug("Content type does not have associated contentTypeVersion: {}",
                                contentTypeName);
                        ContentType contentType = new ContentTypeImpl(contentTypeName, null);

                        finalSet.add(contentType);

                    } else {
                        for (PivotField innerPf : pf.getPivot()) {

                            LOGGER.debug("contentTypeVersion: {}. For contentTypeName: {}", innerPf.getValue(),
                                    contentTypeName);

                            ContentType contentType = new ContentTypeImpl(contentTypeName,
                                    innerPf.getValue().toString());

                            finalSet.add(contentType);
                        }
                    }
                }
            }
        }

    } catch (SolrServerException | IOException e) {
        LOGGER.info("Solr exception getting content types", e);
    }

    return finalSet;
}

From source file:fr.cnes.sitools.metacatalogue.representation.GeoJsonMDEORepresentation.java

License:Open Source License

/**
 * Constructor with a DatabaseRequestParameters, a geometryColName and a converterChained
 * //from   w w w.j av a  2s. com
 * @param authenticatedUser
 *          true if the user is authenticated, false otherwise
 * @param applicationBaseUrl
 *          the current application base url
 * @param thesaurusFacetFields
 *          TODO
 * @param listDocuments
 *          the listDocuments
 * @param searcher
 */
public GeoJsonMDEORepresentation(QueryResponse queryResponse, boolean authenticatedUser,
        String applicationBaseUrl, Map<String, String> conceptsMap, List<String> thesaurusFacetFields) {
    super(MediaType.APPLICATION_JSON);
    this.listDocuments = queryResponse.getResults();
    this.facets = queryResponse.getFacetFields();
    this.pivotFacets = queryResponse.getFacetPivot();
    this.rangesFacets = queryResponse.getFacetRanges();
    this.authenticatedUser = authenticatedUser;
    this.applicationBaseUrl = applicationBaseUrl;
    this.reference = null;
    this.offset = 0;
    this.conceptsMap = conceptsMap;
    this.thesaurusFacetFields = thesaurusFacetFields;

}

From source file:fr.cnes.sitools.metacatalogue.representation.GeoJsonMDEORepresentation.java

License:Open Source License

/**
 * GeoJsonMDEORepresentation// w  ww  . ja  v  a 2 s.com
 * 
 * @param queryResponse
 * @param authenticatedUser
 * @param applicationBaseUrl
 * @param reference
 * @param offset
 * @param startParamName
 * @param limitParamName
 * @param conceptsMap
 * @param thesaurusFacetFields
 */
public GeoJsonMDEORepresentation(QueryResponse queryResponse, boolean authenticatedUser,
        String applicationBaseUrl, Reference reference, long offset, String startParamName,
        String limitParamName, Map<String, String> conceptsMap, List<String> thesaurusFacetFields) {
    super(MediaType.APPLICATION_JSON);
    this.listDocuments = queryResponse.getResults();
    this.facets = queryResponse.getFacetFields();
    this.pivotFacets = queryResponse.getFacetPivot();
    this.rangesFacets = queryResponse.getFacetRanges();
    this.authenticatedUser = authenticatedUser;
    this.applicationBaseUrl = applicationBaseUrl;
    this.reference = reference;
    this.offset = offset;
    this.startParamName = startParamName;
    this.limitParamName = limitParamName;
    this.conceptsMap = conceptsMap;
    this.thesaurusFacetFields = thesaurusFacetFields;
}

From source file:org.alfresco.solr.query.DistributedAlfrescoSolrFacetingTest.java

License:Open Source License

@Test
public void pivotFaceting_mincountMissing_shouldReturnFacetsMincountOne() throws Exception {
    String jsonQuery = "{\"query\":\"(suggest:a)\",\"locales\":[\"en\"], \"templates\": [{\"name\":\"t1\", \"template\":\"%cm:content\"}], \"authorities\": [\"joel\"], \"tenants\": []}";
    putHandleDefaults();//www.j  a v a 2  s.c  o  m

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

    NamedList<List<PivotField>> facetPivot = queryResponse.getFacetPivot();

    List<PivotField> firstLevelValues = facetPivot.getVal(0);
    Assert.assertThat(firstLevelValues.size(), is(2));
    PivotField firstLevelPivot0 = firstLevelValues.get(0);
    Assert.assertThat(firstLevelPivot0.getValue(), is("contenttwo"));
    Assert.assertThat(firstLevelPivot0.getCount(), is(4));

    List<PivotField> firstLevelPivot0Children = firstLevelPivot0.getPivot();
    Assert.assertThat(firstLevelPivot0Children.size(), is(1));
    PivotField secondLevelPivot0 = firstLevelPivot0Children.get(0);
    Assert.assertThat(secondLevelPivot0.getValue(), is("nametwo"));
    Assert.assertThat(secondLevelPivot0.getCount(), is(4));

    PivotField firstLevelPivot1 = firstLevelValues.get(1);
    Assert.assertThat(firstLevelPivot1.getValue(), is("contentone"));
    Assert.assertThat(firstLevelPivot1.getCount(), is(1));

    List<PivotField> firstLevelPivot1Children = firstLevelPivot1.getPivot();
    Assert.assertThat(firstLevelPivot1Children.size(), is(1));
    PivotField secondLevelPivot1 = firstLevelPivot1Children.get(0);
    Assert.assertThat(secondLevelPivot1.getValue(), is("nameone"));
    Assert.assertThat(secondLevelPivot1.getCount(), is(1));
}

From source file:org.alfresco.solr.query.DistributedAlfrescoSolrFacetingTest.java

License:Open Source License

@Test
public void pivotFaceting_mincountSetZero_shouldReturnFacetsMincountOne() throws Exception {
    String jsonQuery = "{\"query\":\"(suggest:a)\",\"locales\":[\"en\"], \"templates\": [{\"name\":\"t1\", \"template\":\"%cm:content\"}], \"authorities\": [\"joel\"], \"tenants\": []}";
    putHandleDefaults();/* w  ww .j ava 2  s . c  o m*/

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

    NamedList<List<PivotField>> facetPivot = queryResponse.getFacetPivot();

    List<PivotField> firstLevelValues = facetPivot.getVal(0);
    Assert.assertThat(firstLevelValues.size(), is(2));
    PivotField firstLevelPivot0 = firstLevelValues.get(0);
    Assert.assertThat(firstLevelPivot0.getValue(), is("contenttwo"));
    Assert.assertThat(firstLevelPivot0.getCount(), is(4));

    List<PivotField> firstLevelPivot0Children = firstLevelPivot0.getPivot();
    Assert.assertThat(firstLevelPivot0Children.size(), is(1));
    PivotField secondLevelPivot0 = firstLevelPivot0Children.get(0);
    Assert.assertThat(secondLevelPivot0.getValue(), is("nametwo"));
    Assert.assertThat(secondLevelPivot0.getCount(), is(4));

    PivotField firstLevelPivot1 = firstLevelValues.get(1);
    Assert.assertThat(firstLevelPivot1.getValue(), is("contentone"));
    Assert.assertThat(firstLevelPivot1.getCount(), is(1));

    List<PivotField> firstLevelPivot1Children = firstLevelPivot1.getPivot();
    Assert.assertThat(firstLevelPivot1Children.size(), is(1));
    PivotField secondLevelPivot1 = firstLevelPivot1Children.get(0);
    Assert.assertThat(secondLevelPivot1.getValue(), is("nameone"));
    Assert.assertThat(secondLevelPivot1.getCount(), is(1));
}

From source file:org.alfresco.solr.query.DistributedAlfrescoSolrFacetingTest.java

License:Open Source License

@Test
public void pivotFaceting_mincountSetTwo_shouldReturnFacetsOriginalMincount() throws Exception {
    String jsonQuery = "{\"query\":\"(suggest:a)\",\"locales\":[\"en\"], \"templates\": [{\"name\":\"t1\", \"template\":\"%cm:content\"}], \"authorities\": [\"joel\"], \"tenants\": []}";
    putHandleDefaults();//from   www . j av  a2  s  .  co  m

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

    NamedList<List<PivotField>> facetPivot = queryResponse.getFacetPivot();

    List<PivotField> firstLevelValues = facetPivot.getVal(0);
    Assert.assertThat(firstLevelValues.size(), is(1));
    PivotField firstLevelPivot0 = firstLevelValues.get(0);
    Assert.assertThat(firstLevelPivot0.getValue(), is("contenttwo"));
    Assert.assertThat(firstLevelPivot0.getCount(), is(4));

    List<PivotField> firstLevelPivot0Children = firstLevelPivot0.getPivot();
    Assert.assertThat(firstLevelPivot0Children.size(), is(1));
    PivotField secondLevelPivot0 = firstLevelPivot0Children.get(0);
    Assert.assertThat(secondLevelPivot0.getValue(), is("nametwo"));
    Assert.assertThat(secondLevelPivot0.getCount(), is(4));
}