Taichi Masuyama 4 лет назад
Родитель
Сommit
a9b53ed687

+ 1 - 4
packages/app/src/server/interfaces/search.ts

@@ -13,10 +13,7 @@ export type QueryTerms = {
   not_tag: string[],
 }
 
-export type ParsedQuery = {
-  queryString: string
-  delegatorName: string
-}
+export type ParsedQuery = { terms?: QueryTerms, delegatorName?: string }
 
 export interface SearchQueryParser {
   parseSearchQuery(queryString: string): Promise<ParsedQuery>

+ 24 - 12
packages/app/src/server/service/search.ts

@@ -154,29 +154,41 @@ class SearchService implements SearchQueryParser, SearchResolver {
   }
 
   async parseSearchQuery(_queryString: string): Promise<ParsedQuery> {
-    const delegatorNameObj = {
-      name: null,
-      isExist: false,
-    };
 
     const regexp = new RE2(/^\[nq:.+\]$/g); // https://regex101.com/r/FzDUvT/1
+    const replaceRegexp = new RE2(/\[nq:|\]/g);
 
     const queryString = normalizeQueryString(_queryString);
 
-    const promises = queryString.split(' ').map(async(word) => {
-      const isNamedQuery = regexp.test(word);
+    // when Normal Query
+    if (!regexp.test(queryString)) {
+      return { terms: this.parseQueryString(queryString) };
+    }
 
-      if (isNamedQuery) {
-        const name = word.replace(/\[nq:|\]/g, ''); // remove '[nq:' and ']'
+    // when Named Query
+    const NamedQuery = mongoose.model('NamedQuery') as NamedQueryModel;
 
-      }
-    });
+    const name = queryString.replace(replaceRegexp, '');
+    const nq = await NamedQuery.findOne({ name });
+    if (nq == null) {
+      throw Error('Named Query not found.');
+    }
+
+    const { aliasOf, delegatorName } = nq;
+
+    let parsedQuery;
+    if (aliasOf != null) {
+      parsedQuery = { terms: this.parseQueryString(aliasOf) };
+    }
+    if (delegatorName != null) {
+      parsedQuery = { delegatorName };
+    }
 
-    return { queryString: _queryString, delegatorName };
+    return parsedQuery;
   }
 
   async resolve(parsedQuery: ParsedQuery): Promise<[SearchDelegator, SearchableData | null]> {
-    const { queryString, delegatorName } = parsedQuery;
+    const { terms, delegatorName } = parsedQuery;
 
     if (delegatorName != null) {
       const nqDelegator = this.nqDelegators[delegatorName];