Permalink
Browse files

support _optimize and _expunge calls.

  • Loading branch information...
1 parent 8ab2472 commit 8203af644add17fde4580ff67821fb813d0b9f5f Robert Newson committed Jan 10, 2010
View
14 README.md
@@ -527,3 +527,17 @@ returns;
<pre>
{"doc_count":517350,"doc_del_count":1,"disk_size":318543045}
</pre>
+
+<h1>Index Maintenance</h1>
+
+For optimal query speed you can optimize your indexes. This causes the index to be rewritten into a single segment.
+
+<pre>
+curl -X POST http://localhost:5984/<db>/_fti/<ddoc>/<index>/_optimize
+</pre>
+
+If you just want to expunge pending deletes, then call;
+
+<pre>
+curl -X POST http://localhost:5984/<db>/_fti/<ddoc>/<index>/_expunge
+</pre>
View
24 couchdb-external-hook.py
@@ -56,32 +56,30 @@ def requests():
def respond(res, req, key):
path = req.get("path", [])
- if len(path) != 4:
- body = "\n".join([
- "Invalid path: %s" % '/'.join([''] + path),
- "Paths should be: /db_name/_fti/ddocid/index_name?q=...",
- "'ddocid' is from the '_design/ddocid' that defines index_name"
- ])
- return mkresp(400, body, {"Content-Type": "text/plain"})
-
# Drop name of external hook.
del path[1]
# URL-escape each part
for index, item in enumerate(path):
path[index] = urllib.quote(path[index], "")
- if req["query"] == {}:
- path = '/'.join(['', 'info', key] + path)
+ if len(path) == 3:
+ if req["query"] == {}:
+ path = '/'.join(['', 'info', key] + path)
+ else:
+ path = '/'.join(['', 'search', key] + path)
+ path = '?'.join([path, urllib.urlencode(req["query"])])
+ elif len(path) == 4:
+ path = '/'.join(['', 'admin', key] + path)
else:
- path = '/'.join(['', 'search', key] + path)
- path = '?'.join([path, urllib.urlencode(req["query"])])
+ return mkresp(400, "Invalid path\n" + str(len(req)), {"Content-Type":"text/plain"})
req_headers = {}
for h in req.get("headers", []):
if h.lower() in ["accept", "if-none-match"]:
req_headers[h] = req["headers"][h]
- res.request("GET", path, headers=req_headers)
+ sys.stderr.write(str(path))
+ res.request(req["method"], path, headers=req_headers)
resp = res.getresponse()
resp_headers = {}
View
27 src/main/java/com/github/rnewson/couchdb/lucene/AdminServlet.java
@@ -17,6 +17,7 @@
*/
import java.io.IOException;
+import java.io.Writer;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
@@ -29,6 +30,7 @@
import com.github.rnewson.couchdb.lucene.Lucene.WriterCallback;
import com.github.rnewson.couchdb.lucene.util.IndexPath;
import com.github.rnewson.couchdb.lucene.util.ServletUtils;
+import com.github.rnewson.couchdb.lucene.util.Utils;
/**
* Administrative functions.
@@ -47,6 +49,8 @@
private static final long serialVersionUID = 1L;
+ private static final String JSON_SUCCESS = "{\"ok\":true}";
+
private Lucene lucene;
private HierarchicalINIConfiguration configuration;
@@ -61,16 +65,18 @@ public void setConfiguration(final HierarchicalINIConfiguration configuration) {
@Override
protected void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
- final String command = req.getParameter("cmd");
-
final IndexPath path = IndexPath.parse(configuration, req);
if (path == null) {
ServletUtils.sendJSONError(req, resp, 400, "Bad path");
return;
}
+ lucene.startIndexing(path, true);
+
+ String command = req.getPathInfo();
+ command = command.substring(command.lastIndexOf("/") + 1);
- if ("expunge".equals(command)) {
+ if ("_expunge".equals(command)) {
lucene.withWriter(path, new WriterCallback() {
public boolean callback(final IndexWriter writer) throws IOException {
writer.expungeDeletes(false);
@@ -81,11 +87,13 @@ public void onMissing() throws IOException {
resp.sendError(404);
}
});
+ Utils.setResponseContentTypeAndEncoding(req, resp);
resp.setStatus(202);
+ sendJSON(resp, JSON_SUCCESS);
return;
}
- if ("optimize".equals(command)) {
+ if ("_optimize".equals(command)) {
lucene.withWriter(path, new WriterCallback() {
public boolean callback(final IndexWriter writer) throws IOException {
writer.optimize(false);
@@ -96,11 +104,22 @@ public void onMissing() throws IOException {
resp.sendError(404);
}
});
+ Utils.setResponseContentTypeAndEncoding(req, resp);
resp.setStatus(202);
+ sendJSON(resp, JSON_SUCCESS);
return;
}
resp.sendError(400, "Bad request");
}
+ private void sendJSON(final HttpServletResponse resp, final String json) throws IOException {
+ final Writer writer = resp.getWriter();
+ try {
+ writer.write(json);
+ } finally {
+ writer.close();
+ }
+ }
+
}
View
1 src/main/java/com/github/rnewson/couchdb/lucene/InfoServlet.java
@@ -66,6 +66,7 @@ protected void doGet(final HttpServletRequest req, final HttpServletResponse res
ServletUtils.sendJSONError(req, resp, 400, "Bad path");
return;
}
+ lucene.startIndexing(path, true);
lucene.withReader(path, Utils.getStaleOk(req), new ReaderCallback() {
public void callback(final IndexReader reader) throws IOException {
View
2 src/main/java/com/github/rnewson/couchdb/lucene/SearchServlet.java
@@ -18,7 +18,6 @@
import static com.github.rnewson.couchdb.lucene.util.ServletUtils.getBooleanParameter;
import static com.github.rnewson.couchdb.lucene.util.ServletUtils.getIntParameter;
-import static com.github.rnewson.couchdb.lucene.util.ServletUtils.getParameter;
import static java.lang.Math.max;
import static java.lang.Math.min;
@@ -55,7 +54,6 @@
import com.github.rnewson.couchdb.lucene.Lucene.SearcherCallback;
import com.github.rnewson.couchdb.lucene.couchdb.Couch;
import com.github.rnewson.couchdb.lucene.couchdb.Database;
-import com.github.rnewson.couchdb.lucene.util.Analyzers;
import com.github.rnewson.couchdb.lucene.util.Constants;
import com.github.rnewson.couchdb.lucene.util.IndexPath;
import com.github.rnewson.couchdb.lucene.util.ServletUtils;
View
2 src/main/java/com/github/rnewson/couchdb/lucene/util/IndexPath.java
@@ -26,7 +26,7 @@
public static IndexPath parse(final HierarchicalINIConfiguration configuration, final HttpServletRequest req) {
final String uri = req.getRequestURI().replaceFirst("^/\\w+/", "");
final String[] parts = uri.split("/");
- if (parts.length != 4) {
+ if (parts.length < 4) {
return null;
}
final Configuration section = configuration.getSection(parts[0]);

0 comments on commit 8203af6

Please sign in to comment.