Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add force_json=true to force 'application/json' response type.

  • Loading branch information...
commit ea19e55420dd0201a0cbb3dddbe06fcd7ba90e08 1 parent b507ea2
@rnewson authored
View
1  README.md
@@ -282,6 +282,7 @@ You can perform all types of queries using Lucene's default <a href="http://luce
<dt>analyzer</dt><dd>The analyzer used to convert the query string into a query object.
<dt>callback</dt><dd>Specify a JSONP callback wrapper. The full JSON result will be prepended with this parameter and also placed with parentheses."
<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>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>
<dt>q</dt><dd>the query to run (e.g, subject:hello). If not specified, the default field is searched.</dd>
View
22 src/main/java/com/github/rnewson/couchdb/lucene/SearchRequest.java
@@ -75,7 +75,7 @@
private final String ifNoneMatch;
- private final String contentType;
+ private final String contentType;
public SearchRequest(final JSONObject obj, final String viewsig) throws ParseException {
final JSONObject headers = obj.getJSONObject("headers");
@@ -92,19 +92,19 @@ public SearchRequest(final JSONObject obj, final String viewsig) throws ParseExc
this.include_docs = query.optBoolean("include_docs", false);
this.rewrite_query = query.optBoolean("rewrite", false);
this.callback = query.optString("callback", null);
-
+
// Negotiate Content-Type of response.
- if (headers.optString("Accept").indexOf("application/json") != -1) {
- this.contentType = "application/json";
+ if (query.optBoolean("force_json", false) || headers.optString("Accept").indexOf("application/json") != -1) {
+ this.contentType = "application/json";
} else {
- this.contentType = "text/plain;charset=utf-8";
+ this.contentType = "text/plain;charset=utf-8";
}
// Parse query.
final Analyzer analyzer = AnalyzerCache.getAnalyzer(query.optString("analyzer", "standard"));
final QueryParser parser = new QueryParser(Config.DEFAULT_FIELD, analyzer);
if ("AND".equalsIgnoreCase(Config.DEFAULT_OPERATOR)) {
- parser.setDefaultOperator(Operator.AND);
+ parser.setDefaultOperator(Operator.AND);
}
this.q = parser.parse(query.getString("q"));
@@ -260,8 +260,8 @@ public String execute(final IndexSearcher searcher) throws IOException {
result.put("code", 200);
final JSONObject headers = new JSONObject();
- headers.put("Content-Type", contentType);
- // Allow short-term caching.
+ headers.put("Content-Type", contentType);
+ // Allow short-term caching.
headers.put("Cache-Control", "max-age=" + Config.COMMIT_MAX / 1000);
// Results can't change unless the IndexReader does.
headers.put("ETag", etag);
@@ -270,9 +270,9 @@ public String execute(final IndexSearcher searcher) throws IOException {
headers.put("Content-Type", "text/plain;charset=utf-8");
result.put("body", escape(json.toString(2)));
} else if (callback != null)
- result.put("body", String.format("%s(%s)", callback, json));
- else
- result.put("json", json);
+ result.put("body", String.format("%s(%s)", callback, json));
+ else
+ result.put("json", json);
// Include headers.
result.put("headers", headers);
Please sign in to comment.
Something went wrong with that request. Please try again.