Example usage for org.apache.solr.client.solrj.response PivotField getPivot

List of usage examples for org.apache.solr.client.solrj.response PivotField getPivot

Introduction

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

Prototype

public List<PivotField> getPivot() 

Source Link

Usage

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

License:Open Source License

/**
 * Read nested pivot results./*from   ww w. j a va2s .  c  o m*/
 *
 * @param pfl
 * @return
 */
private List<FacetPivotResultDTO> getFacetPivotResults(List<PivotField> pfl) {
    if (pfl == null || pfl.size() == 0) {
        return null;
    }

    List<FacetPivotResultDTO> list = new ArrayList<FacetPivotResultDTO>();
    for (PivotField pf : pfl) {
        String value = pf.getValue() != null ? pf.getValue().toString() : null;
        if (pf.getPivot() == null || pf.getPivot().size() == 0) {
            list.add(new FacetPivotResultDTO(null, null, value, pf.getCount()));
        } else {
            list.add(new FacetPivotResultDTO(pf.getPivot().get(0).getField(),
                    getFacetPivotResults(pf.getPivot()), value, pf.getCount()));
        }
    }

    return list;
}

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

License:Apache License

private static List<FacetPivotFieldEntry> convertPivotResult(List<PivotField> pivotResult) {
    if (CollectionUtils.isEmpty(pivotResult)) {
        return Collections.emptyList();
    }//from  w  w  w. j  a  va 2  s  .  c o  m

    ArrayList<FacetPivotFieldEntry> pivotFieldEntries = new ArrayList<FacetPivotFieldEntry>();

    for (PivotField pivotField : pivotResult) {
        SimpleFacetPivotEntry pivotFieldEntry = new SimpleFacetPivotEntry(
                new SimpleField(pivotField.getField()), String.valueOf(pivotField.getValue()),
                pivotField.getCount());

        List<PivotField> pivot = pivotField.getPivot();
        if (pivot != null) {
            pivotFieldEntry.setPivot(convertPivotResult(pivot));
        }

        pivotFieldEntries.add(pivotFieldEntry);
    }

    return pivotFieldEntries;
}

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

License:Open Source License

private int populateFacetPivot(DataTable aTable, String[] aFacetNames, int aRowId, int aParentId,
        PivotField aPivotField) {
    Object facetValue;/*  w w  w.jav  a 2  s. c  om*/
    FieldRow fieldRow;
    String fieldName, facetName, facetNameCount;
    Logger appLogger = mAppMgr.getLogger(this, "populateFacetPivot");

    appLogger.trace(mAppMgr.LOGMSG_TRACE_ENTER);

    if (aPivotField != null) {
        fieldName = aPivotField.getField();
        facetValue = aPivotField.getValue();
        if (facetValue == null)
            facetNameCount = String.format("Unassigned (%s)", aPivotField.getCount());
        else {
            facetName = aPivotField.getValue().toString();
            facetNameCount = String.format("%s (%s)", facetName, aPivotField.getCount());
        }

        ArrayList<String> facetValues = null;
        if (StringUtils.equals(fieldName, aFacetNames[0]))
            aParentId = 0;
        else {
            int rowCount = aTable.rowCount();
            if (rowCount > 0) {
                fieldRow = aTable.getRow(rowCount - 1);
                facetValues = aTable.getValuesByName(fieldRow, fieldName);
                if (facetValues != null) {
                    if (facetValues.size() == 0)
                        facetValues = null;
                    else
                        facetValues.add(facetNameCount);
                }
            }
        }
        if (facetValues == null) {
            fieldRow = aTable.newRow();
            aTable.setValueByName(fieldRow, "id", aRowId + 1);
            aTable.setValueByName(fieldRow, "parent_id", aParentId);
            facetValues = aTable.getValuesByName(fieldRow, fieldName);
            if (facetValues != null) {
                facetValues.add(facetNameCount);
                if (facetValues.size() == 1) {
                    aTable.addRow(fieldRow);
                    aRowId = aTable.rowCount();
                    aParentId = aRowId;
                }
            }
        }

        if (aPivotField.getPivot() != null) {
            for (PivotField pivotField : aPivotField.getPivot())
                aRowId = populateFacetPivot(aTable, aFacetNames, aRowId, aParentId, pivotField);
        }
    }

    appLogger.trace(mAppMgr.LOGMSG_TRACE_DEPART);

    return aRowId;
}

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);/* w w w  .  ja va 2s  .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);//  ww  w .  ja v  a2s  .com
    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

private void writeFacetPivots(JsonGenerator jGenerator, List<PivotField> pivotFields)
        throws JsonGenerationException, IOException {
    if (pivotFields == null) {
        return;/*from   ww  w . ja va2  s . co  m*/
    }
    jGenerator.writeStartArray();
    for (PivotField field : pivotFields) {
        jGenerator.writeStartObject();
        jGenerator.writeStringField("field", field.getField());
        jGenerator.writeStringField("value", getValue(field.getField(), field.getValue().toString()));
        jGenerator.writeNumberField("count", field.getCount());
        if (field.getPivot() != null) {
            jGenerator.writeFieldName("pivot");
            writeFacetPivots(jGenerator, field.getPivot());
        }
        jGenerator.writeEndObject();
    }
    jGenerator.writeEndArray();
}

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();//ww w  . jav 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();//from  w  w w.j  av a2 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  ww  w.j a va 2s  .  c om*/

    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));
}

From source file:org.apache.metron.solr.dao.SolrSearchDao.java

License:Apache License

protected List<GroupResult> getGroupResults(GroupRequest groupRequest, int index,
        List<PivotField> pivotFields) {
    List<Group> groups = groupRequest.getGroups();
    List<GroupResult> searchResultGroups = new ArrayList<>();
    final GroupOrder groupOrder = groups.get(index).getOrder();
    pivotFields.sort((o1, o2) -> {/*from   w  w w .  ja  v a  2  s. co m*/
        String s1 = groupOrder.getGroupOrderType() == GroupOrderType.TERM ? o1.getValue().toString()
                : Integer.toString(o1.getCount());
        String s2 = groupOrder.getGroupOrderType() == GroupOrderType.TERM ? o2.getValue().toString()
                : Integer.toString(o2.getCount());
        if (groupOrder.getSortOrder() == SortOrder.ASC) {
            return s1.compareTo(s2);
        } else {
            return s2.compareTo(s1);
        }
    });

    for (PivotField pivotField : pivotFields) {
        GroupResult groupResult = new GroupResult();
        groupResult.setKey(pivotField.getValue().toString());
        groupResult.setTotal(pivotField.getCount());
        Optional<String> scoreField = groupRequest.getScoreField();
        if (scoreField.isPresent()) {
            groupResult.setScore((Double) pivotField.getFieldStatsInfo().get(scoreField.get()).getSum());
        }
        if (index < groups.size() - 1) {
            groupResult.setGroupedBy(groups.get(index + 1).getField());
            groupResult.setGroupResults(getGroupResults(groupRequest, index + 1, pivotField.getPivot()));
        }
        searchResultGroups.add(groupResult);
    }
    return searchResultGroups;
}