Skip to content
This repository was archived by the owner on Feb 5, 2024. It is now read-only.

Commit ab70875

Browse files
committed
Added search util
1 parent 5a72250 commit ab70875

File tree

4 files changed

+41
-36
lines changed

4 files changed

+41
-36
lines changed

bin/search.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,20 @@ crowi.init()
9292
.action(function (cmd, env) {
9393
var Page = crowi.model('Page');
9494
var search = crowi.getSearcher();
95-
var keyword = "";
96-
97-
debug(cmd, env);
95+
var keyword = cmd;
9896

9997
search.searchKeyword(keyword, {})
10098
.then(function(data) {
101-
console.log(colors.green('Search result: %d of %d total.'), data.meta.results, data.meta.total);
99+
debug('result is', data);
100+
console.log(colors.green('Search result: %d of %d total. (%d ms)'), data.meta.results, data.meta.total, data.meta.took);
102101

103-
return Page.findListByPageIds(data.data.map(function(p) { return p._id; }));
102+
return Page.populatePageListToAnyObjects(data.data);
104103
}).then(function(pages) {
105104
pages.map(function(page) {
106-
console.log(page.path);
105+
console.log(page._score, page._id, page.path);
107106
});
107+
108+
process.exit(0);
108109
})
109110
.catch(function(err) {
110111
console.error('Error', err);

lib/models/page.js

+18-23
Original file line numberDiff line numberDiff line change
@@ -248,35 +248,30 @@ module.exports = function(crowi) {
248248
});
249249
};
250250

251-
pageSchema.statics.populatePageList = function(pageList) {
252-
var Page = self;
253-
var User = crowi.model('User');
251+
pageSchema.statics.populatePageListToAnyObjects = function(pageIdObjectArray) {
252+
var Page = this;
253+
var pageIdMappings = {};
254+
var pageIds = pageIdObjectArray.map(function(page, idx) {
255+
if (!page._id) {
256+
throw new Error('Pass the arg of populatePageListToAnyObjects() must have _id on each element.');
257+
}
254258

255-
return new Promise(function(resolve, reject) {
256-
Page.populate(
257-
pageList,
258-
[
259-
{path: 'creator', model: 'User', select: User.USER_PUBLIC_FIELDS},
260-
{path: 'revision', model: 'Revision'}
261-
],
262-
function(err, pageList) {
263-
if (err) {
264-
return reject(err);
265-
}
259+
pageIdMappings[String(page._id)] = idx;
260+
return page._id;
261+
});
266262

267-
Page.populate(pageList, {path: 'revision.author', model: 'User', select: User.USER_PUBLIC_FIELDS}, function(err, data) {
268-
if (err) {
269-
return reject(err);
270-
}
263+
return new Promise(function(resolve, reject) {
264+
Page.findListByPageIds(pageIds, {limit: 100}) // limit => if the pagIds is greater than 100, ignore
265+
.then(function(pages) {
266+
pages.forEach(function(page) {
267+
Object.assign(pageIdObjectArray[pageIdMappings[String(page._id)]], page._doc);
268+
});
271269

272-
resolve(data);
273-
});
274-
}
275-
);
270+
resolve(pageIdObjectArray);
271+
});
276272
});
277273
};
278274

279-
280275
pageSchema.statics.updateCommentCount = function (page, num)
281276
{
282277
var self = this;

lib/util/search.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ function SearchClient(crowi, esUri) {
1818

1919
this.client = new elasticsearch.Client({
2020
host: this.host,
21+
requestTimeout: 5000,
2122
});
2223

2324
this.mappingFile = crowi.resourceDir + 'search/mappings.json';
@@ -94,6 +95,7 @@ SearchClient.prototype.prepareBodyForCreate = function(body, page) {
9495
body: page.revision.body,
9596
username: page.creator.username,
9697
comment_count: page.commentCount,
98+
bookmark_count: 0, // todo
9799
like_count: page.liker.length || 0,
98100
created_at: page.createdAt,
99101
updated_at: page.updatedAt,
@@ -180,12 +182,16 @@ SearchClient.prototype.search = function(query)
180182
return new Promise(function(resolve, reject) {
181183
self.client.search(query)
182184
.then(function(data) {
183-
var result = {};
184-
result.total = data.hits.total;
185-
result.results = data.hits.hits.length;
186-
result.data = data.hits.hits.map(function(elm) {
187-
return {_id: elm._id, _score: elm._score};
188-
});
185+
var result = {
186+
meta: {
187+
took: data.took,
188+
total: data.hits.total,
189+
results: data.hits.hits.length,
190+
},
191+
data: data.hits.hits.map(function(elm) {
192+
return {_id: elm._id, _score: elm._score};
193+
})
194+
};
189195

190196
resolve(result);
191197
}).catch(function(err) {
@@ -260,7 +266,7 @@ SearchClient.prototype.appendCriteriaForKeywordContains = function(query, keywor
260266
multi_match: {
261267
query: keyword,
262268
fields: [
263-
"path.ja",
269+
"path.ja^2", // ためしに。
264270
"body.ja"
265271
],
266272
operator: "and"

resource/search/mappings.json

+3
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767
"comment_count": {
6868
"type": "integer"
6969
},
70+
"bookmark_count": {
71+
"type": "integer"
72+
},
7073
"like_count": {
7174
"type": "integer"
7275
},

0 commit comments

Comments
 (0)