List of usage examples for org.apache.lucene.queries.function.valuesource ConstValueSource ConstValueSource
public ConstValueSource(float constant)
From source file:alba.solr.core.DynamicValueSourceParser.java
License:Apache License
@SuppressWarnings("unchecked") public ValueSource parse(FunctionQParser fp) throws SyntaxError { String functionName;//from w w w . j a v a2s . c o 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:com.github.tteofili.apacheconeu14.oak.search.nls.NLSQueryIndex.java
License:Apache License
@Override public Cursor query(Filter filter, NodeState nodeState) { Thread thread = Thread.currentThread(); ClassLoader loader = thread.getContextClassLoader(); thread.setContextClassLoader(Client.class.getClassLoader()); try {//from ww w.ja v a 2s . c om final IndexSearcher searcher = IndexUtils.getSearcher(); if (searcher != null) { Filter.PropertyRestriction nativeQueryRestriction = filter.getPropertyRestriction(NATIVE_NLS_QUERY); String nativeQueryString = String .valueOf(nativeQueryRestriction.first.getValue(nativeQueryRestriction.first.getType())); // build the parse tree of the query and filter the uninteresting part (e.g. "who is the admin" -> "admin") String purgedQuery = pcfg.filterQuestion(nativeQueryString); BooleanQuery booleanClauses = new BooleanQuery(); // add clauses for the purged natural language query (if existing) if (purgedQuery != null) { booleanClauses.add(new BooleanClause(new TermQuery(new Term("jcr:title", purgedQuery)), BooleanClause.Occur.SHOULD)); booleanClauses.add(new BooleanClause(new TermQuery(new Term("jcr:description", purgedQuery)), BooleanClause.Occur.SHOULD)); booleanClauses.add(new BooleanClause(new TermQuery(new Term("text", purgedQuery)), BooleanClause.Occur.SHOULD)); } // infer "class" of the query and boost based on that try { initializeClassifier(searcher); ClassificationResult<BytesRef> result = null; try { result = classifier.assignClass(nativeQueryString); } catch (Exception e) { // do nothing } if (result != null) { booleanClauses.add(new BooleanClause(new BoostedQuery( new TermQuery(new Term("jcr:primaryType", result.getAssignedClass())), new ConstValueSource(2.0f)), BooleanClause.Occur.SHOULD)); } final TopDocs topDocs = searcher.search(booleanClauses, 100); final ScoreDoc[] scoreDocs = topDocs.scoreDocs; return new Cursor() { private int index = 0; @Override public IndexRow next() { final ScoreDoc scoreDoc = scoreDocs[index]; index++; return new IndexRow() { @Override public String getPath() { try { return searcher.doc(scoreDoc.doc).get("path"); } catch (IOException e) { return null; } } @Override public PropertyValue getValue(String s) { try { if ("jcr:score".equals(s)) { PropertyValues.newString(String.valueOf(scoreDoc.score)); } return PropertyValues.newString(searcher.doc(scoreDoc.doc).get(s)); } catch (IOException e) { return null; } } }; } @Override public boolean hasNext() { return index < scoreDocs.length; } @Override public void remove() { } }; } catch (IOException e) { // do nothing } } } finally { thread.setContextClassLoader(loader); } return null; }
From source file:org.apache.solr.search.FunctionQParser.java
License:Apache License
/** * Parse an individual value source.//from ww w.j ava2s . co 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; }