Skip to content

Commit

Permalink
allow default_operator=and. Closes rnewson#95
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Newson committed Dec 7, 2010
1 parent 2ff52b0 commit bba040b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -379,6 +379,7 @@ The following parameters can be passed for more sophisticated searches;
<dt>analyzer</dt><dd>Override the default analyzer used to parse the q parameter</dd>
<dt>callback</dt><dd>Specify a JSONP callback wrapper. The full JSON result will be prepended with this parameter and also placed with parentheses."</dd>
<dt>debug</dt><dd>Setting this to true disables response caching (the query is executed every time) and indents the JSON response for readability.</dd>
<dt>default_operator</dt><dd>Change the default operator for boolean queries. Defaults to "OR", other permitted value is "AND".</dd>
<dt>force_json<dt><dd>Usually couchdb-lucene determines the Content-Type of its response based on the presence of the Accept header. If Accept contains "application/json", you get "application/json" in the response, otherwise you get "text/plain;charset=utf8". Some tools, like JSONView for FireFox, do not send the Accept header but do render "application/json" responses if received. Setting force_json=true forces all response to "application/json" regardless of the Accept header.</dd>
<dt>include_docs</dt><dd>whether to include the source docs</dd>
<dt>limit</dt><dd>the maximum number of results to return</dd>
Expand Down
Expand Up @@ -44,6 +44,7 @@
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.queryParser.QueryParser.Operator;
import org.apache.lucene.search.FieldDoc;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
Expand Down Expand Up @@ -126,9 +127,10 @@ public void returnSearcher(final IndexSearcher searcher)
returnReader(searcher.getIndexReader());
}

public Query parse(final String query, final Analyzer analyzer) throws ParseException, JSONException {
public Query parse(final String query, final Operator operator, final Analyzer analyzer) throws ParseException, JSONException {
final QueryParser parser = new CustomQueryParser(Constants.VERSION,
Constants.DEFAULT_FIELD, analyzer);
parser.setDefaultOperator(operator);
return parser.parse(query);
}

Expand Down Expand Up @@ -474,7 +476,9 @@ public void search(final HttpServletRequest req,
}
for (final String queryString : getQueryStrings(req)) {
final Analyzer analyzer = state.analyzer(req.getParameter("analyzer"));
final Query q = state.parse(queryString, analyzer);
final Operator operator = "and".equalsIgnoreCase(req.getParameter("default_operator"))
? Operator.AND : Operator.OR;
final Query q = state.parse(queryString, operator, analyzer);

final JSONObject queryRow = new JSONObject();
queryRow.put("q", q.toString());
Expand Down

0 comments on commit bba040b

Please sign in to comment.