Просмотр исходного кода

refs #82628: fix search keyword result sorting
- Fix FB
- prepare constant variables for sort_axis and order, and use them

NEEDLEMAN3\tatsu 4 лет назад
Родитель
Сommit
76749364d7

+ 3 - 2
packages/app/src/components/SearchPage.jsx

@@ -11,6 +11,7 @@ import SearchPageLayout from './SearchPage/SearchPageLayout';
 import SearchResultContent from './SearchPage/SearchResultContent';
 import SearchResultList from './SearchPage/SearchResultList';
 import SearchControl from './SearchPage/SearchControl';
+import { SORT_AXIS_CONSTS, SORT_ORDER_CONSTS } from '~/utils/search-axis-utils';
 
 export const specificPathNames = {
   user: '/user',
@@ -36,8 +37,8 @@ class SearchPage extends React.Component {
       pagingLimit: this.props.appContainer.config.pageLimitationL,
       excludeUserPages: true,
       excludeTrashPages: true,
-      sort: '_score',
-      order: 'desc',
+      sort: SORT_AXIS_CONSTS.score,
+      order: SORT_ORDER_CONSTS.desc,
     };
 
     this.changeURL = this.changeURL.bind(this);

+ 17 - 11
packages/app/src/components/SearchPage/SearchControl.tsx

@@ -5,10 +5,16 @@ import AppContainer from '../../client/services/AppContainer';
 import DeleteSelectedPageGroup from './DeleteSelectedPageGroup';
 import SearchOptionModal from './SearchOptionModal';
 import { CheckboxType } from '../../interfaces/search';
+import {
+  SORT_AXIS, SORT_AXIS_CONSTS, SORT_ORDER, SORT_ORDER_CONSTS,
+} from '~/utils/search-axis-utils';
+
+const { score: sortByScore, updatedAt: sortByUpdatedAt, createdAt: sortByCreatedAt } = SORT_AXIS_CONSTS;
+const { desc, asc } = SORT_ORDER_CONSTS;
 
 type Props = {
   searchingKeyword: string,
-  sort: string,
+  sort: SORT_AXIS,
   order: string,
   appContainer: AppContainer,
   excludeUserPages: boolean,
@@ -16,7 +22,7 @@ type Props = {
   onSearchInvoked: (data: {keyword: string}) => boolean,
   onExcludeUserPagesSwitched?: () => void,
   onExcludeTrashPagesSwitched?: () => void,
-  onChangeSortInvoked?: (nextSort: string, nextOrder: string) => void,
+  onChangeSortInvoked?: (nextSort: SORT_AXIS, nextOrder: SORT_ORDER) => void,
 }
 
 const SearchControl: FC <Props> = (props: Props) => {
@@ -43,19 +49,19 @@ const SearchControl: FC <Props> = (props: Props) => {
   // refs: https://redmine.weseek.co.jp/issues/82513
   const onClickChangeSort = () => {
     if (props.onChangeSortInvoked != null) {
-      const getNextSort = (sort) => {
+      const getNextSort = (sort: SORT_AXIS) => {
         switch (sort) {
-          case '_score':
-            return 'updated_at';
-          case 'updated_at':
-            return 'created_at';
-          case 'created_at':
+          case sortByScore:
+            return sortByUpdatedAt;
+          case sortByUpdatedAt:
+            return sortByCreatedAt;
+          case sortByCreatedAt:
           default:
-            return '_score';
+            return sortByScore;
         }
       };
-      const nextSort = props.order === 'desc' ? props.sort : getNextSort(props.sort);
-      const nextOrder = nextSort === props.sort ? 'asc' : 'desc';
+      const nextSort = props.order === desc ? props.sort : getNextSort(props.sort);
+      const nextOrder = nextSort === props.sort ? asc : desc;
       props.onChangeSortInvoked(nextSort, nextOrder);
     }
   };

+ 13 - 2
packages/app/src/server/service/search-delegator/elasticsearch.js

@@ -11,6 +11,9 @@ const {
 } = require('stream');
 const streamToPromise = require('stream-to-promise');
 
+const {
+  SORT_AXIS, SORT_AXIS_CONSTS, SORT_ORDER, SORT_ORDER_CONSTS,
+} = require('~/utils/search-axis-utils');
 const { createBatchStream } = require('../../util/batch-stream');
 
 const DEFAULT_OFFSET = 0;
@@ -606,6 +609,14 @@ class ElasticsearchDelegator {
     return query;
   }
 
+  /**
+   * create search query for Elasticsearch
+   *
+   * @param {SORT_AXIS} sort sort axis
+   * @param {SORT_ORDER} order sort order
+   * @param {*} option optional paramas
+   * @returns {object} query object
+   */
   createSearchQuery(sort, order, option) {
     let fields = ['path', 'bookmark_count', 'comment_count', 'seenUsers_count', 'updated_at', 'tag_names'];
     if (option) {
@@ -902,8 +913,8 @@ class ElasticsearchDelegator {
     const size = option.limit || null;
     const type = option.type || null;
     // default sort order is score descending
-    const sort = option.sort || '_score';
-    const order = option.order || 'desc';
+    const sort = option.sort || SORT_AXIS_CONSTS.score;
+    const order = option.order || SORT_ORDER_CONSTS.desc;
     const query = this.createSearchQuery(sort, order);
     this.appendCriteriaForQueryString(query, queryString);
 

+ 24 - 0
packages/app/src/utils/search-axis-utils.ts

@@ -0,0 +1,24 @@
+
+const SORT_AXIS_CONSTS = {
+  score: '_score',
+  createdAt: 'created_at',
+  updatedAt: 'updated_at',
+};
+
+type SORT_AXIS = typeof SORT_AXIS_CONSTS[keyof typeof SORT_AXIS_CONSTS];
+
+const SORT_ORDER_CONSTS = {
+  desc: 'desc',
+  asc: 'asc',
+};
+type SORT_ORDER = typeof SORT_ORDER_CONSTS[keyof typeof SORT_ORDER_CONSTS];
+
+export type {
+  SORT_AXIS,
+  SORT_ORDER,
+};
+
+export {
+  SORT_AXIS_CONSTS,
+  SORT_ORDER_CONSTS,
+};