Permalink
Browse files

new public couchapp

  • Loading branch information...
0 parents commit 47d118f85177b33536d11bb2212eddfa24288b0b @hardtke hardtke committed May 5, 2011
Showing with 89 additions and 0 deletions.
  1. +37 −0 README.md
  2. +51 −0 views/whitespace/map.js
  3. +1 −0 views/whitespace/reduce.js
37 README.md
@@ -0,0 +1,37 @@
+# jsindexer
+
+jsindexer is an example javascript view that creates an inverted index needed to do full text search on a CouchApp or CouchDB database hosted on cloudant.com. This is an alternative to the Java/Lucene indexers used by default.
+
+## Requirements
+
+cloudant hosted account (sign up at <https://cloudant.com/#!/solutions/cloud>), couchapp
+
+## Install
+
+<pre><code>cd jsindexer
+cat > .couchapprc
+{"env":{"default":{"db":"http://&lt;user&gt;:&lt;pass&gt;@&lt;user&gt;.cloudant.com:5984/&lt;db_or_couchapp_you_want_to_search&gt;"}}}
+^C</code></pre>
+
+*that last line means hit **CTRL-C***
+
+To configure the indexing of documents, modify the file:
+
+views/whitespace/map.js
+
+Cloudant search expects key,value pairs with the following format:
+
+<pre><code>emit([field,token],[[1,6,8]]);</code></pre>
+
+where 1,6,8 are the positions of that token in the field. The positions enable phrase searches.
+
+<pre><code>couchapp push</code></pre>
+
+Now trigger indexing with:
+
+<pre><code>http://&lt;user&gt;.cloudant.com:5984/&lt;db_or_couchapp_you_want_to_search&gt;/_design/jsindexer/_view/whitespace</code></pre>
+
+Once indexing is done, you can use the regular search syntax (<http://support.cloudant.com/kb/search/search-api>), but yon need to specify the index:
+
+<pre><code>http://&lt;user&gt;.cloudant.com:5984/&lt;db_or_couchapp_you_want_to_search&gt;/_search?q=myfield:"some phrase"&index=_design/jsindexer/_view/whitespace</code></pre>
+
51 views/whitespace/map.js
@@ -0,0 +1,51 @@
+function(doc) {
+ function map_array(arr,name) {
+ var i;
+ for (i = 0; i < arr.length; i++) {
+ var value = arr[i];
+ if (value instanceof Array) {
+ map_array(value,name);
+ } else if (value instanceof Object) {
+ map_object(value, name);
+ } else {
+ emit_as_string(value, name);
+ }
+ }
+ }
+ function map_object(doc,name) {
+ var key;
+ var newname;
+ for (key in doc) {
+ if (name===null) {
+ newname = key;
+ } else {
+ newname = name + "." + key;
+ }
+ var value = doc[key];
+ if (value instanceof Array) {
+ map_array(value,newname);
+ } else if (value instanceof Object) {
+ map_object(value, newname);
+ } else {
+ emit_as_string(value, newname);
+ }
+ }
+ }
+ function analyzer(value) {
+ var lc = value.toLowerCase();
+ var arr = lc.split(/[\s,.!;?:-]/);
+ return arr;
+ }
+ function emit_as_string(value,name) {
+ try {
+ var arr = analyzer(value);
+ var i;
+ for (i=0;i<arr.length;i++) {
+ emit([name,arr[i]],[[i]]);
+ }
+ } catch (err) {
+ emit([name,value],[[0]]);
+ }
+ }
+ map_object(doc,null);
+}
1 views/whitespace/reduce.js
@@ -0,0 +1 @@
+_count

0 comments on commit 47d118f

Please sign in to comment.