writer.name(facetField.getName()); writer.beginArray(); for (final FacetField.Count count : facetField.getValues()) { writer.beginObject(); writer.name("facet").value(count.getName());
SearchFacetDTO facetDTO = namedFacetMap.get(facetFieldName); for (Count value : facet.getValues()) { SearchFacetResultDTO resultDTO = new SearchFacetResultDTO(); resultDTO.setFacet(facetDTO.getFacet());
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; }
public ComplexResultIterator ( SolrDocumentList results, List<FacetField> facets ) { this(results); this.facetFields = new HashMap<String, List<FacetField.Count>>(); for ( FacetField f : facets ) { this.facetFields.put(f.getName(), f.getValues()); } }
private void setFacetFieldCounts(FacetField facetField, Map<String, Long> countMap){ if ((facetField ==null)||(countMap==null)){ return; } for (FacetField.Count fcnt : facetField.getValues()){ countMap.put(fcnt.getName().toLowerCase().replace(" ", "_") + "_count", fcnt.getCount()); } }
@Override public Object transform(final Object fieldName) { FacetField facetResult = m_solrResultList == null ? null : m_solrResultList.getFacetField(fieldName.toString()); I_CmsSearchControllerFacetField facetController = m_controller.getFieldFacets().getFieldFacetController().get( fieldName.toString()); List<String> result = new ArrayList<String>(); if (null != facetController) { List<String> checkedEntries = facetController.getState().getCheckedEntries(); if (null != facetResult) { List<String> returnedValues = new ArrayList<String>(facetResult.getValues().size()); for (FacetField.Count value : facetResult.getValues()) { returnedValues.add(value.getName()); } for (String checked : checkedEntries) { if (!returnedValues.contains(checked)) { result.add(checked); } } } else { result = checkedEntries; } } return result; } });
private void setFacets(List<FacetField> facetFields) { if(facetFields == null) { return; } DefaultListModel model = new DefaultListModel(); int i = 0; for(FacetField facet:facetFields) { for(Count count:facet.getValues()) { model.add(i++, facet.getName() + ":" + count.getName() + "(" + count.getCount() + ")"); } } jListFacets.setModel(model); }
public Set<String> getOntologyNames() throws SolrServerException { if (ontologyNames == null) { SolrQuery query = new SolrQuery("*:*"); query.setStart(0); query.setRows(0); query.setFacet(true); query.setParam(FacetParams.FACET_FIELD, "ontology"); QueryResponse response = search(query); FacetField facetField = response.getFacetField("ontology"); if (facetField.getValues() == null) { return Collections.EMPTY_SET; } ontologyNames = new HashSet<String>( facetField.getValues().size() ); for (FacetField.Count c : facetField.getValues()) { ontologyNames.add(c.getName()); } } return ontologyNames; }
private static SearchFacetField convertToSearchFacetField( FacetField facetField) { return new BaseSearchFacetField(facetField.getName(), convertToSearchFacetFieldCounts(facetField.getValues())); }
private void filterFacet(SolrDocument doc, FacetField facetField) { // facet filtering by value requires that the facet values match the stored values // a *_facet field must exist, stored (or /w docValues) to be used for faceting and at filtering time if (doc.getFieldNames().contains(facetField.getName())) { // decrease facet value Collection<Object> docFieldValues = doc.getFieldValues(facetField.getName()); if (docFieldValues != null) { for (Object docFieldValue : docFieldValues) { String valueString = String.valueOf(docFieldValue); List<FacetField.Count> toRemove = new LinkedList<FacetField.Count>(); for (FacetField.Count count : facetField.getValues()) { long existingCount = count.getCount(); if (valueString.equals(count.getName())) { if (existingCount > 1) { // decrease the count count.setCount(existingCount - 1); } else { // remove the entire entry toRemove.add(count); } } } for (FacetField.Count f : toRemove) { assert facetField.getValues().remove(f); } } } } }
@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); } }
public <T> List<T> getObjects() { Map<String, FacetField.Count> index = new HashMap<String, FacetField.Count>(); List<String> ids = new ArrayList<String>(); for (FacetField.Count c : this.field.getValues()) { index.put(c.getName(), c); ids.add(c.getName()); } @SuppressWarnings("unchecked") List<T> objects = (List<T>) (this.klass == null || this.klass == Query.class ? Query.fromAll().where("id = ?", ids).selectAll() : Query.from(this.klass).where("id = ?", ids).selectAll()); if (objects != null) { for (Object o : objects) { Record record = (Record) o; FacetField.Count c = index.get(record.getId().toString()); record.getState().getExtras().put("count", Long.valueOf(c.getCount())); } } return objects; } }
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())); }
@Override public List<String> createTypeNames(HttpSolrClient solrServer) throws Exception { List<String> names = new ArrayList<>(); SolrQuery query = new SolrQuery(); query.setQuery("*:*"); query.addFacetField(field); query.setFacet(true); query.setFacetMinCount(1); query.setFacetSort(FacetParams.FACET_SORT_INDEX); query.setRows(0); query.setParam("omitHeader", true); QueryResponse rsp = solrServer.query(query); if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log(Level.FINE, "SOLR query done: " + query.toString()); } List<Count> uniqueFacetFields = rsp.getFacetFields().get(0).getValues(); for (Count field : uniqueFacetFields) { names.add(field.getName()); } return names; }
private static List<FacetFieldResult> processFacetFields(Facets facets, List<FacetField> facetFields) { List<FacetFieldResult> ret = new ArrayList<>(); FacetFieldResult facetResult; if (facetFields != null) { for (FacetField facet : facetFields) { LOGGER.trace("facet:{} count:{}", facet.getName(), facet.getValueCount()); facetResult = new FacetFieldResult(facet.getName(), facet.getValueCount(), facets.getParameters().get(facet.getName()).getValues()); for (Count count : facet.getValues()) { LOGGER.trace(" value:{} value:{}", count.getName(), count.getCount()); facetResult.addFacetValue(count.getName(), count.getName(), count.getCount()); } ret.add(facetResult); } } return ret; }
public static void main() { String url = "http://localhost:8983/solr/autocomplete"; SolrServer solrServer = new HttpSolrServer(url); SolrQuery query = new SolrQuery(); query.set("q", "*"); query.addFilterQuery("name:*"); query.setFacet(true); query.addFacetField("name"); System.out.println(query); QueryResponse queryResponse = solrServer.query(query); List<FacetField> facetFields = queryResponse.getFacetFields(); FacetField cnameMainFacetField = queryResponse.getFacetField("name"); for (Count cnameAndCount : cnameMainFacetField.getValues()) { String cnameMain = cnameAndCount.getName(); System.out.println(cnameMain); System.out.println(cnameAndCount.getCount()); }
/** * Retrieves all the metadata fields which are indexed in the authority control * * @return a list of metadata fields * @throws Exception if error */ @Override public List<String> getAllIndexedMetadataFields() throws Exception { SolrQuery solrQuery = new SolrQuery(); solrQuery.setQuery("*:*"); solrQuery.setFacet(true); solrQuery.addFacetField("field"); QueryResponse response = getSolr().query(solrQuery); List<String> results = new ArrayList<String>(); FacetField facetField = response.getFacetField("field"); if (facetField != null) { List<FacetField.Count> values = facetField.getValues(); if (values != null) { for (FacetField.Count facetValue : values) { if (facetValue != null && facetValue.getName() != null) { results.add(facetValue.getName()); } } } } return results; } }
public static <T extends IsIndexed> List<String> suggest(SolrClient index, Class<T> classToRetrieve, String field, String queryString, boolean justActive, User user, boolean allowPartial) throws GenericException { StringBuilder queryBuilder = new StringBuilder(); appendKeyValue(queryBuilder, field + RodaConstants.INDEX_SEARCH_SUFFIX, queryString + "*"); SolrQuery query = new SolrQuery(); query.setParam("q.op", DEFAULT_QUERY_PARSER_OPERATOR); query.setQuery(queryBuilder.toString()); if (hasPermissionFilters(classToRetrieve)) { query.addFilterQuery(getFilterQueries(user, justActive, classToRetrieve)); } parseAndConfigureFacets(new Facets(new SimpleFacetParameter(field)), query); List<String> suggestions = new ArrayList<>(); try { QueryResponse response = index.query(SolrCollectionRegistry.getIndexName(classToRetrieve), query); response.getFacetField(field).getValues().forEach(count -> suggestions.add(count.getName())); } catch (SolrServerException | IOException | SolrException | NotSupportedException e) { throw new GenericException("Could not get suggestions", e); } catch (RuntimeException e) { throw new GenericException("Unexpected exception while querying index", e); } return suggestions; }
private static void facetFieldToXML(FacetField facet, XMLWriter xml) throws IOException { xml.openElement("facet"); xml.attribute("name", facet.getName()); xml.attribute("count", facet.getValueCount()); for (Count count : facet.getValues()) { xml.openElement("term"); xml.attribute("field", facet.getName()); xml.attribute("text", count.getName()); xml.attribute("cardinality", String.valueOf(count.getCount())); xml.closeElement(); } xml.closeElement(); } }
/** * Map facet field. * * @param facetField the facet field * @return the field facet result */ private static FieldFacetResult mapFacetField(final FacetField facetField) { final String fieldName = getReverseFacetFieldName(facetField.getName()); final FieldFacetResult result = new FieldFacetResult(fieldName); if (facetField.getValueCount() > 0) { for (final FacetField.Count count : facetField.getValues()) { if (count.getName() == null) { // missing count result.setMissingCount(count.getCount()); } else { result.addValue(count.getName(), count.getCount()); } } } return result; }