/** * {@inheritDoc} * * @see org.apache.solr.search.QParser#parse() */ @Override public Query parse() throws ParseException { SolrQueryParser solrParser = new SolrQueryParser(this, DEFAULT_FIELD); solrParser.setAllowLeadingWildcard(true); return solrParser.parse(super.qstr); }
public String[] getDefaultHighlightFields() { return new String[]{lparser.getField()}; }
/** * A SolrQueryParser linked to this IndexSchema for field datatype * information, and populated with default options from the * <solrQueryParser> configuration for this IndexSchema. * * @param defaultField if non-null overrides the schema default * @deprecated */ public SolrQueryParser getSolrQueryParser(String defaultField) { SolrQueryParser qp = new SolrQueryParser(this,defaultField); String operator = getQueryParserDefaultOperator(); qp.setDefaultOperator("AND".equals(operator) ? QueryParser.Operator.AND : QueryParser.Operator.OR); return qp; }
public SolrQueryParser(QParser parser, String defaultField, Analyzer analyzer) { super(defaultField, analyzer); this.schema = parser.getReq().getSchema(); this.parser = parser; this.defaultField = defaultField; setLowercaseExpandedTerms(false); setEnablePositionIncrements(true); checkAllowLeadingWildcards(); }
@Override public Query parse() throws ParseException { String defaultField = getParam(CommonParams.DF); if (defaultField == null) { // TODO poussin 20101216 normalement on devrait retrouver cette // valeur dans la config. Mais en fait a chaque fois on passe // ici car defaultField est null :( alors que dans schema.xml // on a bien defini le defaultField :( defaultField = WikittySolrConstant.SOLR_DEFAULT_FIELD; } lparser = new SolrQueryParser(this, defaultField); lparser.setAllowLeadingWildcard(true); String opParam = getParam(QueryParsing.OP); if (opParam != null) { lparser.setDefaultOperator("AND".equals(opParam) ? QueryParser.Operator.AND : QueryParser.Operator.OR); } String qstr = getString(); if (log.isDebugEnabled()) { log.debug("Query parse : " + qstr); } return lparser.parse(qstr); }
public Query parse() throws ParseException { String qstr = getString(); String defaultField = getParam(CommonParams.DF); if (defaultField==null) { defaultField = getReq().getSchema().getDefaultSearchFieldName(); } lparser = new SolrQueryParser(this, defaultField); // these could either be checked & set here, or in the SolrQueryParser constructor String opParam = getParam(QueryParsing.OP); if (opParam != null) { lparser.setDefaultOperator("AND".equals(opParam) ? QueryParser.Operator.AND : QueryParser.Operator.OR); } else { // try to get default operator from schema QueryParser.Operator operator = getReq().getSchema().getSolrQueryParser(null).getDefaultOperator(); lparser.setDefaultOperator(null == operator ? QueryParser.Operator.OR : operator); } return lparser.parse(qstr); }
/** * Helper utility for parsing a query using the Lucene QueryParser syntax. * @param qs query expression in standard Lucene syntax * @param defaultField default field used for unqualified search terms in the query expression * @param params used to determine the default operator, overriding the schema specified operator * @param schema used for default operator (overridden by params) and passed to the query parser for field format analysis information */ public static Query parseQuery(String qs, String defaultField, SolrParams params, IndexSchema schema) { try { SolrQueryParser parser = schema.getSolrQueryParser(defaultField); String opParam = params.get(OP); if (opParam != null) { parser.setDefaultOperator("AND".equals(opParam) ? QueryParser.Operator.AND : QueryParser.Operator.OR); } Query query = parser.parse(qs); if (SolrCore.log.isTraceEnabled() ) { SolrCore.log.trace("After QueryParser:" + query); } return query; } catch (ParseException e) { SolrCore.log(e); throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,"Query parsing error: " + e.getMessage(),e); } }
protected void checkAllowLeadingWildcards() { boolean allow = false; for (Entry<String, FieldType> e : schema.getFieldTypes().entrySet()) { Analyzer a = e.getValue().getAnalyzer(); if (a instanceof TokenizerChain) { // examine the indexing analysis chain if it supports leading wildcards TokenizerChain tc = (TokenizerChain)a; TokenFilterFactory[] factories = tc.getTokenFilterFactories(); for (TokenFilterFactory factory : factories) { if (factory instanceof ReversedWildcardFilterFactory) { allow = true; leadingWildcards.put(e.getKey(), (ReversedWildcardFilterFactory)factory); } } } } // XXX should be enabled on a per-field basis if (allow) { setAllowLeadingWildcard(true); } }
/** * Helper utility for parsing a query using the Lucene QueryParser syntax. * @param qs query expression in standard Lucene syntax * @param defaultField default field used for unqualified search terms in the query expression * @param schema used for default operator (overridden by params) and passed to the query parser for field format analysis information */ public static Query parseQuery(String qs, String defaultField, IndexSchema schema) { try { Query query = schema.getSolrQueryParser(defaultField).parse(qs); if (SolrCore.log.isTraceEnabled() ) { SolrCore.log.trace("After QueryParser:" + query); } return query; } catch (ParseException e) { SolrCore.log(e); throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,"Error parsing Lucene query",e); } }
@Override protected Query getFieldQuery(String field, String queryText, int slop) throws SyntaxError { if (field.equals("xpath")) { // post-filter with Saxon return new XpathFilterQuery(queryText); } else if (field.equals("xpath_index")) { // pre-filter with xpath index return getLuceneQuery(queryText); } else { // pass-through any non-XPath related fields return super.getFieldQuery(field, queryText, slop); } }
protected Query getPrefixQuery(String field, String termStr) throws ParseException { checkNullField(field); if (getLowercaseExpandedTerms()) { termStr = termStr.toLowerCase(); } // TODO: toInternal() won't necessarily work on partial // values, so it looks like we need a getPrefix() function // on fieldtype? Or at the minimum, a method on fieldType // that can tell me if I should lowercase or not... // Schema could tell if lowercase filter is in the chain, // but a more sure way would be to run something through // the first time and check if it got lowercased. // TODO: throw exception if field type doesn't support prefixes? // (sortable numeric types don't do prefixes, but can do range queries) Term t = new Term(field, termStr); PrefixQuery prefixQuery = new PrefixQuery(t); return prefixQuery; }
protected Query getRangeQuery(String field, String part1, String part2, boolean inclusive) throws ParseException { checkNullField(field); SchemaField sf = schema.getField(field); return sf.getType().getRangeQuery(parser, sf, "*".equals(part1) ? null : part1, "*".equals(part2) ? null : part2, inclusive, inclusive); }
/** * Constructs a SolrQueryParser using the schema to understand the * formats and datatypes of each field. Only the defaultSearchField * will be used from the IndexSchema (unless overridden), * <solrQueryParser> will not be used. * * @param schema Used for default search field name if defaultField is null and field information is used for analysis * @param defaultField default field used for unspecified search terms. if null, the schema default field is used * @see IndexSchema#getDefaultSearchFieldName() */ public SolrQueryParser(IndexSchema schema, String defaultField) { super(defaultField == null ? schema.getDefaultSearchFieldName() : defaultField, schema.getQueryAnalyzer()); this.schema = schema; this.parser = null; this.defaultField = defaultField; setLowercaseExpandedTerms(false); setEnablePositionIncrements(true); checkAllowLeadingWildcards(); }
return super.getFieldQuery(field, queryText); } catch (Exception e) { return null;
protected Query getFieldQuery(String field, String queryText) throws ParseException { checkNullField(field); // intercept magic field name of "_" to use as a hook for our // own functions. if (field.charAt(0) == '_') { if ("_val_".equals(field)) { if (parser==null) { return QueryParsing.parseFunction(queryText, schema); } else { QParser nested = parser.subQuery(queryText, "func"); return nested.getQuery(); } } else if ("_query_".equals(field) && parser != null) { return parser.subQuery(queryText, null).getQuery(); } } // default to a normal field query return super.getFieldQuery(field, queryText); }
@Override public String[] getDefaultHighlightFields() { return new String[]{lparser.getField()}; } }