Skip to content
This repository has been archived by the owner on Oct 22, 2021. It is now read-only.

Commit

Permalink
new public couchapp
Browse files Browse the repository at this point in the history
  • Loading branch information
hardtke committed May 5, 2011
0 parents commit 47d118f
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
37 changes: 37 additions & 0 deletions 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 changes: 51 additions & 0 deletions 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 change: 1 addition & 0 deletions views/whitespace/reduce.js
@@ -0,0 +1 @@
_count

0 comments on commit 47d118f

Please sign in to comment.