Permalink
Browse files

Add include_fields option to return a chosen subset of stored fields.…

… (closes #134)
  • Loading branch information...
1 parent 2525171 commit c9d4f76fbfe354e9319c0a8e8d5100b8137ca952 Robert Newson committed Aug 18, 2011
Showing with 16 additions and 1 deletion.
  1. +1 −0 README.md
  2. +15 −1 src/main/java/com/github/rnewson/couchdb/lucene/DatabaseIndexer.java
View
@@ -425,6 +425,7 @@ The following parameters can be passed for more sophisticated searches;
<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>include_fields</dt><dd>By default, <i>all</i> stored fields are returned with results. Use a comma-separate list of field names with this parameter to refine the response</dd>
<dt>limit</dt><dd>the maximum number of results to return</dd>
<dt>q</dt><dd>the query to run (e.g, subject:hello). If not specified, the default field is searched. Multiple queries can be supplied, separated by commas; the resulting JSON will be an array of responses.</dd>
<dt>skip</dt><dd>the number of results to skip</dd>
@@ -9,6 +9,7 @@
import java.io.InputStreamReader;
import java.io.Writer;
import java.net.SocketException;
+import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
@@ -35,7 +36,9 @@
import org.apache.log4j.Logger;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
+import org.apache.lucene.document.FieldSelector;
import org.apache.lucene.document.Fieldable;
+import org.apache.lucene.document.MapFieldSelector;
import org.apache.lucene.document.NumericField;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexReader.FieldOption;
@@ -521,6 +524,15 @@ public void search(final HttpServletRequest req,
.getParameter("sort"));
final int skip = getIntParameter(req, "skip", 0);
+ final FieldSelector fieldSelector;
+ if (req.getParameter("include_fields") == null) {
+ fieldSelector = null;
+ } else {
+ final String[] fields = Utils.splitOnCommas(
+ req.getParameter("include_fields"));
+ fieldSelector = new MapFieldSelector(Arrays.asList(fields));
+ }
+
if (sort == null) {
td = searcher.search(q, null, skip + limit);
} else {
@@ -534,7 +546,9 @@ public void search(final HttpServletRequest req,
final JSONArray rows = new JSONArray();
final String[] fetch_ids = new String[max];
for (int i = skip; i < skip + max; i++) {
- final Document doc = searcher.doc(td.scoreDocs[i].doc);
+ final Document doc = searcher.doc(td.scoreDocs[i].doc,
+ fieldSelector);
+
final JSONObject row = new JSONObject();
final JSONObject fields = new JSONObject();

0 comments on commit c9d4f76

Please sign in to comment.