1313import com .google .gson .Gson ;
1414import org .eclipse .sw360 .datahandler .cloudantclient .DatabaseConnectorCloudant ;
1515import org .eclipse .sw360 .datahandler .common .DatabaseSettings ;
16- import org .eclipse .sw360 .datahandler .common .SW360Constants ;
1716import org .eclipse .sw360 .datahandler .couchdb .lucene .NouveauLuceneAwareDatabaseConnector ;
1817import org .eclipse .sw360 .datahandler .permissions .ProjectPermissions ;
19- import org .eclipse .sw360 .datahandler .resourcelists .ResourceClassNotFoundException ;
20- import org .eclipse .sw360 .datahandler .resourcelists .ResourceComparatorGenerator ;
2118import org .eclipse .sw360 .datahandler .thrift .PaginationData ;
2219import org .eclipse .sw360 .datahandler .thrift .projects .Project ;
2320import org .eclipse .sw360 .datahandler .thrift .projects .ProjectSortColumn ;
2623import org .eclipse .sw360 .nouveau .designdocument .NouveauIndexDesignDocument ;
2724import org .eclipse .sw360 .nouveau .designdocument .NouveauIndexFunction ;
2825
26+ import javax .annotation .Nonnull ;
2927import java .io .IOException ;
3028import java .util .Collections ;
31- import java .util .Comparator ;
3229import java .util .HashMap ;
3330import java .util .HashSet ;
3431import java .util .List ;
@@ -55,18 +52,26 @@ public class ProjectSearchHandler {
5552 " }" +
5653 " if(doc.projectType !== undefined && doc.projectType != null && doc.projectType.length >0) {" +
5754 " index('text', 'projectType', doc.projectType, {'store': true});" +
55+ " index('string', 'projectType_sort', doc.projectType);" +
5856 " }" +
5957 " if(doc.projectResponsible !== undefined && doc.projectResponsible != null && doc.projectResponsible.length >0) {" +
6058 " index('text', 'projectResponsible', doc.projectResponsible, {'store': true});" +
59+ " index('string', 'projectResponsible_sort', doc.projectResponsible);" +
6160 " }" +
6261 " if(doc.name !== undefined && doc.name != null && doc.name.length >0) {" +
6362 " index('text', 'name', doc.name, {'store': true});" +
63+ " index('string', 'name_sort', doc.name);" +
64+ " }" +
65+ " if(doc.description !== undefined && doc.description != null && doc.description.length >0) {" +
66+ " index('text', 'description', doc.description, {'store': true});" +
67+ " index('string', 'description_sort', doc.description);" +
6468 " }" +
6569 " if(doc.version !== undefined && doc.version != null && doc.version.length >0) {" +
6670 " index('string', 'version', doc.version, {'store': true});" +
6771 " }" +
6872 " if(doc.state !== undefined && doc.state != null && doc.state.length >0) {" +
6973 " index('text', 'state', doc.state, {'store': true});" +
74+ " index('string', 'state_sort', doc.state);" +
7075 " }" +
7176 " if(doc.clearingState) {" +
7277 " index('text', 'clearingState', doc.clearingState, {'store': true});" +
@@ -78,6 +83,11 @@ public class ProjectSearchHandler {
7883 " if(doc.releaseRelationNetwork !== undefined && doc.releaseRelationNetwork != null && doc.releaseRelationNetwork.length > 0) {" +
7984 " index('text', 'releaseRelationNetwork', doc.releaseRelationNetwork, {'store': true});" +
8085 " }" +
86+ " if(doc.createdOn && doc.createdOn.length) {" +
87+ " var dt = new Date(doc.createdOn);" +
88+ " var formattedDt = `${dt.getFullYear()}${(dt.getMonth()+1).toString().padStart(2,'0')}${dt.getDate().toString().padStart(2,'0')}`;" +
89+ " index('double', 'createdOn', Number(formattedDt), {'store': true});" +
90+ " }" +
8191 "}" )
8292 .setFieldAnalyzer (
8393 Map .of ("version" , "keyword" )
@@ -99,10 +109,11 @@ public ProjectSearchHandler(Cloudant client, String dbName) throws IOException {
99109 }
100110
101111 public Map <PaginationData , List <Project >> search (String text , final Map <String , Set <String >> subQueryRestrictions , User user , PaginationData pageData ) {
112+ String sortColumn = getSortColumnName (pageData );
102113 Map <PaginationData , List <Project >> resultProjectList = connector
103114 .searchViewWithRestrictions (Project .class ,
104115 luceneSearchView .getIndexName (), text , subQueryRestrictions ,
105- pageData , null , pageData .isAscending ());
116+ pageData , sortColumn , pageData .isAscending ());
106117
107118 PaginationData respPageData = resultProjectList .keySet ().iterator ().next ();
108119 List <Project > projectList = resultProjectList .values ().iterator ().next ();
@@ -155,4 +166,24 @@ private static Map<String, Set<String>> getFilterMapForSetReleaseIds(Set<String>
155166 filterMap .put (Project ._Fields .RELEASE_RELATION_NETWORK .getFieldName (), values );
156167 return filterMap ;
157168 }
169+
170+ /**
171+ * Convert sort column number back to sorting column name. This function makes sure to use the string column (with
172+ * `_sort` suffix) for text indexes.
173+ * @param pageData Pagination Data from the request.
174+ * @return Sort column name. Defaults to name_sort
175+ */
176+ private static @ Nonnull String getSortColumnName (@ Nonnull PaginationData pageData ) {
177+ return switch (ProjectSortColumn .findByValue (pageData .getSortColumnNumber ())) {
178+ case ProjectSortColumn .BY_CREATEDON -> "createdOn" ;
179+ // case ProjectSortColumn.BY_VENDOR -> "vendor_sort";
180+ // case ProjectSortColumn.BY_MAINLICENSE -> "license_sort";
181+ case ProjectSortColumn .BY_TYPE -> "projectType_sort" ;
182+ case ProjectSortColumn .BY_DESCRIPTION -> "description_sort" ;
183+ case ProjectSortColumn .BY_RESPONSIBLE -> "projectResponsible_sort" ;
184+ case ProjectSortColumn .BY_STATE -> "state_sort" ;
185+ case null -> "name_sort" ;
186+ default -> "name_sort" ;
187+ };
188+ }
158189}
0 commit comments