List of usage examples for org.apache.solr.common.params FacetParams FACET_FIELD
String FACET_FIELD
To view the source code for org.apache.solr.common.params FacetParams FACET_FIELD.
Click Source Link
From source file:at.newmedialab.lmf.util.solr.suggestion.service.SuggestionService.java
License:Apache License
private SolrQueryResponse query(String query, String df, String[] fields, String[] fqs) { SolrQueryResponse rsp = new SolrQueryResponse(); //append *// w ww . j a v a 2s. c o m if (!query.endsWith("*")) { query = query.trim() + "*"; } //Prepare query ModifiableSolrParams params = new ModifiableSolrParams(); SolrQueryRequest req = new LocalSolrQueryRequest(solrCore, params); params.add(CommonParams.Q, query.toLowerCase()); params.add(CommonParams.DF, df); params.add("q.op", "AND"); params.add(FacetParams.FACET, "true"); params.add(FacetParams.FACET_LIMIT, internalFacetLimit); params.add(FacetParams.FACET_MINCOUNT, "1"); for (String field : fields) { params.add(FacetParams.FACET_FIELD, field); } if (fqs != null) { for (String fq : fqs) { params.add(CommonParams.FQ, fq); } } if (spellcheck_enabled) { params.add("spellcheck", "true"); params.add("spellcheck.collate", "true"); } try { //execute query and return searchHandler.handleRequestBody(req, rsp); return rsp; } catch (SolrException se) { throw se; } catch (Exception e) { e.printStackTrace(); throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "internal server error"); } finally { req.close(); } }
From source file:at.pagu.soldockr.core.QueryParserTest.java
License:Apache License
private void assertFactingNotPresent(SolrQuery solrQuery) { Assert.assertNull(solrQuery.get(FacetParams.FACET_FIELD)); }
From source file:com.tsgrp.solr.handler.NYPhilSearchHandler.java
License:Mozilla Public License
/** * @see org.apache.solr.handler.component.SearchHandler#handleRequestBody(org.apache.solr.request.SolrQueryRequest, * org.apache.solr.request.SolrQueryResponse) *//*from w w w . j a v a2s.co m*/ @Override public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception, ParseException, InstantiationException, IllegalAccessException { SolrParams requestParams = req.getParams(); NamedList<Object> params = requestParams.toNamedList(); //for now lets echo the handler and all the params for debugging purposes //params.add( CommonParams.HEADER_ECHO_HANDLER, Boolean.TRUE ); //params.add( CommonParams.HEADER_ECHO_PARAMS, CommonParams.EchoParamStyle.ALL ); String rows = (String) params.get(CommonParams.ROWS); if (rows == null || rows.trim().length() < 1) { //setup items per page, default to 10 items params.add(CommonParams.ROWS, 10); rows = "10"; } //enable faceting and only return facets with at least 1 item params.add(FacetParams.FACET, Boolean.TRUE); params.add(FacetParams.FACET_MINCOUNT, 1); //defult ordering to index order (alphabetical) params.add(FacetParams.FACET_SORT, FacetParams.FACET_SORT_INDEX); //only return a maximum of 10 facet values params.add(FacetParams.FACET_LIMIT, 10); //always add facets unless they are explicitly not requested String addFacets = (String) params.get(PARAM_GENERATE_FACETS); boolean generateFacets = (addFacets == null) ? true : addFacets.equalsIgnoreCase("true"); //enable highlighting params.add(HighlightParams.HIGHLIGHT, Boolean.TRUE); //remove the query if provided, always want to use our translated query String originalQuery = (String) params.remove(CommonParams.Q); if (logger.isDebugEnabled()) { logger.debug("Original query: " + originalQuery); } //setup sorting params String sortColumn = (String) params.get(PARAM_SORT_COLUMN); String sortOrder = (String) params.get(PARAM_SORT_ORDER); if (sortColumn != null && sortOrder != null) { params.add(CommonParams.SORT, sortColumn + " " + sortOrder); } //get date fields String sDateFrom = (String) params.get(PARAM_DATE_FROM); String sDateTo = (String) params.get(PARAM_DATE_TO); Date dateFrom = null; Date dateTo = null; if (sDateFrom != null && sDateTo != null) { dateFrom = DATE_FORMAT_PARAM.parse(sDateFrom); dateTo = DATE_FORMAT_PARAM.parse(sDateTo); } String keywords = (String) params.get(PARAM_KEYWORDS); if (keywords == null || keywords.trim().length() < 1) { //query everything since nothing was provided keywords = "*"; } //always use the extended dismax parser params.add("defType", ExtendedDismaxQParserPlugin.NAME); //the keywords sent in are the query since we're in DISMAX mode params.add(CommonParams.Q, keywords); String pageIndex = (String) params.get(PARAM_PAGE_INDEX); String resultsPerPage = (String) params.get(PARAM_RESULTS_PER_PAGE); if (resultsPerPage == null || resultsPerPage.trim().length() < 0) { resultsPerPage = "10"; } if (pageIndex == null || pageIndex.trim().length() < 1) { pageIndex = "1"; } //figure out the skip count, use the (pageIndex - 1) * resultsPerPage //ie pageIndex = 3, 10 results per page, we'll set start to (3-1)*10 = 20 int start = (Integer.parseInt(pageIndex) - 1) * Integer.parseInt(resultsPerPage); params.add(CommonParams.START, start); String doctype = (String) params.get(PARAM_DOCTYPE); if (doctype == null || doctype.trim().length() < 1) { doctype = ""; } String facetQuery = (String) params.get(PARAM_FACET_QUERY); if (facetQuery != null && facetQuery.trim().length() > 0) { //facetQuery is pre formatted and correct, just add it as a filter query params.add(CommonParams.FQ, facetQuery); } String suggestedQuery = (String) params.get(PARAM_SUGGESTED_QUERY); if (suggestedQuery != null && suggestedQuery.trim().length() > 0) { //suggestedQuery is pre formatted and correct, just add it as a filter query params.add(CommonParams.FQ, suggestedQuery); } //base fields params.add(DisMaxParams.QF, "nyp:DocumentType"); params.add(DisMaxParams.QF, "nyp:Notes"); //program fields params.add(DisMaxParams.QF, "npp:ProgramID"); params.add(DisMaxParams.QF, "npp:Season"); params.add(DisMaxParams.QF, "npp:OrchestraCode"); params.add(DisMaxParams.QF, "npp:OrchestraName"); params.add(DisMaxParams.QF, "npp:LocationName"); params.add(DisMaxParams.QF, "npp:VenueName"); params.add(DisMaxParams.QF, "npp:EventTypeName"); params.add(DisMaxParams.QF, "npp:SubEventName"); params.add(DisMaxParams.QF, "npp:ConductorName"); params.add(DisMaxParams.QF, "npp:SoloistsNames"); params.add(DisMaxParams.QF, "npp:SoloistsInstrumentName"); params.add(DisMaxParams.QF, "npp:WorksComposerNames"); params.add(DisMaxParams.QF, "npp:WorksTitle"); params.add(DisMaxParams.QF, "npp:WorksShortTitle"); params.add(DisMaxParams.QF, "npp:WorksConductorNames"); //printedMusic fields params.add(DisMaxParams.QF, "npm:LibraryID"); params.add(DisMaxParams.QF, "npm:ShortTitle"); params.add(DisMaxParams.QF, "npm:ComposerName"); params.add(DisMaxParams.QF, "npm:PublisherName"); params.add(DisMaxParams.QF, "npm:ComposerNameTitle"); params.add(DisMaxParams.QF, "npm:ScoreMarkingArtist"); params.add(DisMaxParams.QF, "npm:ScoreEditionTypeDesc"); params.add(DisMaxParams.QF, "npm:ScoreNotes"); //part fields params.add(DisMaxParams.QF, "npm:PartTypeDesc"); params.add(DisMaxParams.QF, "npm:PartMarkingArtist"); params.add(DisMaxParams.QF, "npm:UsedByArtistName"); //businessRecord fields params.add(DisMaxParams.QF, "npb:BoxNumber"); params.add(DisMaxParams.QF, "npb:RecordGroup"); params.add(DisMaxParams.QF, "npb:Series"); params.add(DisMaxParams.QF, "npb:SubSeries"); params.add(DisMaxParams.QF, "npb:Folder"); params.add(DisMaxParams.QF, "npb:Names"); params.add(DisMaxParams.QF, "npb:Subject"); params.add(DisMaxParams.QF, "npb:Abstract"); //visual fields params.add(DisMaxParams.QF, "npv:ID"); params.add(DisMaxParams.QF, "npv:BoxNumber"); params.add(DisMaxParams.QF, "npv:PhilharmonicSource"); params.add(DisMaxParams.QF, "npv:OutsideSource"); params.add(DisMaxParams.QF, "npv:Photographer"); params.add(DisMaxParams.QF, "npv:CopyrightHolder"); params.add(DisMaxParams.QF, "npv:PlaceOfImage"); params.add(DisMaxParams.QF, "npv:PersonalNames"); params.add(DisMaxParams.QF, "npv:CorporateNames"); params.add(DisMaxParams.QF, "npv:Event"); params.add(DisMaxParams.QF, "npv:ImageType"); params.add(DisMaxParams.QF, "npv:LocationName"); params.add(DisMaxParams.QF, "npv:VenueName"); //audio fields params.add(DisMaxParams.QF, "npa:ProgramID"); params.add(DisMaxParams.QF, "npa:Location"); params.add(DisMaxParams.QF, "npa:EventTypeName"); params.add(DisMaxParams.QF, "npa:ConductorName"); params.add(DisMaxParams.QF, "npa:SoloistsAndInstruments"); params.add(DisMaxParams.QF, "npa:ComposerWork"); params.add(DisMaxParams.QF, "npa:OrchestraName"); params.add(DisMaxParams.QF, "npa:IntermissionFeature"); params.add(DisMaxParams.QF, "npa:LocationName"); params.add(DisMaxParams.QF, "npa:VenueName"); params.add(DisMaxParams.QF, "npa:SubEventName"); params.add(DisMaxParams.QF, "npa:URLLocation"); params.add(DisMaxParams.QF, "npa:IntermissionGuests"); params.add(DisMaxParams.QF, "npa:Announcer"); //video fields params.add(DisMaxParams.QF, "npx:ProgramID"); params.add(DisMaxParams.QF, "npx:Location"); params.add(DisMaxParams.QF, "npx:EventTypeName"); params.add(DisMaxParams.QF, "npx:ConductorName"); params.add(DisMaxParams.QF, "npx:SoloistsAndInstruments"); params.add(DisMaxParams.QF, "npx:ComposerNameWork"); params.add(DisMaxParams.QF, "npx:OrchestraName"); params.add(DisMaxParams.QF, "npx:IntermissionFeature"); params.add(DisMaxParams.QF, "npx:LocationName"); params.add(DisMaxParams.QF, "npx:VenueName"); params.add(DisMaxParams.QF, "npx:SubEventName"); params.add(DisMaxParams.QF, "npx:IntermissionGuests"); params.add(DisMaxParams.QF, "npx:Announcer"); params.add(DisMaxParams.QF, "npt:tagged"); //add in all the restrictions that can be applied to a document type count in the same filter query //this includes all DATE range queries //if a date query does not exist, the TYPE is queried so all results are shown //restriction is applied to business records so only the web publishable items are shown StringBuffer filterTypesCountsQuery = new StringBuffer(); if (dateFrom != null && dateTo != null) { //program date query filterTypesCountsQuery.append("(npp\\:Date:[" + solrField.toExternal(dateFrom) + " TO " + solrField.toExternal(dateTo) + "])"); //printedMusic (scores) and parts have no date range, so just or in the type so those results come back filterTypesCountsQuery.append(" OR (nyp\\:DocumentType:Printed Music)"); filterTypesCountsQuery.append(" OR (nyp\\:DocumentType:Part)"); //business record range AND web publishable restriction filterTypesCountsQuery .append(" OR (nyp\\:WebPublishable:true AND ((npb\\:DateFrom:[" + solrField.toExternal(dateFrom) + " TO *] AND npb\\:DateTo:[* TO " + solrField.toExternal(dateTo) + "])"); filterTypesCountsQuery.append(" OR (npb\\:DateFrom:[* TO " + solrField.toExternal(dateFrom) + "] AND npb\\:DateTo:[* TO " + solrField.toExternal(dateTo) + "] AND npb\\:DateTo:[" + solrField.toExternal(dateFrom) + " TO *])"); filterTypesCountsQuery.append(" OR (npb\\:DateFrom:[" + solrField.toExternal(dateFrom) + " TO *] AND npb\\:DateTo:[" + solrField.toExternal(dateTo) + " TO *] AND npb\\:DateFrom:[* TO " + solrField.toExternal(dateFrom) + "])))"); //visual filterTypesCountsQuery.append(" OR ((npv\\:DateFrom:[" + solrField.toExternal(dateFrom) + " TO *] AND npv\\:DateTo:[* TO " + solrField.toExternal(dateTo) + "])"); filterTypesCountsQuery.append(" OR (npv\\:DateFrom:[* TO " + solrField.toExternal(dateFrom) + "] AND npv\\:DateTo:[* TO " + solrField.toExternal(dateTo) + "] AND npv\\:DateTo:[" + solrField.toExternal(dateFrom) + " TO *])"); filterTypesCountsQuery.append(" OR (npv\\:DateFrom:[" + solrField.toExternal(dateFrom) + " TO *] AND npv\\:DateTo:[" + solrField.toExternal(dateTo) + " TO *] AND npv\\:DateFrom:[* TO " + solrField.toExternal(dateFrom) + "]))"); //audio filterTypesCountsQuery.append(" OR (npa\\:Date:[" + solrField.toExternal(dateFrom) + " TO " + solrField.toExternal(dateTo) + "])"); //video filterTypesCountsQuery.append(" OR (npx\\:Date:[" + solrField.toExternal(dateFrom) + " TO " + solrField.toExternal(dateTo) + "])"); } else { //no date queries, just perform query based on type restrictions filterTypesCountsQuery.append( "(nyp\\:DocumentType:Program) OR (nyp\\:DocumentType:Printed Music) OR (nyp\\:DocumentType:Part) OR (nyp\\:DocumentType:Business Record AND nyp\\:WebPublishable:true)"); filterTypesCountsQuery.append( " OR (nyp\\:DocumentType:Visual) OR (nyp\\:DocumentType:Audio) OR (nyp\\:DocumentType:Video)"); } params.add(CommonParams.FQ, filterTypesCountsQuery); //default facet for document type that will always return the counts regardless of filter queries applied params.add(FacetParams.FACET_FIELD, "{!ex=test}nyp:DocumentType_facet"); //allow this facet to always display all values, even when there are 0 results for the type params.add("f.nyp:DocumentType_facet.facet.mincount", 0); if (doctype.equalsIgnoreCase("program")) { //set the document type restriction params.add(CommonParams.FQ, "{!tag=test}nyp\\:DocumentType:Program"); if (generateFacets) { params.add(FacetParams.FACET_FIELD, "npp:ConductorName_facet"); params.add(FacetParams.FACET_FIELD, "npp:SoloistsNames_facet"); params.add(FacetParams.FACET_FIELD, "npp:WorksComposerNames_facet"); params.add(FacetParams.FACET_FIELD, "npp:LocationName_facet"); params.add(FacetParams.FACET_FIELD, "npp:VenueName_facet"); params.add(FacetParams.FACET_FIELD, "npp:EventTypeName_facet"); params.add(FacetParams.FACET_FIELD, "npp:Season_facet"); } } else if (doctype.equalsIgnoreCase("printedMusic")) { //set the document type restriction params.add(CommonParams.FQ, "{!tag=test}nyp\\:DocumentType:Printed Music"); if (generateFacets) { params.add(FacetParams.FACET_FIELD, "npm:ScoreMarkingArtist_facet"); params.add(FacetParams.FACET_FIELD, "npm:ComposerName_facet"); } } else if (doctype.equalsIgnoreCase("part")) { //set the document type restriction params.add(CommonParams.FQ, "{!tag=test}nyp\\:DocumentType:Part"); if (generateFacets) { params.add(FacetParams.FACET_FIELD, "npm:ComposerName_facet"); params.add(FacetParams.FACET_FIELD, "npm:UsedByArtistName_facet"); params.add(FacetParams.FACET_FIELD, "npm:PartMarkingArtist_facet"); params.add(FacetParams.FACET_FIELD, "npm:PartTypeDesc_facet"); } // if sort parameter has been supplied (e.g. non-facet search) - apply additional part sorting if (sortColumn != null && sortOrder != null) { logger.debug("Addition additional sort parameter for PART type..."); String prevParams = (String) params.get(CommonParams.SORT); String newParams = prevParams + ", npm:PartID ASC"; params.add(CommonParams.SORT, newParams); } } else if (doctype.equalsIgnoreCase("businessRecord")) { //set the document type restriction params.add(CommonParams.FQ, "{!tag=test}nyp\\:DocumentType:Business Record"); if (generateFacets) { params.add(FacetParams.FACET_FIELD, "npb:Names_facet"); params.add(FacetParams.FACET_FIELD, "npb:Subject_facet"); params.add(FacetParams.FACET_FIELD, "npb:RecordGroup_facet"); params.add(FacetParams.FACET_FIELD, "npb:Series_facet"); params.add(FacetParams.FACET_FIELD, "npb:SubSeries_facet"); } } else if (doctype.equalsIgnoreCase("visual")) { //set the document type restriction params.add(CommonParams.FQ, "{!tag=test}nyp\\:DocumentType:Visual"); if (generateFacets) { params.add(FacetParams.FACET_FIELD, "npv:Photographer_facet"); params.add(FacetParams.FACET_FIELD, "npv:CopyrightHolder_facet"); params.add(FacetParams.FACET_FIELD, "npv:ImageType_facet"); params.add(FacetParams.FACET_FIELD, "npv:PlaceOfImage_facet"); params.add(FacetParams.FACET_FIELD, "npv:Event_facet"); params.add(FacetParams.FACET_FIELD, "npv:PersonalNames_facet"); params.add(FacetParams.FACET_FIELD, "npv:LocationName_facet"); params.add(FacetParams.FACET_FIELD, "npv:VenueName_facet"); } } else if (doctype.equalsIgnoreCase("audio")) { //set the document type restriction params.add(CommonParams.FQ, "{!tag=test}nyp\\:DocumentType:Audio"); //no facets being generated } else if (doctype.equalsIgnoreCase("video")) { //set the document type restriction params.add(CommonParams.FQ, "{!tag=test}nyp\\:DocumentType:Video"); //no facets being generated } else { logger.error("Invalid document type: " + doctype); throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid document type: " + doctype); } if (logger.isDebugEnabled()) { logger.debug("Params: " + params); } //set the new request parameters, then call the default handler behavior req.setParams(SolrParams.toSolrParams(params)); super.handleRequestBody(req, rsp); }
From source file:com.tsgrp.solr.handler.NYPhilTagAutoCompleteHandler.java
License:Mozilla Public License
@Override public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse res) throws Exception, ParseException, InstantiationException, IllegalAccessException { NamedList<Object> params = req.getParams().toNamedList(); params.add(CommonParams.ROWS, 0);//from ww w. j a v a2 s .c o m params.add(FacetParams.FACET, true); params.add(FacetParams.FACET_FIELD, NYPhilSolrConstants.NPT_CONTENT_FACET); params.add(FacetParams.FACET_MINCOUNT, 1); params.add(FacetParams.FACET_SORT, FacetParams.FACET_SORT_INDEX); params.add(CommonParams.HEADER_ECHO_PARAMS, "explicit"); params.add(CommonParams.WT, "json"); params.add("json.nl", "map"); String query = (String) params.get(PARAM_VALUE); if (query == null || query.length() == 0) { query = "*"; } else { query = QueryParser.escape(query.toLowerCase()); } String[] queryTerms = query.split(" "); // wrap our query term in parentheses to allow searching on terms separated by whitespace StringBuffer q = new StringBuffer(); // for each query term, require the term with a trailing wildcard match for (String queryTerm : queryTerms) { // remove all non-alphanumeric chars from the query term queryTerm = QUERY_TERM_REGEX.matcher(queryTerm.toLowerCase()).replaceAll(""); q.append("+").append(NYPhilSolrConstants.NPT_CONTENT_ESC).append(":") .append(QueryParser.escape(queryTerm)).append("* "); } q.append("+").append(NYPhilSolrConstants.NPT_STATUS_ESC).append(":") .append(NYPhilSolrConstants.STATUS_APPROVED); params.add(CommonParams.Q, q.toString()); if (logger.isDebugEnabled()) { logger.debug("Autocomplete Query: " + q.toString()); } String cb = (String) params.get(PARAM_CALLBACK); if (cb != null && cb.length() > 0) { params.add("json.wrf", cb); } req.setParams(SolrParams.toSolrParams(params)); super.handleRequestBody(req, res); }
From source file:de.uni_tuebingen.ub.ixTheo.handler.component.FacetPrefixSortComponent.java
License:Apache License
/** * Actually run the query/*from ww w .j a v a2 s . c o m*/ */ @Override public void process(ResponseBuilder rb) throws IOException { if (rb.doFacets) { final ModifiableSolrParams params = new ModifiableSolrParams(); final SolrParams origParams = rb.req.getParams(); final Iterator<String> iter = origParams.getParameterNamesIterator(); setCollator(origParams.get("lang")); while (iter.hasNext()) { final String paramName = iter.next(); // Deduplicate the list with LinkedHashSet, but _only_ for facet // params. if (!paramName.startsWith(FacetParams.FACET)) { params.add(paramName, origParams.getParams(paramName)); continue; } final HashSet<String> deDupe = new LinkedHashSet<>(Arrays.asList(origParams.getParams(paramName))); params.add(paramName, deDupe.toArray(new String[deDupe.size()])); } final SimplePrefixSortFacets facets = new SimplePrefixSortFacets(rb.req, rb.getResults().docSet, params, rb); final NamedList<Object> counts = org.apache.solr.handler.component.FacetComponent .getFacetCounts(facets); final String[] pivots = params.getParams(FacetParams.FACET_PIVOT); if (pivots != null && pivots.length > 0) { PivotFacetProcessor pivotProcessor = new PivotFacetProcessor(rb.req, rb.getResults().docSet, params, rb); SimpleOrderedMap<List<NamedList<Object>>> v = pivotProcessor.process(pivots); if (v != null) { counts.add(PIVOT_KEY, v); } } // Check whether we have to reorder out results // according to prefix final String sort = params.get(FacetParams.FACET_SORT); if (FacetPrefixSortParams.FACET_SORT_PREFIX.equals(sort)) { // Determine a score relative to the original query // Determine the query and make it compatible with our metric // class // by splitting the single terms String[] queryTerms = params.getParams(CommonParams.Q); final Collection<String> queryTermsCollection = new ArrayList<>(); for (String s : queryTerms) { // Split at whitespace except we have a quoted term Matcher matcher = WHITE_SPACES_WITH_QUOTES_SPLITTING_PATTERN.matcher(s); while (matcher.find()) { queryTermsCollection.add(matcher.group().replaceAll("^\"|\"$", "")); } } // In some contexts, i.e. in KWC that are derived from ordinary // keywords or if // wildcards occur, also add all the query terms as a single // phrase term // with stripped wildcards StringBuilder sb = new StringBuilder(); for (String s : queryTermsCollection) { s = s.replace("*", ""); sb.append(s); sb.append(" "); } queryTermsCollection.add(sb.toString().trim()); final ArrayList<String> queryList = new ArrayList<>(queryTermsCollection); final String facetfield = params.get(FacetParams.FACET_FIELD); // Get the current facet entry and make it compatible with our // metric class // "facet_fields" itself contains a NamedList with the // facet.field as key final NamedList<Object> facetFieldsNamedList = (NamedList<Object>) counts.get("facet_fields"); final NamedList<Object> facetFields = (NamedList<Object>) facetFieldsNamedList.get(facetfield); final List<Entry<Entry<String, Object>, Double>> facetPrefixListScored = new ArrayList<>(); for (final Entry<String, Object> entry : facetFields) { final String facetTerms = entry.getKey(); // Split up each KWC and calculate the scoring ArrayList<String> facetList = new ArrayList<>( Arrays.asList(facetTerms.split("(?<!" + Pattern.quote("\\") + ")/"))); // For usability reasons sort the result facets according to // the order of the search facetList = KeywordSort.sortToReferenceChain(queryList, facetList); final double score = KeywordChainMetric.calculateSimilarityScore(queryList, facetList); // Collect the result in a sorted list and throw away // garbage if (score > 0) { String facetTermsSorted = StringUtils.join(facetList, "/"); Map.Entry<String, Object> sortedEntry = new AbstractMap.SimpleEntry<>(facetTermsSorted, entry.getValue()); facetPrefixListScored.add(new AbstractMap.SimpleEntry<>(sortedEntry, score)); } } Collections.sort(facetPrefixListScored, ENTRY_COMPARATOR); // Extract all the values wrap it back to NamedList again and // replace in the original structure facetFieldsNamedList.clear(); NamedList<Object> facetNamedListSorted = new NamedList<>(); // We had to disable all limits and offsets sort according // Handle this accordingly now int offset = (params.getInt(FacetParams.FACET_OFFSET) != null) ? params.getInt(FacetParams.FACET_OFFSET) : 0; int limit = (params.getInt(FacetParams.FACET_LIMIT) != null) ? params.getInt(FacetParams.FACET_LIMIT) : 100; // Strip uneeded elements int s = facetPrefixListScored.size(); int off = (offset < s) ? offset : 0; limit = (limit < 0) ? s : limit; // Handle a negative limit // param, i.e. unlimited results int lim = (offset + limit <= s) ? (offset + limit) : s; final List<Entry<Entry<String, Object>, Double>> facetPrefixListScoredTruncated = facetPrefixListScored .subList(off, lim); for (Entry<Entry<String, Object>, Double> e : facetPrefixListScoredTruncated) { facetNamedListSorted.add(e.getKey().getKey(), e.getKey().getValue()); } facetFieldsNamedList.add(facetfield, facetNamedListSorted); NamedList<Object> countList = new NamedList<>(); countList.add("count", facetPrefixListScored.size()); facetFieldsNamedList.add(facetfield + "-count", countList); counts.remove("facet_fields"); counts.add("facet_fields", facetFieldsNamedList); } rb.rsp.add("facet_counts", counts); } }
From source file:org.alfresco.solr.SolrInformationServer.java
License:Open Source License
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override//from w ww .j av a2 s.c o m
public void addFTSStatusCounts(NamedList<Object> report) {
try (SolrQueryRequest request = newSolrQueryRequest()) {
ModifiableSolrParams params = new ModifiableSolrParams(request.getParams()).set(CommonParams.Q, "*:*")
.set(CommonParams.ROWS, 0).set(FacetParams.FACET, true)
.set(FacetParams.FACET_FIELD, FIELD_FTSSTATUS);
SolrQueryResponse response = cloud.getResponse(nativeRequestHandler, request, params);
NamedList facetCounts = (NamedList) response.getValues().get("facet_counts");
NamedList facetFields = (NamedList) facetCounts.get("facet_fields");
NamedList<Integer> ftsStatusCounts = (NamedList) facetFields.get(FIELD_FTSSTATUS);
long cleanCount = this.getSafeCount(ftsStatusCounts, FTSStatus.Clean.toString());
report.add("Node count with FTSStatus Clean", cleanCount);
long dirtyCount = this.getSafeCount(ftsStatusCounts, FTSStatus.Dirty.toString());
report.add("Node count with FTSStatus Dirty", dirtyCount);
long newCount = this.getSafeCount(ftsStatusCounts, FTSStatus.New.toString());
report.add("Node count with FTSStatus New", newCount);
}
}
From source file:org.alfresco.solr.SolrInformationServer.java
License:Open Source License
@SuppressWarnings({ "unchecked", "rawtypes" })
private NamedList<Integer> getFacets(SolrQueryRequest request, String query, String field, int minCount) {
ModifiableSolrParams params = new ModifiableSolrParams(request.getParams()).set(CommonParams.Q, query)
.set(CommonParams.ROWS, 0).set(FacetParams.FACET, true).set(FacetParams.FACET_FIELD, field)
.set(FacetParams.FACET_MINCOUNT, minCount);
SolrQueryResponse response = cloud.getResponse(nativeRequestHandler, request, params);
NamedList facetCounts = (NamedList) response.getValues().get("facet_counts");
NamedList facetFields = (NamedList) facetCounts.get("facet_fields");
return (NamedList) facetFields.get(field);
}
From source file:org.apache.jackrabbit.core.query.lucene.FacetHandler.java
License:Open Source License
private void extractFacetParameters(String facetFunctionPrefix, NamedList<Object> parameters, int counter, Map.Entry<String, PropertyValue> column) throws RepositoryException { // first extract options from rep:facet() from column key final String key = column.getKey(); final String facetOptions = key.substring(key.indexOf(facetFunctionPrefix) + facetFunctionPrefix.length(), key.lastIndexOf(")")); // remember nodetype and query values if encountered so that we can process them once the whole facet is parsed String nodeType = null;//from w w w .ja v a 2 s .com List<String> unparsedQueries = null; // loop invariants final String columnPropertyName = column.getValue().getPropertyName(); final String propertyName = columnPropertyName + SimpleJahiaJcrFacets.PROPNAME_INDEX_SEPARATOR + counter; // we can assert the facet type by checking whether the the options String contains date or range, otherwise, the type is field final boolean isQuery = facetOptions.contains(FacetParams.FACET_QUERY); String facetType = FacetParams.FACET_FIELD; // default facet type if (isQuery) { facetType = FacetParams.FACET_QUERY; } else if (facetOptions.contains("date")) { facetType = FacetParams.FACET_DATE; } else if (facetOptions.contains("range")) { facetType = FacetParams.FACET_RANGE; } parameters.add(facetType, propertyName); // populate parameters // each parameter name/value pair is separated from the next one by & so split on this final String[] paramPairs = StringUtils.split(facetOptions, "&"); for (String paramPair : paramPairs) { // for each pair, extract the name and value separated by = int separator = paramPair.indexOf('='); if (separator >= 0) { // todo: what should we do if a pair doesn't have an equal sign in it? final String paramName = paramPair.substring(0, separator); final String paramValue = paramPair.substring(separator + 1); // some parameters need to be specially processed and not be added as others so process them and exit current iteration when encountered if (paramName.equals("nodetype")) { nodeType = paramValue; // remember node type value for later processing continue; } else if (paramName.contains("query")) { if (unparsedQueries == null) { unparsedQueries = new LinkedList<String>(); } unparsedQueries.add(paramValue); // remember query value for later processing continue; } // create full parameter name and add its value to the parameters String facetOption = getFacetOption(paramName); parameters.add(getFullParameterName(propertyName, facetOption), paramValue); } } // node type parameter if (StringUtils.isEmpty(nodeType)) { // if we didn't have a node type specified in the given options, extract it from the selector name and create the associated parameter nodeType = getNodeTypeFromSelector(column.getValue().getSelectorName(), columnPropertyName); } // only add node type parameter if we're not dealing with a query if (!isQuery) { parameters.add(getFullParameterName(propertyName, getFacetOption("nodetype")), nodeType); } // deal with embedded query if needed, at this point, nodeType will have been either extracted or asserted from selector name if (unparsedQueries != null) { ExtendedPropertyDefinition epd = NodeTypeRegistry.getInstance().getNodeType(nodeType) .getPropertyDefinition(columnPropertyName); for (String unparsedQuery : unparsedQueries) { if (unparsedQuery.split("(?<!\\\\):").length == 1 && !columnPropertyName.equals("rep:facet()")) { if (epd != null) { String fieldNameInIndex = getFieldNameInIndex(propertyName, epd, ""); unparsedQuery = QueryParser.escape(fieldNameInIndex) + ":" + unparsedQuery; } } parameters.add(getFullParameterName(propertyName, "query"), unparsedQuery); } } }
From source file:org.dspace.app.xmlui.aspect.discovery.json.JSONSolrSearcher.java
License:BSD License
@Override public void setup(SourceResolver resolver, Map objectModel, String src, Parameters par) throws ProcessingException, SAXException, IOException { //Retrieve all the given parameters Request request = ObjectModelHelper.getRequest(objectModel); this.response = ObjectModelHelper.getResponse(objectModel); query = request.getParameter(CommonParams.Q); if (query == null) { query = "*:*"; }// w ww . j av a 2 s. c o m //Retrieve all our filter queries filterQueries = request.getParameterValues(CommonParams.FQ); //Retrieve our facet fields facetFields = request.getParameterValues(FacetParams.FACET_FIELD); //Retrieve our facet limit (if any) if (request.getParameter(FacetParams.FACET_LIMIT) != null) { try { facetLimit = Integer.parseInt(request.getParameter(FacetParams.FACET_LIMIT)); } catch (Exception e) { //Should an invalid value be supplied use -1 facetLimit = -1; } } else { facetLimit = -1; } //Retrieve our sorting value facetSort = request.getParameter(FacetParams.FACET_SORT); //Make sure we have a valid sorting value if (!FacetParams.FACET_SORT_INDEX.equals(facetSort) && !FacetParams.FACET_SORT_COUNT.equals(facetSort)) { facetSort = null; } //Retrieve our facet min count facetMinCount = 1; try { facetMinCount = Integer.parseInt(request.getParameter(FacetParams.FACET_MINCOUNT)); } catch (Exception e) { facetMinCount = 1; } jsonWrf = request.getParameter("json.wrf"); //Retrieve our discovery solr path ExtendedProperties props = null; //Method that will retrieve all the possible configs we have props = ExtendedProperties.convertProperties(ConfigurationManager.getProperties()); InputStream is = null; try { File config = new File(props.getProperty("dspace.dir") + "/config/dspace-solr-search.cfg"); if (config.exists()) { props.combine(new ExtendedProperties(config.getAbsolutePath())); } else { is = SolrServiceImpl.class.getResourceAsStream("dspace-solr-search.cfg"); ExtendedProperties defaults = new ExtendedProperties(); defaults.load(is); props.combine(defaults); } } catch (Exception e) { log.error("Error while retrieving solr url", e); e.printStackTrace(); } finally { if (is != null) { is.close(); } } if (props.getProperty("solr.search.server") != null) { this.solrServerUrl = props.getProperty("solr.search.server").toString(); } }
From source file:org.dspace.app.xmlui.aspect.discovery.json.JSONSolrSearcher.java
License:BSD License
public void generate() throws IOException, SAXException, ProcessingException { if (solrServerUrl == null) { return;/*from w ww . j a v a2s . c o m*/ } Map<String, String> params = new HashMap<String, String>(); String solrRequestUrl = solrServerUrl + "/select"; //Add our default parameters params.put(CommonParams.ROWS, "0"); params.put(CommonParams.WT, "json"); //We uwe json as out output type params.put("json.nl", "map"); params.put("json.wrf", jsonWrf); params.put(FacetParams.FACET, Boolean.TRUE.toString()); //Generate our json out of the given params try { params.put(CommonParams.Q, URLEncoder.encode(query, Constants.DEFAULT_ENCODING)); } catch (UnsupportedEncodingException uee) { //Should never occur return; } params.put(FacetParams.FACET_LIMIT, String.valueOf(facetLimit)); if (facetSort != null) { params.put(FacetParams.FACET_SORT, facetSort); } params.put(FacetParams.FACET_MINCOUNT, String.valueOf(facetMinCount)); solrRequestUrl = AbstractDSpaceTransformer.generateURL(solrRequestUrl, params); if (facetFields != null || filterQueries != null) { StringBuilder urlBuilder = new StringBuilder(solrRequestUrl); if (facetFields != null) { //Add our facet fields for (String facetField : facetFields) { urlBuilder.append("&").append(FacetParams.FACET_FIELD).append("="); //This class can only be used for autocomplete facet fields if (!facetField.endsWith(".year") && !facetField.endsWith("_ac")) { urlBuilder.append(URLEncoder.encode(facetField + "_ac", Constants.DEFAULT_ENCODING)); } else { urlBuilder.append(URLEncoder.encode(facetField, Constants.DEFAULT_ENCODING)); } } } if (filterQueries != null) { for (String filterQuery : filterQueries) { urlBuilder.append("&").append(CommonParams.FQ).append("=") .append(URLEncoder.encode(filterQuery, Constants.DEFAULT_ENCODING)); } } solrRequestUrl = urlBuilder.toString(); } try { GetMethod get = new GetMethod(solrRequestUrl); new HttpClient().executeMethod(get); String result = get.getResponseBodyAsString(); if (result != null) { ByteArrayInputStream inputStream = new ByteArrayInputStream(result.getBytes("UTF-8")); byte[] buffer = new byte[8192]; response.setHeader("Content-Length", String.valueOf(result.length())); int length; while ((length = inputStream.read(buffer)) > -1) { out.write(buffer, 0, length); } out.flush(); } } catch (Exception e) { log.error("Error while getting json solr result for discovery search recommendation", e); e.printStackTrace(); } }