From 3c8b8bfb3c75fa695b340ea6a1e0f6fb75b965fa Mon Sep 17 00:00:00 2001 From: river Date: Fri, 31 Mar 2017 18:57:11 +0800 Subject: [PATCH 1/8] fix search query is empty problems --- library.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/library.js b/library.js index bf78cf4..61509aa 100644 --- a/library.js +++ b/library.js @@ -208,6 +208,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) }; From fd4a727d6bc9798ecca2132455d0426afb560a4c Mon Sep 17 00:00:00 2001 From: Revir Yang Date: Fri, 14 Jul 2017 22:33:17 +0800 Subject: [PATCH 2/8] 1.5.x compability --- library.js | 33 ++++++++++++++++----------------- package.json | 2 +- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/library.js b/library.js index 61509aa..24bb9f2 100644 --- a/library.js +++ b/library.js @@ -478,20 +478,20 @@ Elasticsearch.flush = function(req, res) { }; 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)) { @@ -502,59 +502,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); }); }; diff --git a/package.json b/package.json index 8a44b7d..85c37d2 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,6 @@ "underscore.string": "^2.4.0" }, "nbbpm": { - "compatibility": "^1.x.x" + "compatibility": "^1.5.x" } } From 6fd9168c21855cb6c9bacc79b1631318f9b49c15 Mon Sep 17 00:00:00 2001 From: river Date: Fri, 11 Aug 2017 10:20:20 +0800 Subject: [PATCH 3/8] underscore dependency --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 85c37d2..9ba2cb8 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "dependencies": { "elasticsearch": "^12.1.3", "lru-cache": "^2.5.0", - "underscore.string": "^2.4.0" + "underscore": "*" }, "nbbpm": { "compatibility": "^1.5.x" From 9072082052539ba8d839f36dfc40395a47008cd4 Mon Sep 17 00:00:00 2001 From: river Date: Fri, 11 Aug 2017 13:34:54 +0800 Subject: [PATCH 4/8] fix underscore dependency" --- library.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.js b/library.js index 24bb9f2..1b7bb83 100644 --- a/library.js +++ b/library.js @@ -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 From 75b1c1912b962a1b497723a461358dc12757da67 Mon Sep 17 00:00:00 2001 From: river Date: Sat, 12 Aug 2017 16:48:43 +0800 Subject: [PATCH 5/8] fix underscore dependency" --- package.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 9ba2cb8..83f72ac 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,9 @@ "dependencies": { "elasticsearch": "^12.1.3", "lru-cache": "^2.5.0", - "underscore": "*" + "underscore": "*", + "underscore.string": "*" + }, "nbbpm": { "compatibility": "^1.5.x" From bc496e2e75462730f2aa9ec6ed433f29bdc5ac77 Mon Sep 17 00:00:00 2001 From: river Date: Fri, 18 Aug 2017 08:05:41 +0800 Subject: [PATCH 6/8] createIndex --- library.js | 88 ++++++++++++++++------- package.json | 2 +- templates/admin/plugins/elasticsearch.tpl | 8 +-- 3 files changed, 68 insertions(+), 30 deletions(-) diff --git a/library.js b/library.js index 1b7bb83..b598631 100644 --- a/library.js +++ b/library.js @@ -190,6 +190,8 @@ Elasticsearch.connect = function() { delete Elasticsearch.config.host; Elasticsearch.client = new elasticsearch.Client(Elasticsearch.config); + + Elasticsearch.createIndex(); }; Elasticsearch.adminMenu = function(custom_header, callback) { @@ -242,12 +244,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') { @@ -477,6 +479,42 @@ 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(obj) { if (!parseInt(Elasticsearch.config.enabled, 10)) { @@ -728,29 +766,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) { diff --git a/package.json b/package.json index 83f72ac..2dba705 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/templates/admin/plugins/elasticsearch.tpl b/templates/admin/plugins/elasticsearch.tpl index 1114277..c2310ba 100644 --- a/templates/admin/plugins/elasticsearch.tpl +++ b/templates/admin/plugins/elasticsearch.tpl @@ -129,7 +129,7 @@ data: { _csrf: csrf } - }).success(function() { + }).done(function() { ajaxify.refresh(); app.alert({ @@ -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 @@ -171,7 +171,7 @@ data: { _csrf: csrf } - }).success(function() { + }).done(function() { ajaxify.refresh(); app.alert({ @@ -205,4 +205,4 @@ }); }); }); - \ No newline at end of file + From 0f202dd485527319d7a3aca7e43dbfe760454fa6 Mon Sep 17 00:00:00 2001 From: river Date: Fri, 18 Aug 2017 18:58:41 +0800 Subject: [PATCH 7/8] update --- library.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/library.js b/library.js index b598631..7be73a7 100644 --- a/library.js +++ b/library.js @@ -165,7 +165,8 @@ Elasticsearch.getTopicCount = function(callback) { }; Elasticsearch.connect = function() { - if (!Elasticsearch.config.host) { + var originalHost = Elasticsearch.config.host || Elasticsearch.originalHost; + if (!originalHost) { return; } @@ -174,7 +175,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. @@ -187,6 +188,7 @@ 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); From fa9782ee4d4ed380cc3411c322bb12cb3a39c316 Mon Sep 17 00:00:00 2001 From: river Date: Fri, 18 Aug 2017 20:42:26 +0800 Subject: [PATCH 8/8] fix client --- library.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/library.js b/library.js index 7be73a7..9606da2 100644 --- a/library.js +++ b/library.js @@ -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 = { /* @@ -191,7 +189,10 @@ Elasticsearch.connect = function() { 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(); };