|
|
@@ -343,8 +343,6 @@ SearchClient.prototype.addAllPages = async function() {
|
|
|
* }
|
|
|
*/
|
|
|
SearchClient.prototype.search = async function(query) {
|
|
|
- let self = this;
|
|
|
-
|
|
|
// for debug
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
|
const result = await this.client.indices.validateQuery({
|
|
|
@@ -356,28 +354,19 @@ SearchClient.prototype.search = async function(query) {
|
|
|
logger.info('ES returns explanations: ', result.explanations);
|
|
|
}
|
|
|
|
|
|
- return new Promise(function(resolve, reject) {
|
|
|
- self.client
|
|
|
- .search(query)
|
|
|
- .then(function(data) {
|
|
|
- let result = {
|
|
|
- meta: {
|
|
|
- took: data.took,
|
|
|
- total: data.hits.total,
|
|
|
- results: data.hits.hits.length,
|
|
|
- },
|
|
|
- data: data.hits.hits.map(function(elm) {
|
|
|
- return { _id: elm._id, _score: elm._score, _source: elm._source };
|
|
|
- }),
|
|
|
- };
|
|
|
-
|
|
|
- resolve(result);
|
|
|
- })
|
|
|
- .catch(function(err) {
|
|
|
- logger.error('Search error', err);
|
|
|
- reject(err);
|
|
|
- });
|
|
|
- });
|
|
|
+ const result = await this.client.search(query);
|
|
|
+
|
|
|
+ return {
|
|
|
+ meta: {
|
|
|
+ took: result.took,
|
|
|
+ total: result.hits.total,
|
|
|
+ results: result.hits.hits.length,
|
|
|
+ },
|
|
|
+ data: result.hits.hits.map(function(elm) {
|
|
|
+ return { _id: elm._id, _score: elm._score, _source: elm._source };
|
|
|
+ }),
|
|
|
+ };
|
|
|
+
|
|
|
};
|
|
|
|
|
|
SearchClient.prototype.createSearchQuerySortedByUpdatedAt = function(option) {
|
|
|
@@ -538,7 +527,21 @@ SearchClient.prototype.appendCriteriaForPathFilter = function(query, path) {
|
|
|
});
|
|
|
};
|
|
|
|
|
|
-SearchClient.prototype.filterPagesByViewer = function(query, user, userGroups) {
|
|
|
+SearchClient.prototype.filterPagesByViewer = async function(query, user, userGroups) {
|
|
|
+ const Config = this.crowi.model('Config');
|
|
|
+ const config = this.crowi.getConfig();
|
|
|
+
|
|
|
+ // determine User condition
|
|
|
+ const hidePagesRestrictedByOwner = Config.hidePagesRestrictedByOwnerInList(config);
|
|
|
+ user = hidePagesRestrictedByOwner ? user : null;
|
|
|
+
|
|
|
+ // determine UserGroup condition
|
|
|
+ const hidePagesRestrictedByGroup = Config.hidePagesRestrictedByGroupInList(config);
|
|
|
+ if (hidePagesRestrictedByGroup && user != null) {
|
|
|
+ const UserGroupRelation = this.crowi.model('UserGroupRelation');
|
|
|
+ userGroups = await UserGroupRelation.findAllUserGroupIdsRelatedToUser(user);
|
|
|
+ }
|
|
|
+
|
|
|
query = this.initializeBoolQuery(query);
|
|
|
|
|
|
const Page = this.crowi.model('Page');
|
|
|
@@ -578,7 +581,12 @@ SearchClient.prototype.filterPagesByViewer = function(query, user, userGroups) {
|
|
|
);
|
|
|
}
|
|
|
|
|
|
- if (userGroups != null && userGroups.length > 0) {
|
|
|
+ if (userGroups == null) {
|
|
|
+ grantConditions.push(
|
|
|
+ { term: { grant: GRANT_USER_GROUP } },
|
|
|
+ );
|
|
|
+ }
|
|
|
+ else if (userGroups.length > 0) {
|
|
|
const userGroupIds = userGroups.map(group => group._id.toString() );
|
|
|
grantConditions.push(
|
|
|
{ bool: {
|
|
|
@@ -646,7 +654,7 @@ SearchClient.prototype.appendFunctionScore = function(query) {
|
|
|
};
|
|
|
};
|
|
|
|
|
|
-SearchClient.prototype.searchKeyword = function(keyword, user, userGroups, option) {
|
|
|
+SearchClient.prototype.searchKeyword = async function(keyword, user, userGroups, option) {
|
|
|
const from = option.offset || null;
|
|
|
const size = option.limit || null;
|
|
|
const type = option.type || null;
|
|
|
@@ -654,7 +662,7 @@ SearchClient.prototype.searchKeyword = function(keyword, user, userGroups, optio
|
|
|
this.appendCriteriaForKeywordContains(query, keyword);
|
|
|
|
|
|
this.filterPagesByType(query, type);
|
|
|
- this.filterPagesByViewer(query, user, userGroups);
|
|
|
+ await this.filterPagesByViewer(query, user, userGroups);
|
|
|
|
|
|
this.appendResultSize(query, from, size);
|
|
|
|
|
|
@@ -663,11 +671,11 @@ SearchClient.prototype.searchKeyword = function(keyword, user, userGroups, optio
|
|
|
return this.search(query);
|
|
|
};
|
|
|
|
|
|
-SearchClient.prototype.searchByPath = function(keyword, prefix) {
|
|
|
+SearchClient.prototype.searchByPath = async function(keyword, prefix) {
|
|
|
// TODO path 名だけから検索
|
|
|
};
|
|
|
|
|
|
-SearchClient.prototype.searchKeywordUnderPath = function(keyword, path, user, userGroups, option) {
|
|
|
+SearchClient.prototype.searchKeywordUnderPath = async function(keyword, path, user, userGroups, option) {
|
|
|
const from = option.offset || null;
|
|
|
const size = option.limit || null;
|
|
|
const type = option.type || null;
|
|
|
@@ -676,7 +684,7 @@ SearchClient.prototype.searchKeywordUnderPath = function(keyword, path, user, us
|
|
|
this.appendCriteriaForPathFilter(query, path);
|
|
|
|
|
|
this.filterPagesByType(query, type);
|
|
|
- this.filterPagesByViewer(query, user, userGroups);
|
|
|
+ await this.filterPagesByViewer(query, user, userGroups);
|
|
|
|
|
|
this.appendResultSize(query, from, size);
|
|
|
|