search.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. /**
  2. * Search
  3. */
  4. const elasticsearch = require('elasticsearch');
  5. const debug = require('debug')('growi:lib:search');
  6. const logger = require('@alias/logger')('growi:lib:search');
  7. function SearchClient(crowi, esUri) {
  8. this.DEFAULT_OFFSET = 0;
  9. this.DEFAULT_LIMIT = 50;
  10. this.esNodeName = '-';
  11. this.esNodeNames = [];
  12. this.esVersion = 'unknown';
  13. this.esVersions = [];
  14. this.esPlugin = [];
  15. this.esPlugins = [];
  16. this.esUri = esUri;
  17. this.crowi = crowi;
  18. this.searchEvent = crowi.event('search');
  19. // In Elasticsearch RegExp, we don't need to used ^ and $.
  20. // Ref: https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-regexp-query.html#_standard_operators
  21. this.queries = {
  22. PORTAL: {
  23. regexp: {
  24. 'path.raw': '.*/',
  25. },
  26. },
  27. PUBLIC: {
  28. regexp: {
  29. 'path.raw': '.*[^/]',
  30. },
  31. },
  32. USER: {
  33. prefix: {
  34. 'path.raw': '/user/',
  35. },
  36. },
  37. };
  38. const uri = this.parseUri(this.esUri);
  39. this.host = uri.host;
  40. this.indexName = uri.indexName;
  41. this.client = new elasticsearch.Client({
  42. host: this.host,
  43. requestTimeout: 5000,
  44. // log: 'debug',
  45. });
  46. this.registerUpdateEvent();
  47. this.mappingFile = `${crowi.resourceDir}search/mappings.json`;
  48. }
  49. SearchClient.prototype.getInfo = function() {
  50. return this.client.info({});
  51. };
  52. SearchClient.prototype.checkESVersion = async function() {
  53. try {
  54. const nodes = await this.client.nodes.info();
  55. if (!nodes._nodes || !nodes.nodes) {
  56. throw new Error('no nodes info');
  57. }
  58. for (const [nodeName, nodeInfo] of Object.entries(nodes.nodes)) {
  59. this.esNodeName = nodeName;
  60. this.esNodeNames.push(nodeName);
  61. this.esVersion = nodeInfo.version;
  62. this.esVersions.push(nodeInfo.version);
  63. this.esPlugin = nodeInfo.plugins;
  64. this.esPlugins.push(nodeInfo.plugins);
  65. }
  66. }
  67. catch (error) {
  68. logger.error('es check version error:', error);
  69. }
  70. };
  71. SearchClient.prototype.registerUpdateEvent = function() {
  72. const pageEvent = this.crowi.event('page');
  73. pageEvent.on('create', this.syncPageCreated.bind(this));
  74. pageEvent.on('update', this.syncPageUpdated.bind(this));
  75. pageEvent.on('updateTag', this.syncPageUpdated.bind(this));
  76. pageEvent.on('delete', this.syncPageDeleted.bind(this));
  77. const bookmarkEvent = this.crowi.event('bookmark');
  78. bookmarkEvent.on('create', this.syncBookmarkChanged.bind(this));
  79. bookmarkEvent.on('delete', this.syncBookmarkChanged.bind(this));
  80. const tagEvent = this.crowi.event('tag');
  81. tagEvent.on('update', this.syncTagChanged.bind(this));
  82. };
  83. SearchClient.prototype.shouldIndexed = function(page) {
  84. return (page.redirectTo == null);
  85. };
  86. // BONSAI_URL is following format:
  87. // => https://{ID}:{PASSWORD}@{HOST}
  88. SearchClient.prototype.parseUri = function(uri) {
  89. let indexName = 'crowi';
  90. let host = uri;
  91. const match = uri.match(/^(https?:\/\/[^/]+)\/(.+)$/);
  92. if (match) {
  93. host = match[1];
  94. indexName = match[2];
  95. }
  96. return {
  97. host,
  98. indexName,
  99. };
  100. };
  101. SearchClient.prototype.buildIndex = function(uri) {
  102. return this.client.indices.create({
  103. index: this.indexName,
  104. body: require(this.mappingFile),
  105. });
  106. };
  107. SearchClient.prototype.deleteIndex = function(uri) {
  108. return this.client.indices.delete({
  109. index: this.indexName,
  110. });
  111. };
  112. /**
  113. * generate object that is related to page.grant*
  114. */
  115. function generateDocContentsRelatedToRestriction(page) {
  116. let grantedUserIds = null;
  117. if (page.grantedUsers != null && page.grantedUsers.length > 0) {
  118. grantedUserIds = page.grantedUsers.map((user) => {
  119. const userId = (user._id == null) ? user : user._id;
  120. return userId.toString();
  121. });
  122. }
  123. let grantedGroupId = null;
  124. if (page.grantedGroup != null) {
  125. const groupId = (page.grantedGroup._id == null) ? page.grantedGroup : page.grantedGroup._id;
  126. grantedGroupId = groupId.toString();
  127. }
  128. return {
  129. grant: page.grant,
  130. granted_users: grantedUserIds,
  131. granted_group: grantedGroupId,
  132. };
  133. }
  134. SearchClient.prototype.prepareBodyForUpdate = function(body, page) {
  135. if (!Array.isArray(body)) {
  136. throw new Error('Body must be an array.');
  137. }
  138. const command = {
  139. update: {
  140. _index: this.indexName,
  141. _type: 'pages',
  142. _id: page._id.toString(),
  143. },
  144. };
  145. let document = {
  146. path: page.path,
  147. body: page.revision.body,
  148. comment_count: page.commentCount,
  149. bookmark_count: page.bookmarkCount || 0,
  150. like_count: page.liker.length || 0,
  151. updated_at: page.updatedAt,
  152. tag_names: page.tagNames,
  153. };
  154. document = Object.assign(document, generateDocContentsRelatedToRestriction(page));
  155. body.push(command);
  156. body.push({
  157. doc: document,
  158. doc_as_upsert: true,
  159. });
  160. };
  161. SearchClient.prototype.prepareBodyForCreate = function(body, page) {
  162. if (!Array.isArray(body)) {
  163. throw new Error('Body must be an array.');
  164. }
  165. const command = {
  166. index: {
  167. _index: this.indexName,
  168. _type: 'pages',
  169. _id: page._id.toString(),
  170. },
  171. };
  172. const bookmarkCount = page.bookmarkCount || 0;
  173. let document = {
  174. path: page.path,
  175. body: page.revision.body,
  176. username: page.creator.username,
  177. comment_count: page.commentCount,
  178. bookmark_count: bookmarkCount,
  179. like_count: page.liker.length || 0,
  180. created_at: page.createdAt,
  181. updated_at: page.updatedAt,
  182. tag_names: page.tagNames,
  183. };
  184. document = Object.assign(document, generateDocContentsRelatedToRestriction(page));
  185. body.push(command);
  186. body.push(document);
  187. };
  188. SearchClient.prototype.prepareBodyForDelete = function(body, page) {
  189. if (!Array.isArray(body)) {
  190. throw new Error('Body must be an array.');
  191. }
  192. const command = {
  193. delete: {
  194. _index: this.indexName,
  195. _type: 'pages',
  196. _id: page._id.toString(),
  197. },
  198. };
  199. body.push(command);
  200. };
  201. SearchClient.prototype.addPages = async function(pages) {
  202. const Bookmark = this.crowi.model('Bookmark');
  203. const PageTagRelation = this.crowi.model('PageTagRelation');
  204. const body = [];
  205. /* eslint-disable no-await-in-loop */
  206. for (const page of pages) {
  207. page.bookmarkCount = await Bookmark.countByPageId(page._id);
  208. const tagRelations = await PageTagRelation.find({ relatedPage: page._id }).populate('relatedTag');
  209. page.tagNames = tagRelations.map((relation) => { return relation.relatedTag.name });
  210. this.prepareBodyForCreate(body, page);
  211. }
  212. /* eslint-enable no-await-in-loop */
  213. logger.debug('addPages(): Sending Request to ES', body);
  214. return this.client.bulk({
  215. body,
  216. });
  217. };
  218. SearchClient.prototype.updatePages = async function(pages) {
  219. const self = this;
  220. const PageTagRelation = this.crowi.model('PageTagRelation');
  221. const body = [];
  222. /* eslint-disable no-await-in-loop */
  223. for (const page of pages) {
  224. const tagRelations = await PageTagRelation.find({ relatedPage: page._id }).populate('relatedTag');
  225. page.tagNames = tagRelations.map((relation) => { return relation.relatedTag.name });
  226. self.prepareBodyForUpdate(body, page);
  227. }
  228. logger.debug('updatePages(): Sending Request to ES', body);
  229. return this.client.bulk({
  230. body,
  231. });
  232. };
  233. SearchClient.prototype.deletePages = function(pages) {
  234. const self = this;
  235. const body = [];
  236. pages.map((page) => {
  237. self.prepareBodyForDelete(body, page);
  238. return;
  239. });
  240. logger.debug('deletePages(): Sending Request to ES', body);
  241. return this.client.bulk({
  242. body,
  243. });
  244. };
  245. SearchClient.prototype.addAllPages = async function() {
  246. const self = this;
  247. const Page = this.crowi.model('Page');
  248. const allPageCount = await Page.allPageCount();
  249. const Bookmark = this.crowi.model('Bookmark');
  250. const PageTagRelation = this.crowi.model('PageTagRelation');
  251. const cursor = Page.getStreamOfFindAll();
  252. let body = [];
  253. let sent = 0;
  254. let skipped = 0;
  255. let total = 0;
  256. return new Promise((resolve, reject) => {
  257. const bulkSend = (body) => {
  258. self.client
  259. .bulk({
  260. body,
  261. requestTimeout: Infinity,
  262. })
  263. .then((res) => {
  264. logger.info('addAllPages add anyway (items, errors, took): ', (res.items || []).length, res.errors, res.took, 'ms');
  265. })
  266. .catch((err) => {
  267. logger.error('addAllPages error on add anyway: ', err);
  268. });
  269. };
  270. cursor
  271. .eachAsync(async(doc) => {
  272. if (!doc.creator || !doc.revision || !self.shouldIndexed(doc)) {
  273. // debug('Skipped', doc.path);
  274. skipped++;
  275. return;
  276. }
  277. total++;
  278. const bookmarkCount = await Bookmark.countByPageId(doc._id);
  279. const tagRelations = await PageTagRelation.find({ relatedPage: doc._id }).populate('relatedTag');
  280. const page = { ...doc, bookmarkCount, tagNames: tagRelations.map((relation) => { return relation.relatedTag.name }) };
  281. self.prepareBodyForCreate(body, page);
  282. if (body.length >= 4000) {
  283. // send each 2000 docs. (body has 2 elements for each data)
  284. sent++;
  285. logger.debug('Sending request (seq, total, skipped)', sent, total, skipped);
  286. bulkSend(body);
  287. this.searchEvent.emit('addPageProgress', allPageCount, total, skipped);
  288. body = [];
  289. }
  290. })
  291. .then(() => {
  292. // send all remaining data on body[]
  293. logger.debug('Sending last body of bulk operation:', body.length);
  294. bulkSend(body);
  295. this.searchEvent.emit('finishAddPage', allPageCount, total, skipped);
  296. resolve();
  297. })
  298. .catch((e) => {
  299. logger.error('Error wile iterating cursor.eachAsync()', e);
  300. reject(e);
  301. });
  302. });
  303. };
  304. /**
  305. * search returning type:
  306. * {
  307. * meta: { total: Integer, results: Integer},
  308. * data: [ pages ...],
  309. * }
  310. */
  311. SearchClient.prototype.search = async function(query) {
  312. // for debug
  313. if (process.env.NODE_ENV === 'development') {
  314. const result = await this.client.indices.validateQuery({
  315. explain: true,
  316. body: {
  317. query: query.body.query,
  318. },
  319. });
  320. logger.debug('ES returns explanations: ', result.explanations);
  321. }
  322. const result = await this.client.search(query);
  323. // for debug
  324. logger.debug('ES result: ', result);
  325. return {
  326. meta: {
  327. took: result.took,
  328. total: result.hits.total,
  329. results: result.hits.hits.length,
  330. },
  331. data: result.hits.hits.map((elm) => {
  332. return { _id: elm._id, _score: elm._score, _source: elm._source };
  333. }),
  334. };
  335. };
  336. SearchClient.prototype.createSearchQuerySortedByUpdatedAt = function(option) {
  337. // getting path by default is almost for debug
  338. let fields = ['path', 'bookmark_count', 'tag_names'];
  339. if (option) {
  340. fields = option.fields || fields;
  341. }
  342. // default is only id field, sorted by updated_at
  343. const query = {
  344. index: this.indexName,
  345. type: 'pages',
  346. body: {
  347. sort: [{ updated_at: { order: 'desc' } }],
  348. query: {}, // query
  349. _source: fields,
  350. },
  351. };
  352. this.appendResultSize(query);
  353. return query;
  354. };
  355. SearchClient.prototype.createSearchQuerySortedByScore = function(option) {
  356. let fields = ['path', 'bookmark_count', 'tag_names'];
  357. if (option) {
  358. fields = option.fields || fields;
  359. }
  360. // sort by score
  361. const query = {
  362. index: this.indexName,
  363. type: 'pages',
  364. body: {
  365. sort: [{ _score: { order: 'desc' } }],
  366. query: {}, // query
  367. _source: fields,
  368. },
  369. };
  370. this.appendResultSize(query);
  371. return query;
  372. };
  373. SearchClient.prototype.appendResultSize = function(query, from, size) {
  374. query.from = from || this.DEFAULT_OFFSET;
  375. query.size = size || this.DEFAULT_LIMIT;
  376. };
  377. SearchClient.prototype.initializeBoolQuery = function(query) {
  378. // query is created by createSearchQuerySortedByScore() or createSearchQuerySortedByUpdatedAt()
  379. if (!query.body.query.bool) {
  380. query.body.query.bool = {};
  381. }
  382. const isInitialized = (query) => { return !!query && Array.isArray(query) };
  383. if (!isInitialized(query.body.query.bool.filter)) {
  384. query.body.query.bool.filter = [];
  385. }
  386. if (!isInitialized(query.body.query.bool.must)) {
  387. query.body.query.bool.must = [];
  388. }
  389. if (!isInitialized(query.body.query.bool.must_not)) {
  390. query.body.query.bool.must_not = [];
  391. }
  392. return query;
  393. };
  394. SearchClient.prototype.appendCriteriaForQueryString = function(query, queryString) {
  395. query = this.initializeBoolQuery(query); // eslint-disable-line no-param-reassign
  396. // parse
  397. const parsedKeywords = this.parseQueryString(queryString);
  398. if (parsedKeywords.match.length > 0) {
  399. const q = {
  400. multi_match: {
  401. query: parsedKeywords.match.join(' '),
  402. type: 'most_fields',
  403. fields: ['path.ja^2', 'path.en^2', 'body.ja', 'body.en'],
  404. },
  405. };
  406. query.body.query.bool.must.push(q);
  407. }
  408. if (parsedKeywords.not_match.length > 0) {
  409. const q = {
  410. multi_match: {
  411. query: parsedKeywords.not_match.join(' '),
  412. fields: ['path.ja', 'path.en', 'body.ja', 'body.en'],
  413. operator: 'or',
  414. },
  415. };
  416. query.body.query.bool.must_not.push(q);
  417. }
  418. if (parsedKeywords.phrase.length > 0) {
  419. const phraseQueries = [];
  420. parsedKeywords.phrase.forEach((phrase) => {
  421. phraseQueries.push({
  422. multi_match: {
  423. query: phrase, // each phrase is quoteted words
  424. type: 'phrase',
  425. fields: [
  426. // Not use "*.ja" fields here, because we want to analyze (parse) search words
  427. 'path.raw^2',
  428. 'body',
  429. ],
  430. },
  431. });
  432. });
  433. query.body.query.bool.must.push(phraseQueries);
  434. }
  435. if (parsedKeywords.not_phrase.length > 0) {
  436. const notPhraseQueries = [];
  437. parsedKeywords.not_phrase.forEach((phrase) => {
  438. notPhraseQueries.push({
  439. multi_match: {
  440. query: phrase, // each phrase is quoteted words
  441. type: 'phrase',
  442. fields: [
  443. // Not use "*.ja" fields here, because we want to analyze (parse) search words
  444. 'path.raw^2',
  445. 'body',
  446. ],
  447. },
  448. });
  449. });
  450. query.body.query.bool.must_not.push(notPhraseQueries);
  451. }
  452. if (parsedKeywords.prefix.length > 0) {
  453. const queries = parsedKeywords.prefix.map((path) => {
  454. return { prefix: { 'path.raw': path } };
  455. });
  456. query.body.query.bool.filter.push({ bool: { should: queries } });
  457. }
  458. if (parsedKeywords.not_prefix.length > 0) {
  459. const queries = parsedKeywords.not_prefix.map((path) => {
  460. return { prefix: { 'path.raw': path } };
  461. });
  462. query.body.query.bool.filter.push({ bool: { must_not: queries } });
  463. }
  464. if (parsedKeywords.tag.length > 0) {
  465. const queries = parsedKeywords.tag.map((tag) => {
  466. return { term: { tag_names: tag } };
  467. });
  468. query.body.query.bool.filter.push({ bool: { must: queries } });
  469. }
  470. if (parsedKeywords.not_tag.length > 0) {
  471. const queries = parsedKeywords.not_tag.map((tag) => {
  472. return { term: { tag_names: tag } };
  473. });
  474. query.body.query.bool.filter.push({ bool: { must_not: queries } });
  475. }
  476. };
  477. SearchClient.prototype.filterPagesByViewer = async function(query, user, userGroups) {
  478. const Config = this.crowi.model('Config');
  479. const config = this.crowi.getConfig();
  480. const showPagesRestrictedByOwner = !Config.hidePagesRestrictedByOwnerInList(config);
  481. const showPagesRestrictedByGroup = !Config.hidePagesRestrictedByGroupInList(config);
  482. query = this.initializeBoolQuery(query); // eslint-disable-line no-param-reassign
  483. const Page = this.crowi.model('Page');
  484. const {
  485. GRANT_PUBLIC, GRANT_RESTRICTED, GRANT_SPECIFIED, GRANT_OWNER, GRANT_USER_GROUP,
  486. } = Page;
  487. const grantConditions = [
  488. { term: { grant: GRANT_PUBLIC } },
  489. ];
  490. // ensure to hit to GRANT_RESTRICTED pages that the user specified at own
  491. if (user != null) {
  492. grantConditions.push(
  493. {
  494. bool: {
  495. must: [
  496. { term: { grant: GRANT_RESTRICTED } },
  497. { term: { granted_users: user._id.toString() } },
  498. ],
  499. },
  500. },
  501. );
  502. }
  503. if (showPagesRestrictedByOwner) {
  504. grantConditions.push(
  505. { term: { grant: GRANT_SPECIFIED } },
  506. { term: { grant: GRANT_OWNER } },
  507. );
  508. }
  509. else if (user != null) {
  510. grantConditions.push(
  511. {
  512. bool: {
  513. must: [
  514. { term: { grant: GRANT_SPECIFIED } },
  515. { term: { granted_users: user._id.toString() } },
  516. ],
  517. },
  518. },
  519. {
  520. bool: {
  521. must: [
  522. { term: { grant: GRANT_OWNER } },
  523. { term: { granted_users: user._id.toString() } },
  524. ],
  525. },
  526. },
  527. );
  528. }
  529. if (showPagesRestrictedByGroup) {
  530. grantConditions.push(
  531. { term: { grant: GRANT_USER_GROUP } },
  532. );
  533. }
  534. else if (userGroups != null && userGroups.length > 0) {
  535. const userGroupIds = userGroups.map((group) => { return group._id.toString() });
  536. grantConditions.push(
  537. {
  538. bool: {
  539. must: [
  540. { term: { grant: GRANT_USER_GROUP } },
  541. { terms: { granted_group: userGroupIds } },
  542. ],
  543. },
  544. },
  545. );
  546. }
  547. query.body.query.bool.filter.push({ bool: { should: grantConditions } });
  548. };
  549. SearchClient.prototype.filterPortalPages = function(query) {
  550. query = this.initializeBoolQuery(query); // eslint-disable-line no-param-reassign
  551. query.body.query.bool.must_not.push(this.queries.USER);
  552. query.body.query.bool.filter.push(this.queries.PORTAL);
  553. };
  554. SearchClient.prototype.filterPublicPages = function(query) {
  555. query = this.initializeBoolQuery(query); // eslint-disable-line no-param-reassign
  556. query.body.query.bool.must_not.push(this.queries.USER);
  557. query.body.query.bool.filter.push(this.queries.PUBLIC);
  558. };
  559. SearchClient.prototype.filterUserPages = function(query) {
  560. query = this.initializeBoolQuery(query); // eslint-disable-line no-param-reassign
  561. query.body.query.bool.filter.push(this.queries.USER);
  562. };
  563. SearchClient.prototype.filterPagesByType = function(query, type) {
  564. const Page = this.crowi.model('Page');
  565. switch (type) {
  566. case Page.TYPE_PORTAL:
  567. return this.filterPortalPages(query);
  568. case Page.TYPE_PUBLIC:
  569. return this.filterPublicPages(query);
  570. case Page.TYPE_USER:
  571. return this.filterUserPages(query);
  572. default:
  573. return query;
  574. }
  575. };
  576. SearchClient.prototype.appendFunctionScore = function(query, queryString) {
  577. const User = this.crowi.model('User');
  578. const count = User.count({}) || 1;
  579. const minScore = queryString.length * 0.1 - 1; // increase with length
  580. logger.debug('min_score: ', minScore);
  581. query.body.query = {
  582. function_score: {
  583. query: { ...query.body.query },
  584. // // disable min_score -- 2019.02.28 Yuki Takei
  585. // // more precise adjustment is needed...
  586. // min_score: minScore,
  587. field_value_factor: {
  588. field: 'bookmark_count',
  589. modifier: 'log1p',
  590. factor: 10000 / count,
  591. missing: 0,
  592. },
  593. boost_mode: 'sum',
  594. },
  595. };
  596. };
  597. SearchClient.prototype.searchKeyword = async function(queryString, user, userGroups, option) {
  598. const from = option.offset || null;
  599. const size = option.limit || null;
  600. const type = option.type || null;
  601. const query = this.createSearchQuerySortedByScore();
  602. this.appendCriteriaForQueryString(query, queryString);
  603. this.filterPagesByType(query, type);
  604. await this.filterPagesByViewer(query, user, userGroups);
  605. this.appendResultSize(query, from, size);
  606. this.appendFunctionScore(query, queryString);
  607. return this.search(query);
  608. };
  609. SearchClient.prototype.parseQueryString = function(queryString) {
  610. const matchWords = [];
  611. const notMatchWords = [];
  612. const phraseWords = [];
  613. const notPhraseWords = [];
  614. const prefixPaths = [];
  615. const notPrefixPaths = [];
  616. const tags = [];
  617. const notTags = [];
  618. queryString.trim();
  619. queryString = queryString.replace(/\s+/g, ' '); // eslint-disable-line no-param-reassign
  620. // First: Parse phrase keywords
  621. const phraseRegExp = new RegExp(/(-?"[^"]+")/g);
  622. const phrases = queryString.match(phraseRegExp);
  623. if (phrases !== null) {
  624. queryString = queryString.replace(phraseRegExp, ''); // eslint-disable-line no-param-reassign
  625. phrases.forEach((phrase) => {
  626. phrase.trim();
  627. if (phrase.match(/^-/)) {
  628. notPhraseWords.push(phrase.replace(/^-/, ''));
  629. }
  630. else {
  631. phraseWords.push(phrase);
  632. }
  633. });
  634. }
  635. // Second: Parse other keywords (include minus keywords)
  636. queryString.split(' ').forEach((word) => {
  637. if (word === '') {
  638. return;
  639. }
  640. // https://regex101.com/r/pN9XfK/1
  641. const matchNegative = word.match(/^-(prefix:|tag:)?(.+)$/);
  642. // https://regex101.com/r/3qw9FQ/1
  643. const matchPositive = word.match(/^(prefix:|tag:)?(.+)$/);
  644. if (matchNegative != null) {
  645. if (matchNegative[1] === 'prefix:') {
  646. notPrefixPaths.push(matchNegative[2]);
  647. }
  648. else if (matchNegative[1] === 'tag:') {
  649. notTags.push(matchNegative[2]);
  650. }
  651. else {
  652. notMatchWords.push(matchNegative[2]);
  653. }
  654. }
  655. else if (matchPositive != null) {
  656. if (matchPositive[1] === 'prefix:') {
  657. prefixPaths.push(matchPositive[2]);
  658. }
  659. else if (matchPositive[1] === 'tag:') {
  660. tags.push(matchPositive[2]);
  661. }
  662. else {
  663. matchWords.push(matchPositive[2]);
  664. }
  665. }
  666. });
  667. return {
  668. match: matchWords,
  669. not_match: notMatchWords,
  670. phrase: phraseWords,
  671. not_phrase: notPhraseWords,
  672. prefix: prefixPaths,
  673. not_prefix: notPrefixPaths,
  674. tag: tags,
  675. not_tag: notTags,
  676. };
  677. };
  678. SearchClient.prototype.syncPageCreated = function(page, user, bookmarkCount = 0) {
  679. debug('SearchClient.syncPageCreated', page.path);
  680. if (!this.shouldIndexed(page)) {
  681. return;
  682. }
  683. page.bookmarkCount = bookmarkCount;
  684. this.addPages([page])
  685. .then((res) => {
  686. debug('ES Response', res);
  687. })
  688. .catch((err) => {
  689. logger.error('ES Error', err);
  690. });
  691. };
  692. SearchClient.prototype.syncPageUpdated = function(page, user, bookmarkCount = 0) {
  693. debug('SearchClient.syncPageUpdated', page.path);
  694. // TODO delete
  695. if (!this.shouldIndexed(page)) {
  696. this.deletePages([page])
  697. .then((res) => {
  698. debug('deletePages: ES Response', res);
  699. })
  700. .catch((err) => {
  701. logger.error('deletePages:ES Error', err);
  702. });
  703. return;
  704. }
  705. page.bookmarkCount = bookmarkCount;
  706. this.updatePages([page])
  707. .then((res) => {
  708. debug('ES Response', res);
  709. })
  710. .catch((err) => {
  711. logger.error('ES Error', err);
  712. });
  713. };
  714. SearchClient.prototype.syncPageDeleted = function(page, user) {
  715. debug('SearchClient.syncPageDeleted', page.path);
  716. this.deletePages([page])
  717. .then((res) => {
  718. debug('deletePages: ES Response', res);
  719. })
  720. .catch((err) => {
  721. logger.error('deletePages:ES Error', err);
  722. });
  723. };
  724. SearchClient.prototype.syncBookmarkChanged = async function(pageId) {
  725. const Page = this.crowi.model('Page');
  726. const Bookmark = this.crowi.model('Bookmark');
  727. const page = await Page.findById(pageId);
  728. const bookmarkCount = await Bookmark.countByPageId(pageId);
  729. page.bookmarkCount = bookmarkCount;
  730. this.updatePages([page])
  731. .then((res) => { return debug('ES Response', res) })
  732. .catch((err) => { return logger.error('ES Error', err) });
  733. };
  734. SearchClient.prototype.syncTagChanged = async function(page) {
  735. this.updatePages([page])
  736. .then((res) => { return debug('ES Response', res) })
  737. .catch((err) => { return logger.error('ES Error', err) });
  738. };
  739. module.exports = SearchClient;