@@ -98,31 +98,48 @@ public class RegistrySearchUtil {
98
98
* @throws APIManagementException If there is an error in the search query
99
99
*/
100
100
private static String constructQueryWithProvidedCriterias (String inputSearchQuery ) throws APIPersistenceException {
101
-
102
101
String newSearchQuery = "" ;
103
- // sub context and doc content doesn't support AND search
104
- if (inputSearchQuery != null && inputSearchQuery .contains (" " )
105
- && !inputSearchQuery .contains (TAG_COLON_SEARCH_TYPE_PREFIX )
106
- && (!inputSearchQuery .contains (CONTENT_SEARCH_TYPE_PREFIX ) || inputSearchQuery .split (":" ).length > 2 )) {
107
- if (inputSearchQuery .split (" " ).length > 1 ) {
108
- String [] searchCriterias = inputSearchQuery .split (" " );
109
- for (int i = 0 ; i < searchCriterias .length ; i ++) {
110
- if (searchCriterias [i ].contains (":" ) && searchCriterias [i ].split (":" ).length > 1 ) {
111
- if (DOCUMENTATION_SEARCH_TYPE_PREFIX .equalsIgnoreCase (searchCriterias [i ].split (":" )[0 ])) {
112
- throw new APIPersistenceException ("Invalid query. AND based search is not supported for "
113
- + "doc prefix" );
114
- }
115
- }
116
- if (i == 0 ) {
117
- newSearchQuery = getSingleSearchCriteria (searchCriterias [i ]);
118
- } else {
119
- newSearchQuery = newSearchQuery + SEARCH_AND_TAG + getSingleSearchCriteria (searchCriterias [i ]);
120
- }
102
+
103
+ //for empty search query this method should return name=* as the new search query
104
+ if (StringUtils .isEmpty (inputSearchQuery )) {
105
+ newSearchQuery = getSingleSearchCriteria (inputSearchQuery );
106
+ } else {
107
+ String [] criterea = inputSearchQuery .split (" " );
108
+ Map <String , List <String >> critereaMap = new HashMap <>();
109
+ for (int i = 0 ; i < criterea .length ; i ++) {
110
+ if (criterea [i ].contains (":" ) && criterea [i ].split (":" ).length > 1 ) {
111
+ String searchPrefix = criterea [i ].split (":" )[0 ];
112
+ String searchValue = criterea [i ].split (":" )[1 ];
113
+
114
+ List <String > values = critereaMap .containsKey (searchPrefix ) ? critereaMap .get (searchPrefix ) : new ArrayList <>();
115
+ values .add (searchValue );
116
+ critereaMap .put (searchPrefix , values );
121
117
}
122
118
}
123
- } else {
124
- newSearchQuery = getSingleSearchCriteria (inputSearchQuery );
119
+
120
+ // doc content doesn't support AND search
121
+ if (critereaMap .size () > 1 && critereaMap .containsKey (DOCUMENTATION_SEARCH_TYPE_PREFIX )) {
122
+ throw new APIPersistenceException ("Invalid query. AND based search is not supported for "
123
+ + "doc prefix" );
124
+ }
125
+
126
+ // When multiple values are present for the same search key those are considered as an OR based search.
127
+ // ex: tags:sales tags:dev -> tags=(sales OR dev)
128
+ // When multiple search keys are present those are considered as an AND based search.
129
+ // ex: name:pizzashack version:1.0 -> name=pizzashack AND version=1.0
130
+ for (Map .Entry <String , List <String >> entry : critereaMap .entrySet ()) {
131
+ String nextCriterea = "" ;
132
+ if (entry .getValue ().size () > 1 ) {
133
+ nextCriterea = entry .getKey () + "=" + getORBasedSearchCriteria (entry .getValue ().toArray (new String [0 ]));
134
+ } else {
135
+ nextCriterea = getSingleSearchCriteria (entry .getKey () + ":" + entry .getValue ().get (0 ));
136
+ }
137
+
138
+ newSearchQuery = StringUtils .isNotEmpty (newSearchQuery ) ? (newSearchQuery + SEARCH_AND_TAG + nextCriterea ) :
139
+ nextCriterea ;
140
+ }
125
141
}
142
+
126
143
return newSearchQuery ;
127
144
}
128
145
0 commit comments