Explorar el Código

WIP: add search util

Sotaro KARASAWA hace 10 años
padre
commit
5017569c99
Se han modificado 3 ficheros con 276 adiciones y 0 borrados
  1. 183 0
      lib/util/search.js
  2. 17 0
      resource/search/mappings.json
  3. 76 0
      search.js

+ 183 - 0
lib/util/search.js

@@ -0,0 +1,183 @@
+/**
+ * Search
+ */
+
+
+//module.exports = SearchClient;
+
+/*
+var elasticsearch = require('elasticsearch'),
+  debug = require('debug')('crowi:lib:search'),
+
+function SearchClient(crowi) {
+  this.crowi = crowi;
+  this.Page = crowi.model('Page');
+  this.Config = crowi.model('Config');
+  this.config = crowi.getConfig();
+}
+
+SearchClient.prototype.deleteIndex = function() {
+};
+*/
+
+module.exports = function(crowi) {
+  var elasticsearch = require('elasticsearch'),
+    debug = require('debug')('crowi:lib:search'),
+    Page = crowi.model('Page'),
+    Config = crowi.model('Config'),
+    config = crowi.getConfig(),
+    TYPE_PAGE = 'page',
+    SLOW_INTERVAL = 200, // 200ms interval.
+    lib = {};
+
+  // TODO: configurable
+  var host = '127.0.0.1:9200';
+  var index_name = 'crowi';
+  var default_mapping_file = crowi.resourceDir + 'search/mappings.json';
+
+  var client = new elasticsearch.Client({
+    host: host,
+  });
+
+
+  lib.deleteIndex = function() {
+    return client.indices.delete({
+      index: index_name
+    });
+  };
+
+  lib.buildIndex = function() {
+    return client.indices.create({
+      index: index_name,
+      body: require(default_mapping_file)
+    });
+  };
+
+  lib.rebuildIndex = function() {
+    var self = this;
+
+    return self.deleteIndex()
+    .then(function(data) {
+      return self.buildIndex();
+    });
+  };
+
+  lib.addAllPages = function() {
+    var offset = 0;
+    var stream = Page.getStreamOfFindAll();
+    var self = this;
+
+    stream.on('data', function (doc) {
+      if (!doc.creator || !doc.revision) {
+        debug('Skipped', doc.path);
+        return ;
+      }
+
+      var likeCount = doc.liker.length;
+      var bookmarkCount = 0; // TODO
+      var updated = doc.updatedAt; // TODO
+
+      self.addPage(doc._id.toString(), doc.path, doc.revision.body, doc.creator.username, likeCount, bookmarkCount, updated, true)
+      .then(function(data) {
+        debug('Page Added', data);
+      }).catch(function (err) {
+        debug('Error addPage:', err);
+      });
+
+      //debug('Data received: ', doc.path, doc.liker.length, doc.revision.body);
+    }).on('error', function (err) {
+      debug('Error stream:', err);
+      // handle err
+    }).on('close', function () {
+      debug('Close');
+      // all done
+    });
+  };
+
+
+  /**
+   * @return Promise
+   */
+  lib.addPage = function(id, path, body, creator, likeCount, bookmarkCount, updated, is_public) {
+    var self = this;
+
+    return client.create({
+      index: index_name,
+      type: 'page',
+      id: id,
+      body: {
+        path: path,
+        body: body,
+        creator: creator,
+        likeCount: likeCount,
+        bookmarkCount: bookmarkCount,
+        is_public: is_public,
+        updated: updated,
+      }
+    });
+
+  };
+
+  lib.updatePage = function(id, path, body, creator, likeCount, bookmarkCount, updated, is_public) {
+  };
+
+  lib.searchPageByKeyword = function(keyword) {
+    var queryBody = {
+       query: {
+        bool: {
+          should: [
+            {term: { path: { term: keyword, boost: 2.0 } }},
+            {term: { body: { term: keyword } }}
+          ]
+        }
+      },
+      highlight : { fields : { body : {} } },
+      //sort: [{ updated: { order: "desc" } } ]
+    };
+    return client.search({
+      index: index_name,
+      body: queryBody
+    });
+
+  /*
+     {
+     "query": {
+     "bool": {
+     "should": [
+     {"term": {
+     "path": {
+     "term": "php",
+     "boost": 2.0
+     }
+     }},
+     {"term": {
+     "body": {
+     "term": "php"
+     }
+     }}
+     ]
+     }
+     },
+     "highlight" : {
+     "fields" : {
+     "body" : {}
+     }
+     },
+     "sort": [
+     {
+     "updated": {
+     "order": "desc"
+     }
+     }
+     ]
+     }
+     */
+
+  };
+
+  lib.searchPageByLikeCount = function() {
+  };
+
+  return lib;
+};
+

+ 17 - 0
resource/search/mappings.json

@@ -0,0 +1,17 @@
+{
+  "mappings": {
+    "page": {
+      "_all":       { "enabled": false  },
+      "properties": {
+        "path":    { "type": "string", "analyzer": "kuromoji" },
+        "body":    { "type": "string", "analyzer": "kuromoji" },
+        "creator": { "type": "string" },
+        "lastUpdateUser": { "type": "string" },
+        "likeCount": { "type": "integer" },
+        "bookmarkCount": { "type": "integer" },
+        "updated": { "type": "date" },
+        "is_public": { "type": "boolean" }
+      }
+    }
+  }
+}

+ 76 - 0
search.js

@@ -0,0 +1,76 @@
+
+var program = require('commander')
+  , debug = require('debug')('debug:console:search-util')
+  , crowi = new (require('./lib/crowi'))(__dirname, process.env)
+  ;
+
+crowi.init()
+  .then(function(app) {
+    var search = require('./lib/util/search')(crowi);
+
+    program
+      .version(crowi.version);
+
+    program
+      .command('create-index [name]')
+      .action(function (cmd, env) {
+
+        search.buildIndex()
+          .then(function(data) {
+            console.log(data);
+          })
+          .catch(function(err) {
+            console.log("Error", err);
+          });
+      });
+
+    program
+      .command('rebuild-index [name]')
+      .action(function (cmd, env) {
+
+        search.rebuildIndex()
+          .then(function(data) {
+            console.log('rebuildIndex:', data);
+            search.addAllPages();
+          })
+          .catch(function(err) {
+            debug('Error', err);
+          });
+      });
+
+    program.parse(process.argv);
+  }).catch(crowi.exitOnError);
+
+
+//program
+//  .command('search [query]', 'search with optional query')
+//  .command('list', 'list packages installed', {isDefault: true})
+
+
+/*
+crowi.init()
+  .then(function(app) {
+    var search = require('./lib/util/search')(crowi);
+
+    search.buildIndex()
+      .then(function(data) {
+        console.log(data);
+      })
+      .catch(function(err) {
+        console.log("Error", err);
+      });
+  }).catch(crowi.exitOnError);
+
+cli.parse({
+    seed:      [false, 'Password seed', 'string', ''],
+    password:  [false, 'Password raw string', 'string'],
+});
+
+cli.main(function(args, options)
+{
+  console.log("args", args);
+  console.log("options", options);
+
+  this.output();
+});
+*/