|
@@ -21,12 +21,17 @@ function SearchClient(crowi, esUri) {
|
|
|
requestTimeout: 5000,
|
|
requestTimeout: 5000,
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
+ this.registerUpdateEvent();
|
|
|
|
|
+
|
|
|
this.mappingFile = crowi.resourceDir + 'search/mappings.json';
|
|
this.mappingFile = crowi.resourceDir + 'search/mappings.json';
|
|
|
- //this.Page = crowi.model('Page');
|
|
|
|
|
- //this.Config = crowi.model('Config');
|
|
|
|
|
- //this.config = crowi.getConfig();
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+SearchClient.prototype.registerUpdateEvent = function() {
|
|
|
|
|
+ var pageEvent = this.crowi.event('page');
|
|
|
|
|
+ pageEvent.on('create', this.syncPageCreated.bind(this))
|
|
|
|
|
+ pageEvent.on('update', this.syncPageUpdated.bind(this))
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
SearchClient.prototype.parseUri = function(uri) {
|
|
SearchClient.prototype.parseUri = function(uri) {
|
|
|
if (!(m = uri.match(/^elasticsearch:\/\/([^:]+):([^\/]+)\/(.+)$/))) {
|
|
if (!(m = uri.match(/^elasticsearch:\/\/([^:]+):([^\/]+)\/(.+)$/))) {
|
|
|
throw new Error('Invalid ELASTICSEARCH_URI format. Should be elasticsearch://host:port/index_name');
|
|
throw new Error('Invalid ELASTICSEARCH_URI format. Should be elasticsearch://host:port/index_name');
|
|
@@ -65,12 +70,14 @@ SearchClient.prototype.prepareBodyForUpdate = function(body, page) {
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
var document = {
|
|
var document = {
|
|
|
- path: page.path,
|
|
|
|
|
- body: page.revision.body,
|
|
|
|
|
- username: page.creator.username,
|
|
|
|
|
- comment_count: page.commentCount,
|
|
|
|
|
- like_count: page.liker.length || 0,
|
|
|
|
|
- updated_at: page.updatedAt,
|
|
|
|
|
|
|
+ doc: {
|
|
|
|
|
+ path: page.path,
|
|
|
|
|
+ body: page.revision.body,
|
|
|
|
|
+ comment_count: page.commentCount,
|
|
|
|
|
+ bookmark_count: 0, // todo
|
|
|
|
|
+ like_count: page.liker.length || 0,
|
|
|
|
|
+ updated_at: page.updatedAt,
|
|
|
|
|
+ }
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
body.push(command);
|
|
body.push(command);
|
|
@@ -114,6 +121,7 @@ SearchClient.prototype.addPages = function(pages)
|
|
|
self.prepareBodyForCreate(body, page);
|
|
self.prepareBodyForCreate(body, page);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
+ debug('addPages(): Sending Request to ES', body);
|
|
|
return this.client.bulk({
|
|
return this.client.bulk({
|
|
|
body: body,
|
|
body: body,
|
|
|
});
|
|
});
|
|
@@ -128,6 +136,7 @@ SearchClient.prototype.updatePages = function(pages)
|
|
|
self.prepareBodyForUpdate(body, page);
|
|
self.prepareBodyForUpdate(body, page);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
+ debug('updatePages(): Sending Request to ES', body);
|
|
|
return this.client.bulk({
|
|
return this.client.bulk({
|
|
|
body: body,
|
|
body: body,
|
|
|
});
|
|
});
|
|
@@ -323,28 +332,27 @@ SearchClient.prototype.searchKeywordUnderPath = function(keyword, path, option)
|
|
|
return this.search(query);
|
|
return this.search(query);
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
-module.exports = SearchClient;
|
|
|
|
|
|
|
+SearchClient.prototype.syncPageCreated = function(page, user)
|
|
|
|
|
+{
|
|
|
|
|
+ this.addPages([page])
|
|
|
|
|
+ .then(function(res) {
|
|
|
|
|
+ debug('ES Response', res);
|
|
|
|
|
+ })
|
|
|
|
|
+ .catch(function(err){
|
|
|
|
|
+ debug('ES Error', err);
|
|
|
|
|
+ });
|
|
|
|
|
+};
|
|
|
|
|
|
|
|
-/*
|
|
|
|
|
-
|
|
|
|
|
- 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
|
|
|
|
|
- });
|
|
|
|
|
|
|
+SearchClient.prototype.syncPageUpdated = function(page, user)
|
|
|
|
|
+{
|
|
|
|
|
+ this.updatePages([page])
|
|
|
|
|
+ .then(function(res) {
|
|
|
|
|
+ debug('ES Response', res);
|
|
|
|
|
+ })
|
|
|
|
|
+ .catch(function(err){
|
|
|
|
|
+ debug('ES Error', err);
|
|
|
|
|
+ });
|
|
|
|
|
+};
|
|
|
|
|
|
|
|
- };
|
|
|
|
|
|
|
|
|
|
-*/
|
|
|
|
|
|
|
+module.exports = SearchClient;
|