This repository has been archived by the owner on Oct 22, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 47d118f
Showing
3 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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://<user>:<pass>@<user>.cloudant.com:5984/<db_or_couchapp_you_want_to_search>"}}} | ||
^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://<user>.cloudant.com:5984/<db_or_couchapp_you_want_to_search>/_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://<user>.cloudant.com:5984/<db_or_couchapp_you_want_to_search>/_search?q=myfield:"some phrase"&index=_design/jsindexer/_view/whitespace</code></pre> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
_count |