/** * Construct a {@link FacetTreeBuilder} from a set of local parameters. * @param params the local parameters parsed from a * <code>facet.tree.field</code> parameter. * @return an appropriate {@link FacetTreeBuilder}, or <code>null</code> * if none could be found. * @throws SyntaxError if required parameters are missing for the derived * {@link FacetTreeBuilder}, or the strategy is unrecognised. */ public FacetTreeBuilder constructFacetTreeBuilder(SolrParams params) throws SyntaxError { if (params == null) { throw new SyntaxError("No local parameters supplied."); } String strategy = deriveStrategyFromLocalParams(params); if (StringUtils.isBlank(strategy)) { throw new SyntaxError("Unable to derive strategy from parameters"); } FacetTreeBuilder ftb = constructFacetTreeBuilderFromStrategy(strategy); if (ftb == null) { throw new SyntaxError("Unrecognised strategy for facet tree: " + strategy); } // Initialise the FacetTreeBuilder parameters ftb.initialiseParameters(params); return ftb; }
/** * From a list of alternate queries in text format, parse them using the default * ExtendedSolrQueryParser and return the queries. * * @param solrParams * @param alternateQueryTexts * @return */ private List<Query> createSynonymQueries(SolrParams solrParams, List<String> alternateQueryTexts) { String nullsafeOriginalString = getQueryStringFromParser(); List<Query> result = new ArrayList<>(); for (String alternateQueryText : alternateQueryTexts) { if (alternateQueryText.equalsIgnoreCase(nullsafeOriginalString)) { // alternate query is the same as what the user entered continue; } synonymQueryParser.setString(alternateQueryText); try { result.add(synonymQueryParser.parse()); } catch (SyntaxError e) { // TODO: better error handling - for now just bail out; ignore this synonym e.printStackTrace(System.err); } } return result; }
@Override public void initialiseParameters(SolrParams localParams) throws SyntaxError { getLogger().trace("Initialising parameters..."); if (localParams == null) { throw new SyntaxError("Missing facet tree parameters"); } // Initialise the node field - REQUIRED nodeField = localParams.get(FacetTreeParameters.NODE_FIELD_PARAM); if (StringUtils.isBlank(nodeField)) { // Not specified in localParams - use the key value instead nodeField = localParams.get(QueryParsing.V); // If still blank, we have a problem if (StringUtils.isBlank(nodeField)) { throw new SyntaxError("No node field defined in " + localParams); } } // Initialise the optional fields labelField = localParams.get(FacetTreeParameters.LABEL_FIELD_PARAM, null); }
/** * Parses the fq param and adds it as a list of filter queries or reverts to null if nothing is found * or an Exception is thrown. * * @param req * @return either a query from the QueryParser or null */ private List<Query> getFilterQueries(SolrQueryRequest req) { List<Query> filters = null; String[] fqs = req.getParams().getParams("fq"); if (fqs != null && fqs.length != 0) { filters = new ArrayList<>(fqs.length); try { for (String fq : fqs) { if (fq != null && fq.trim().length() != 0) { QParser fqp = QParser.getParser(fq, req); fqp.setIsFilter(true); filters.add(fqp.getQuery()); } } } catch (SyntaxError e) { e.printStackTrace(); } if (filters.isEmpty()) { filters = null; } } return filters; }
@Override public Query parse() throws SyntaxError { try { return new SparqlQuery(QueryFactory.create(qstr, Syntax.syntaxARQ), isHybrid(req)); } catch (final Exception exception) { throw new SyntaxError(exception); } } }
@Override public Query parse() throws SyntaxError { if (qstr == null || qstr.length()==0) return null; String defaultField = getParam(CommonParams.DF); if (defaultField==null) { defaultField = getReq().getSchema().getDefaultSearchFieldName(); } xmlParser = new XmlQueryParser(defaultField, new StandardAnalyzer(req.getCore().getSolrConfig().luceneMatchVersion)); try { return xmlParser.parse(new ByteArrayInputStream(qstr.getBytes(Charset.forName("utf-8")))); } catch (ParserException e) { throw new SyntaxError (e); } }
@Override public void initialiseParameters(SolrParams localParams) throws SyntaxError { super.initialiseParameters(localParams); // Initialise the child field - REQUIRED childField = localParams.get(FacetTreeParameters.CHILD_FIELD_PARAM); if (StringUtils.isBlank(childField)) { throw new SyntaxError("Missing child field definition in " + localParams); } // Initialise the optional fields maxLevels = localParams.getInt(FacetTreeParameters.LEVELS_PARAM, 0); docFields.addAll(Arrays.asList(getNodeField(), childField)); if (hasLabelField()) { docFields.add(getLabelField()); } }
@Override public void initialiseParameters(SolrParams localParams) throws SyntaxError { // Initialise the common fields super.initialiseParameters(localParams); // Initialise the parent field - REQUIRED parentField = localParams.get(FacetTreeParameters.PARENT_FIELD_PARAM); if (StringUtils.isBlank(parentField)) { throw new SyntaxError("Missing parent field definition in " + localParams); } // Initialise the optional fields maxLevels = localParams.getInt(FacetTreeParameters.LEVELS_PARAM, 0); docFields.addAll(Arrays.asList(getNodeField(), parentField)); if (hasLabelField()) { docFields.add(getLabelField()); } }
/** * Uses {@link SolrPluginUtils#parseFieldBoosts(String)} with the 'qf' * parameter. Falls back to the 'df' parameter or * {@link org.apache.solr.schema.IndexSchema#getDefaultSearchFieldName()}. */ public static Map<String, Float> parseQueryFields(final IndexSchema indexSchema, final SolrParams solrParams) throws SyntaxError { final Map<String, Float> queryFields = SolrPluginUtils.parseFieldBoosts(solrParams.getParams(SirenParams.QF)); if (queryFields.isEmpty()) { final String df = QueryParsing.getDefaultField(indexSchema, solrParams.get(CommonParams.DF)); if (df == null) { throw new SyntaxError("Neither "+SirenParams.QF+", "+CommonParams.DF +", nor the default search field are present."); } queryFields.put(df, 1.0f); } checkFieldTypes(indexSchema, queryFields); return queryFields; }
public Pruner constructPruner(SolrParams params) throws SyntaxError { Pruner pruner = null; String prunerParam = params.get(PRUNE_PARAM, componentParameters.getDefault(PRUNE_PARAM)); if (StringUtils.isNotBlank(prunerParam)) { if (SIMPLE_PRUNER_VALUE.equals(prunerParam)) { pruner = new SimplePruner(params.getInt(SimplePruner.CHILD_COUNT_PARAM, SimplePruner.MIN_CHILD_COUNT)); } else if (DATAPOINTS_PRUNER_VALUE.equals(prunerParam)) { int dp = params.getInt(DATAPOINTS_PARAM, componentParameters.getIntDefault(DATAPOINTS_PARAM)); if (dp <= 0) { throw new SyntaxError("Datapoints parameter invalid"); } pruner = new DatapointPruner(dp, componentParameters.getDefault(FacetTreeParameters.DATAPOINTS_MORELABEL_PARAM, DatapointPruner.DEFAULT_MORE_LABEL)); } } return pruner; }
@Override public Query parse() throws SyntaxError { try { luceneQueries = controller.process(); if (luceneQueries.querqyBoostQueries != null && luceneQueries.querqyBoostQueries.size() > 0) { final BooleanQuery.Builder builder = new BooleanQuery.Builder(); for (final Query q : luceneQueries.querqyBoostQueries) { builder.add(q, BooleanClause.Occur.SHOULD); } processedQuery = new QuerqyReRankQuery(maybeWrapQuery(luceneQueries.mainQuery), maybeWrapQuery(builder.build()), requestAdapter.getReRankNumDocs(), 1.0); } else { processedQuery = maybeWrapQuery(luceneQueries.mainQuery); } } catch (final LuceneSearchEngineRequestAdapter.SyntaxException e) { throw new SyntaxError("Syntax error", e); } return processedQuery; }