Skip to content

Commit 735ad0d

Browse files
committed
CouchDB River: Support couchdb filter query parameters, closes #389.
1 parent 06c7c4a commit 735ad0d

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

plugins/river/couchdb/src/main/java/org/elasticsearch/river/couchdb/CouchdbRiver.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,10 @@
3636
import org.elasticsearch.indices.IndexAlreadyExistsException;
3737
import org.elasticsearch.river.*;
3838

39-
import java.io.BufferedReader;
40-
import java.io.IOException;
41-
import java.io.InputStream;
42-
import java.io.InputStreamReader;
39+
import java.io.*;
4340
import java.net.HttpURLConnection;
4441
import java.net.URL;
42+
import java.net.URLEncoder;
4543
import java.util.Map;
4644
import java.util.concurrent.TimeUnit;
4745

@@ -61,6 +59,7 @@ public class CouchdbRiver extends AbstractRiverComponent implements River {
6159
private final int couchPort;
6260
private final String couchDb;
6361
private final String couchFilter;
62+
private final String couchFilterParamsUrl;
6463

6564
private final String indexName;
6665
private final String typeName;
@@ -84,11 +83,26 @@ public class CouchdbRiver extends AbstractRiverComponent implements River {
8483
couchPort = XContentMapValues.nodeIntegerValue(couchSettings.get("port"), 5984);
8584
couchDb = XContentMapValues.nodeStringValue(couchSettings.get("db"), riverName.name());
8685
couchFilter = XContentMapValues.nodeStringValue(couchSettings.get("filter"), null);
86+
if (couchSettings.containsKey("filter_params")) {
87+
Map<String, Object> filterParams = (Map<String, Object>) couchSettings.get("filter_params");
88+
StringBuilder sb = new StringBuilder();
89+
for (Map.Entry<String, Object> entry : filterParams.entrySet()) {
90+
try {
91+
sb.append("&").append(URLEncoder.encode(entry.getKey(), "UTF-8")).append(URLEncoder.encode(entry.getValue().toString(), "UTF-8"));
92+
} catch (UnsupportedEncodingException e) {
93+
// should not happen...
94+
}
95+
}
96+
couchFilterParamsUrl = sb.toString();
97+
} else {
98+
couchFilterParamsUrl = null;
99+
}
87100
} else {
88101
couchHost = "localhost";
89102
couchPort = 5984;
90103
couchDb = "db";
91104
couchFilter = null;
105+
couchFilterParamsUrl = null;
92106
}
93107

94108
if (settings.settings().containsKey("index")) {
@@ -261,7 +275,14 @@ private class Slurper implements Runnable {
261275

262276
String file = "/" + couchDb + "/_changes?feed=continuous&include_docs=true&heartbeat=10000";
263277
if (couchFilter != null) {
264-
file = file + "&filter=" + couchFilter;
278+
try {
279+
file = file + "&filter=" + URLEncoder.encode(couchFilter, "UTF-8");
280+
} catch (UnsupportedEncodingException e) {
281+
// should not happen!
282+
}
283+
if (couchFilterParamsUrl != null) {
284+
file = file + couchFilterParamsUrl;
285+
}
265286
}
266287
if (lastSeq != null) {
267288
file = file + "&since=" + lastSeq;

0 commit comments

Comments
 (0)