List of usage examples for org.apache.lucene.queries.function.valuesource LiteralValueSource LiteralValueSource
public LiteralValueSource(String string)
From source file:alba.solr.core.DynamicValueSourceParser.java
License:Apache License
@SuppressWarnings("unchecked") public ValueSource parse(FunctionQParser fp) throws SyntaxError { String functionName;// w w w . j a v a 2 s . co m List<ValueSource> valueSourceList = new ArrayList<ValueSource>(); functions = (Map<String, CallableFunction>) fp.getReq().getContext().get(Loader.FUNCTIONS); Map<String, ValueSource> values = new HashMap<String, ValueSource>(); int i = 0; while (fp.hasMoreArguments()) { rawargs[i++] = fp.parseArg(); } functionName = rawargs[0]; CallableFunction function = functions.get(functionName); //still need this? // FunctionExecutionContext cachedEC = (FunctionExecutionContext)fp.getReq().getContext().get(fp.getString() ); /* if (cachedEC != null) { logger.error("reusing executor from cache!"); return cachedEC.getFunctionExecutor(); } */ for (int k = 1; k < i; k++) { String parts[] = rawargs[k].split("="); String name = parts[0]; String value = parts[1]; args.put(parts[0], parts[1]); if (value.startsWith("\"") && value.endsWith("\"")) { //probably quite ineffcient.. String v = value.replaceAll("^\"", "").replaceAll("\"$", ""); LiteralValueSource l = new LiteralValueSource(v); values.put(name, l); valueSourceList.add(l); } else if (NumberUtils.isNumber(value)) { ConstValueSource cvs = new ConstValueSource(Float.parseFloat(value)); values.put(name, cvs); valueSourceList.add(cvs); } else { SchemaField f = fp.getReq().getSchema().getField(value); ValueSource vs = f.getType().getValueSource(f, fp); values.put(name, vs); valueSourceList.add(vs); } } FunctionExecutor executor = new FunctionExecutor(values, valueSourceList, fp, this); executor.setFunction(functions.get(functionName)); // still need this???? FunctionExecutionContext ec = new FunctionExecutionContext(fp.getString(), values, function, executor); fp.getReq().getContext().put(fp.getString(), ec); return executor; }
From source file:alba.solr.docvalues.FunctionExecutor.java
License:Apache License
public FunctionExecutor(Map<String, ValueSource> values, List<ValueSource> sourceList, FunctionQParser fp, DynamicValueSourceParser dynamicValueSourceParser) { super(sourceList); this.values = values; fnParams = new ArrayList<Object>(); this.fp = fp; mappings = new HashMap<Class<?>, String>(); mappings.put(String.class, "strVal"); mappings.put(Integer.class, "intVal"); mappings.put(int.class, "intVal"); mappings.put(Boolean.class, "boolVal"); mappings.put(boolean.class, "boolVal"); mappings.put(Double.class, "doubleVal"); mappings.put(double.class, "doubleVal"); mappings.put(Float.class, "floatVal"); mappings.put(float.class, "floatVal"); mappings.put(Long.class, "longVal"); mappings.put(long.class, "longVal"); vs = new LiteralValueSource(""); wrappedIntDocValues = new WrappedIntDocValues(vs); autoVars = new HashMap<String, Object>(); autoVars.put("org.apache.solr.request.SolrQueryRequest", this.fp.getReq()); this.caller = dynamicValueSourceParser; }
From source file:org.apache.solr.schema.GeoHashField.java
License:Apache License
@Override public Query createSpatialQuery(QParser parser, SpatialOptions options) { double[] point = new double[0]; try {//from w w w . ja v a 2 s . c o m point = ParseUtils.parsePointDouble(null, options.pointStr, 2); } catch (InvalidShapeException e) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e); } String geohash = GeohashUtils.encodeLatLon(point[0], point[1]); //TODO: optimize this return new SolrConstantScoreQuery( new ValueSourceRangeFilter( new GeohashHaversineFunction(getValueSource(options.field, parser), new LiteralValueSource(geohash), options.radius), "0", String.valueOf(options.distance), true, true)); }
From source file:org.apache.solr.search.FunctionQParser.java
License:Apache License
/** * Parse an individual value source./*from ww w . ja v a2s. c o m*/ * * @param doConsumeDelimiter whether to consume a delimiter following the ValueSource */ protected ValueSource parseValueSource(boolean doConsumeDelimiter) throws SyntaxError { ValueSource valueSource; int ch = sp.peek(); if (ch >= '0' && ch <= '9' || ch == '.' || ch == '+' || ch == '-') { Number num = sp.getNumber(); if (num instanceof Long) { valueSource = new LongConstValueSource(num.longValue()); } else if (num instanceof Double) { valueSource = new DoubleConstValueSource(num.doubleValue()); } else { // shouldn't happen valueSource = new ConstValueSource(num.floatValue()); } } else if (ch == '"' || ch == '\'') { valueSource = new LiteralValueSource(sp.getQuotedString()); } else if (ch == '$') { sp.pos++; String param = sp.getId(); String val = getParam(param); if (val == null) { throw new SyntaxError("Missing param " + param + " while parsing function '" + sp.val + "'"); } QParser subParser = subQuery(val, "func"); if (subParser instanceof FunctionQParser) { ((FunctionQParser) subParser).setParseMultipleSources(true); } Query subQuery = subParser.getQuery(); if (subQuery instanceof FunctionQuery) { valueSource = ((FunctionQuery) subQuery).getValueSource(); } else { valueSource = new QueryValueSource(subQuery, 0.0f); } /*** // dereference *simple* argument (i.e., can't currently be a function) // In the future we could support full function dereferencing via a stack of ValueSource (or StringParser) objects ch = val.length()==0 ? '\0' : val.charAt(0); if (ch>='0' && ch<='9' || ch=='.' || ch=='+' || ch=='-') { QueryParsing.StrParser sp = new QueryParsing.StrParser(val); Number num = sp.getNumber(); if (num instanceof Long) { valueSource = new LongConstValueSource(num.longValue()); } else if (num instanceof Double) { valueSource = new DoubleConstValueSource(num.doubleValue()); } else { // shouldn't happen valueSource = new ConstValueSource(num.floatValue()); } } else if (ch == '"' || ch == '\'') { QueryParsing.StrParser sp = new QueryParsing.StrParser(val); val = sp.getQuotedString(); valueSource = new LiteralValueSource(val); } else { if (val.length()==0) { valueSource = new LiteralValueSource(val); } else { String id = val; SchemaField f = req.getSchema().getField(id); valueSource = f.getType().getValueSource(f, this); } } ***/ } else { String id = sp.getId(); if (sp.opt("(")) { // a function... look it up. ValueSourceParser argParser = req.getCore().getValueSourceParser(id); if (argParser == null) { throw new SyntaxError("Unknown function " + id + " in FunctionQuery(" + sp + ")"); } valueSource = argParser.parse(this); sp.expect(")"); } else { if ("true".equals(id)) { valueSource = new BoolConstValueSource(true); } else if ("false".equals(id)) { valueSource = new BoolConstValueSource(false); } else { SchemaField f = req.getSchema().getField(id); valueSource = f.getType().getValueSource(f, this); } } } if (doConsumeDelimiter) consumeArgumentDelimiter(); return valueSource; }