Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for NodeBB v1.5.x #10

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 93 additions & 50 deletions library.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var db = module.parent.require('./database'),
winston = module.parent.require('winston'),
elasticsearch = require('elasticsearch'),
async = module.parent.require('async'),
_ = module.parent.require('underscore'),
_ = require('underscore'),

//LRU = require('lru-cache'),
//cache = LRU({ max: 20, maxAge: 1000 * 60 * 60 }), // Remember the last 20 searches in the past hour
Expand All @@ -21,10 +21,8 @@ var db = module.parent.require('./database'),
});
},

client = new elasticsearch.Client({
host: 'localhost:9200'
// log: 'trace'
}),
client = null,

// this config dosen't work for newer version of elasticsearch api
Elasticsearch = {
/*
Expand Down Expand Up @@ -165,7 +163,8 @@ Elasticsearch.getTopicCount = function(callback) {
};

Elasticsearch.connect = function() {
if (!Elasticsearch.config.host) {
var originalHost = Elasticsearch.config.host || Elasticsearch.originalHost;
if (!originalHost) {
return;
}

Expand All @@ -174,7 +173,7 @@ Elasticsearch.connect = function() {
}

// Convert host to array
var hosts = Elasticsearch.config.host.split(',');
var hosts = originalHost.split(',');
hosts = _.map(hosts, function(host){ return _.trim(host); });

// Compact array to remove empty elements just in case.
Expand All @@ -187,9 +186,15 @@ Elasticsearch.connect = function() {
Elasticsearch.config.hosts = hosts;

// Now remove the host since we're going to use hosts.
Elasticsearch.originalHost = originalHost;
delete Elasticsearch.config.host;

Elasticsearch.client = new elasticsearch.Client(Elasticsearch.config);
// Elasticsearch.config.host = 'elasticsearch24';
// Elasticsearch.config.port = 9200;
console.log('Elasticsearch connect: ', Elasticsearch.config);
client = Elasticsearch.client = new elasticsearch.Client(Elasticsearch.config);

Elasticsearch.createIndex();
};

Elasticsearch.adminMenu = function(custom_header, callback) {
Expand All @@ -208,6 +213,9 @@ Elasticsearch.search = function(data, callback) {
winston.warn('[plugin/elasticsearch] Another search plugin (dbsearch or solr) is enabled, so search via Elasticsearch was aborted.');
return callback(null, data);
}
if (!data.content) {
return callback(null, []);
}
var queryMatch = {
content: escapeSpecialChars(data.content)
};
Expand Down Expand Up @@ -239,12 +247,12 @@ Elasticsearch.search = function(data, callback) {
}
};
// changing the client obj
console.log('search query', query);
// console.log('search query', query);
client.search(query, function(err, obj) {
if (err) {
callback(err);
} else if (obj && obj.hits && obj.hits.hits && obj.hits.hits.length > 0) {
console.log('search hits', obj.hits.hits);
// console.log('search hits', obj.hits.hits);
var payload = obj.hits.hits.map(function(result) {
// return the correct post id
if (data.index === 'topic') {
Expand Down Expand Up @@ -474,21 +482,57 @@ Elasticsearch.flush = function(req, res) {
});
};

Elasticsearch.createIndex = function() {
if (!Elasticsearch.client) {
return;
}
console.log('createIndex....');
Elasticsearch.client.indices.create({
index: Elasticsearch.config.index_name,
type: Elasticsearch.config.post_type,
body: {
"properties": {
"title": {
"type": "string",
"analyzer": "ik_smart",
"search_analyzer": "ik_smart"
},
"content": {
"type": "string",
"analyzer": "ik_smart",
"search_analyzer": "ik_smart",
"include_in_all": "true",
"boost": 8
}
}
}
}, function (err, resp, respcode) {
if ( err && /IndexAlreadyExistsException|index_already_exists_exception/im.test(err.message) ) {
winston.info('Elasticsearch index already exists.');
} else if (err) {
winston.error('Elasticsearch create index failed');
winston.error(err);
} else {
winston.info('Elasticsearch create index succeed.');
}
});
};

Elasticsearch.post = {};
Elasticsearch.post.save = function(postData) {
Elasticsearch.post.save = function(obj) {
if (!parseInt(Elasticsearch.config.enabled, 10)) {
return;
}

Elasticsearch.indexPost(postData);
Elasticsearch.indexPost(obj.post);
};

Elasticsearch.post.delete = function(pid, callback) {
Elasticsearch.post.delete = function(obj, callback) {
if (!parseInt(Elasticsearch.config.enabled, 10)) {
return;
}

Elasticsearch.remove(pid);
Elasticsearch.remove(obj.post.pid);

if (typeof callback === 'function') {
if (!parseInt(Elasticsearch.config.enabled, 10)) {
Expand All @@ -499,59 +543,58 @@ Elasticsearch.post.delete = function(pid, callback) {
}
};

Elasticsearch.post.restore = function(postData) {
Elasticsearch.post.restore = function(obj) {
if (!parseInt(Elasticsearch.config.enabled, 10)) {
return;
}

Elasticsearch.indexPost(postData);
Elasticsearch.indexPost(obj.post);
};

Elasticsearch.post.edit = Elasticsearch.post.restore;

Elasticsearch.topic = {};
Elasticsearch.topic.post = function(topicObj) {
Elasticsearch.topic.post = function(obj) {
if (!parseInt(Elasticsearch.config.enabled, 10)) {
return;
}

Elasticsearch.indexTopic(topicObj);
Elasticsearch.indexTopic(obj.topic);
};

Elasticsearch.topic.delete = function(topicData) {
var tid = (void 0 === topicData.tid) ? topicData : topicData.tid;
Elasticsearch.topic.delete = function(obj) {
if (!parseInt(Elasticsearch.config.enabled, 10)) {
return;
}

Elasticsearch.deindexTopic(tid);
Elasticsearch.deindexTopic(obj.topic.tid);
};

Elasticsearch.topic.restore = function(topicObj) {
Elasticsearch.topic.restore = function(obj) {
if (!parseInt(Elasticsearch.config.enabled, 10)) {
return;
}

Elasticsearch.indexTopic(topicObj);
Elasticsearch.indexTopic(obj.topic);
};

Elasticsearch.topic.edit = function(topicObj) {
Elasticsearch.topic.edit = function(obj) {
if (!parseInt(Elasticsearch.config.enabled, 10)) {
return;
}

async.waterfall([
async.apply(posts.getPostFields, topicObj.mainPid, ['pid', 'content']),
async.apply(posts.getPostFields, obj.topic.mainPid, ['pid', 'content']),
Elasticsearch.indexPost,
], function(err, payload) {
if (err) {
return winston.error(err.message);
}
if (!payload) {
return winston.warn('[plugins/elasticsearch] no payload for pid ' + topicObj.mainPid);
return winston.warn('[plugins/elasticsearch] no payload for pid ' + obj.topic.mainPid);
}

payload.title = topicObj.title;
payload.title = obj.topic.title;
Elasticsearch.add(payload);
});
};
Expand Down Expand Up @@ -726,29 +769,29 @@ Elasticsearch.rebuildIndex = function(req, res) {
});
};

Elasticsearch.createIndex = function(callback) {
if (!Elasticsearch.client) {
return callback(new Error('not-connected'));
}

var indexName = Elasticsearch.config.index_name;
if (indexName && 0 < indexName.length) {
Elasticsearch.client.indices.create({
index : Elasticsearch.config.index_name
}, function(err, results){
if (!err) {
callback(null, results);
}
else if ( /IndexAlreadyExistsException/im.test(err.message) ) { // we can ignore if index is already there
winston.info("[plugin/elasticsearch] Ignoring error creating mapping " + err);
callback(null);
}
else {
callback(err);
}
});
}
};
// Elasticsearch.createIndex = function(callback) {
// if (!Elasticsearch.client) {
// return callback(new Error('not-connected'));
// }

// var indexName = Elasticsearch.config.index_name;
// if (indexName && 0 < indexName.length) {
// Elasticsearch.client.indices.create({
// index : Elasticsearch.config.index_name
// }, function(err, results){
// if (!err) {
// callback(null, results);
// }
// else if ( /IndexAlreadyExistsException/im.test(err.message) ) { // we can ignore if index is already there
// winston.info("[plugin/elasticsearch] Ignoring error creating mapping " + err);
// callback(null);
// }
// else {
// callback(err);
// }
// });
// }
// };

Elasticsearch.deleteIndex = function(callback) {
if (!Elasticsearch.client) {
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nodebb-plugin-search-elasticsearch",
"version": "0.2.1",
"version": "0.2.2",
"description": "Full-text searching using Elasticsearch",
"main": "library.js",
"repository": {
Expand Down Expand Up @@ -34,9 +34,11 @@
"dependencies": {
"elasticsearch": "^12.1.3",
"lru-cache": "^2.5.0",
"underscore.string": "^2.4.0"
"underscore": "*",
"underscore.string": "*"

},
"nbbpm": {
"compatibility": "^1.x.x"
"compatibility": "^1.5.x"
}
}
8 changes: 4 additions & 4 deletions templates/admin/plugins/elasticsearch.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
data: {
_csrf: csrf
}
}).success(function() {
}).done(function() {
ajaxify.refresh();

app.alert({
Expand All @@ -152,7 +152,7 @@
_csrf: csrf,
state: parseInt($('button[data-action="toggle"]').attr('data-enabled'), 10) ^ 1
}
}).success(ajaxify.refresh);
}).done(ajaxify.refresh);
});

// Index All event
Expand All @@ -171,7 +171,7 @@
data: {
_csrf: csrf
}
}).success(function() {
}).done(function() {
ajaxify.refresh();

app.alert({
Expand Down Expand Up @@ -205,4 +205,4 @@
});
});
});
</script>
</script>