summaryrefslogtreecommitdiff
path: root/search/elasticlunr/create_index
diff options
context:
space:
mode:
Diffstat (limited to 'search/elasticlunr/create_index')
-rwxr-xr-xsearch/elasticlunr/create_index31
1 files changed, 31 insertions, 0 deletions
diff --git a/search/elasticlunr/create_index b/search/elasticlunr/create_index
new file mode 100755
index 0000000..2fb3adc
--- /dev/null
+++ b/search/elasticlunr/create_index
@@ -0,0 +1,31 @@
+#!/usr/bin/env node
+
+const elasticlunr = require( './elasticlunr.min' );
+const fs = require( 'fs' );
+const JSONStream = require( 'JSONStream' );
+const es = require( 'event-stream' );
+
+console.log( 'Creating ElasticLunr index..' );
+
+const index = new elasticlunr.Index( );
+index.addField( 'title' );
+index.addField( 'content' );
+index.setRef( 'uri' );
+index.saveDocument( true );
+
+var stream = fs.createReadStream( './posts.json' );
+
+stream.pipe( JSONStream.parse( '*') )
+ .pipe( es.mapSync( function( data ) {
+ index.addDoc( data );
+ return data;
+ } )
+ .on( 'end', function( ) {
+ fs.writeFile( './posts.index', JSON.stringify( index ),
+ function( err ) {
+ if( err ) throw err;
+ console.log( 'Finished creating index..' );
+ }
+ );
+ } )
+)