for (final FacetField.Count count : facetField.getValues()) { writer.beginObject(); writer.name("facet").value(count.getName()); writer.name("count").value(count.getCount()); writer.endObject();
resultDTO.setFacet(facetDTO.getFacet()); resultDTO.setQuantity(new Long(value.getCount()).intValue()); resultDTO.setValue(value.getName()); facetDTO.getFacetValues().add(resultDTO);
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; }
@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; } });
@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; }
/** * 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; }
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; }
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 void collectFacetCountsFor(Collection<FieldCount> facetFieldCounts, List<FacetField.Count> facetCounts, String searchFieldName, String[] prefixes) { if (facetCounts != null && !facetCounts.isEmpty()){ for (FacetField.Count count : facetCounts){ if (prefixes == null){ FieldCount fieldCount = new FieldCount(count.getName(), count.getCount(), searchFieldName); facetFieldCounts.add(fieldCount); } else { for (String prefix : prefixes){ if (prefix != null && count.getName().startsWith(prefix+":")){ FieldCount fieldCount = new FieldCount(count.getName(), count.getCount(), searchFieldName); facetFieldCounts.add(fieldCount); break; } } } } } }
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 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; }
private SearchResultsBean searchTermsInternal(String searchQuery, String facetField, Map<String, String> attributes, UserRegistry registry) throws IndexerException, RegistryException { SearchResultsBean resultsBean = new SearchResultsBean(); SolrClient client = SolrClient.getInstance(); //authenticate required attribute is not used, since we are going to authorize each time and not depends on this flag. attributes.remove(IndexingConstants.AUTH_REQUIRED); List<FacetField.Count> results = (searchQuery == null) ? client.facetQuery(registry.getTenantId(), attributes) : client.facetQuery(searchQuery, facetField, registry.getTenantId()); if (log.isDebugEnabled()) { log.debug("result for the term search: " + results); } List<TermData> termDataList = new ArrayList<>(); for (FacetField.Count count : results) { termDataList.add(new TermData(count.getName(), count.getCount())); } resultsBean.setTermDataList(termDataList.toArray(new TermData[termDataList.size()])); return resultsBean; }
@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); } }
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(); } }
private static List<BaseCount> convertToSearchFacetFieldCounts( List<FacetField.Count> solrCounts) { List<BaseCount> searchCounts = new ArrayList<>(); if (solrCounts != null) { for (FacetField.Count solrCount : solrCounts) { searchCounts.add(new BaseCount(solrCount.getName(), solrCount .getCount())); } } return searchCounts; }
private void collectFacetCountsFor(Collection<FieldCount> facetFieldCounts, List<FacetField.Count> facetCounts, String searchFieldName, String prefix) { if (facetCounts != null && !facetCounts.isEmpty()){ for (FacetField.Count count : facetCounts){ if (prefix == null || (prefix != null && count.getName().startsWith(prefix+":"))){ FieldCount fieldCount = new FieldCount(count.getName(), count.getCount(), searchFieldName); facetFieldCounts.add(fieldCount); } } } }
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); }
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()); } }
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())); }
private void collectFacetCountsFor(Collection<FieldCount> facetFieldCounts, List<FacetField.Count> facetCounts, String searchFieldName) { if (facetCounts != null && !facetCounts.isEmpty()){ for (FacetField.Count count : facetCounts){ FieldCount fieldCount = new FieldCount(count.getName(), count.getCount(), searchFieldName); facetFieldCounts.add(fieldCount); } } }