From d8928bb3c75b313714a0e3923445a135ed37037a Mon Sep 17 00:00:00 2001 From: Yonik Seeley Date: Wed, 8 Jun 2011 16:34:33 +0000 Subject: [PATCH] SOLR-2444: implement parsing only of augmenters (localParams syntax but using []) git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1133457 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/solr/search/QueryParsing.java | 20 ++++++++++------ .../org/apache/solr/search/ReturnFields.java | 24 ++++++++++++++----- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/solr/src/java/org/apache/solr/search/QueryParsing.java b/solr/src/java/org/apache/solr/search/QueryParsing.java index 22a68f8266a..4ee880c4f5b 100644 --- a/solr/src/java/org/apache/solr/search/QueryParsing.java +++ b/solr/src/java/org/apache/solr/search/QueryParsing.java @@ -88,11 +88,16 @@ public class QueryParsing { // note to self: something needs to detect infinite recursion when parsing queries - static int parseLocalParams(String txt, int start, Map target, SolrParams params) throws ParseException { + public static int parseLocalParams(String txt, int start, Map target, SolrParams params) throws ParseException { + return parseLocalParams(txt, start, target, params, LOCALPARAM_START, LOCALPARAM_END); + } + + + public static int parseLocalParams(String txt, int start, Map target, SolrParams params, String startString, char endChar) throws ParseException { int off = start; - if (!txt.startsWith(LOCALPARAM_START, off)) return start; + if (!txt.startsWith(startString, off)) return start; StrParser p = new StrParser(txt, start, txt.length()); - p.pos += 2; // skip over "{!" + p.pos += startString.length(); // skip over "{!" for (; ;) { /* @@ -101,13 +106,13 @@ public class QueryParsing { } */ char ch = p.peek(); - if (ch == LOCALPARAM_END) { + if (ch == endChar) { return p.pos + 1; } String id = p.getId(); if (id.length() == 0) { - throw new ParseException("Expected identifier '}' parsing local params '" + txt + '"'); + throw new ParseException("Expected ending character '" + endChar + "' parsing local params '" + txt + '"'); } String val = null; @@ -131,7 +136,7 @@ public class QueryParsing { if (ch == '\"' || ch == '\'') { val = p.getQuotedString(); } else { - // read unquoted literal ended by whitespace or '}' + // read unquoted literal ended by whitespace or endChar (normally '}') // there is no escaping. int valStart = p.pos; for (; ;) { @@ -139,7 +144,7 @@ public class QueryParsing { throw new ParseException("Missing end to unquoted value starting at " + valStart + " str='" + txt + "'"); } char c = p.val.charAt(p.pos); - if (c == LOCALPARAM_END || Character.isWhitespace(c)) { + if (c == endChar || Character.isWhitespace(c)) { val = p.val.substring(valStart, p.pos); break; } @@ -157,6 +162,7 @@ public class QueryParsing { } } + public static String encodeLocalParamVal(String val) { int len = val.length(); int i = 0; diff --git a/solr/src/java/org/apache/solr/search/ReturnFields.java b/solr/src/java/org/apache/solr/search/ReturnFields.java index 9880309306b..9f8c82b2c48 100644 --- a/solr/src/java/org/apache/solr/search/ReturnFields.java +++ b/solr/src/java/org/apache/solr/search/ReturnFields.java @@ -16,17 +16,14 @@ */ package org.apache.solr.search; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Set; +import java.util.*; import org.apache.commons.io.FilenameUtils; import org.apache.lucene.queryParser.ParseException; import org.apache.lucene.search.Query; import org.apache.solr.common.SolrException; import org.apache.solr.common.params.CommonParams; +import org.apache.solr.common.params.MapSolrParams; import org.apache.solr.common.params.SolrParams; import org.apache.solr.common.util.NamedList; import org.apache.solr.core.SolrCore; @@ -211,9 +208,24 @@ public class ReturnFields sp.pos = start; } - // let's try it as a function instead String funcStr = sp.val.substring(start); + // Is it an augmenter of the form [augmenter_name foo=1 bar=myfield]? + // This is identical to localParams syntax except it uses [] instead of {!} + + if (funcStr.startsWith("[")) { + Map augmenterArgs = new HashMap(); + int end = QueryParsing.parseLocalParams(funcStr, 0, augmenterArgs, req.getParams(), "[", ']'); + sp.pos += end; + String augmenterName = augmenterArgs.get("type"); // [foo] is short for [type=foo] in localParams syntax + // TODO: look up and add the augmenter. If the form was myalias:[myaugmenter], then "key" will be myalias + SolrParams augmenterParams = new MapSolrParams(augmenterArgs); + log.info("Parsed augmenter " + augmenterParams + " with alias " + key); // TODO: remove log statement after augmenter works + continue; + } + + + // let's try it as a function instead QParser parser = QParser.getParser(funcStr, FunctionQParserPlugin.NAME, req); Query q = null; ValueSource vs = null;