/*
 * Copyright 2021 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.b2international.snowowl.core.request;

import com.b2international.index.Hits;
import com.b2international.index.query.Expression;
import com.b2international.index.query.Expressions;
import com.b2international.index.query.Expressions.ExpressionBuilder;
import com.b2international.snowowl.core.Resources;
import com.b2international.snowowl.core.domain.RepositoryContext;
import com.b2international.snowowl.core.internal.ResourceDocument;

/**
 * @since 8.0
 */
final class ResourceSearchRequest extends SearchIndexResourceRequest<RepositoryContext, Resources, ResourceDocument> {

	private static final long serialVersionUID = 1L;

	enum OptionKey {
		
		/**
		 * Filter matches by their resource type.
		 */
		RESOURCE_TYPE, 
		
		/**
		 * Filter matches by their title (exact match).
		 */
		TITLE_EXACT, 
		
		/**
		 * Filter matches by their tooling id property.
		 */
		TOOLING_ID,
		
		/**
		 * Filter matches by their currently associated working branch (exact match).
		 */
		BRANCH,
		
	}
	
	@Override
	protected Class<ResourceDocument> getDocumentType() {
		return ResourceDocument.class;
	}
	
	@Override
	protected Expression prepareQuery(RepositoryContext context) {
		final ExpressionBuilder queryBuilder = Expressions.builder();
		addIdFilter(queryBuilder, ResourceDocument.Expressions::ids);
		addFilter(queryBuilder, OptionKey.RESOURCE_TYPE, String.class, ResourceDocument.Expressions::resourceTypes);
		addFilter(queryBuilder, OptionKey.TITLE_EXACT, String.class, ResourceDocument.Expressions::titles);
		addFilter(queryBuilder, OptionKey.TOOLING_ID, String.class, ResourceDocument.Expressions::toolingIds);
		addFilter(queryBuilder, OptionKey.BRANCH, String.class, ResourceDocument.Expressions::branchPaths);
		return queryBuilder.build();
	}

	@Override
	protected Resources toCollectionResource(RepositoryContext context, Hits<ResourceDocument> hits) {
		return new ResourceConverter(context, expand(), locales()).convert(hits);
	}

	@Override
	protected Resources createEmptyResult(int limit) {
		return new Resources(limit, 0);
	}

	
	
}

@@ -1275,18 +1275,16 @@
 onKey %7B%0A
-%09%09
 %0A%09%09/**%0A%09
@@ -1348,20 +1348,17 @@
 CE_TYPE,
- %0A%09%09
+%0A
 %0A%09%09/**%0A%09
@@ -1430,12 +1430,99 @@
 ACT,
- %0A%09%09
+%0A%0A%09%09/** %22Smart%22 search by title (taking prefixes, stemming, etc. into account) */%0A%09%09TITLE,%0A
 %0A%09%09/
@@ -1594,18 +1594,16 @@
 ING_ID,%0A
-%09%09
 %0A%09%09/**%0A%09
@@ -1701,15 +1701,12 @@
 CH,%0A
-%09%09
 %0A%09%7D%0A
-%09
 %0A%09@O
@@ -1804,17 +1804,16 @@
 ass;%0A%09%7D%0A
-%09
 %0A%09@Overr
@@ -2434,36 +2434,796 @@
 ;%0A%09%09
-return queryBuilder.build();
+%0A%09%09addTermFilters(queryBuilder);%0A%09%09%0A%09%09return queryBuilder.build();%0A%09%7D%0A%0A%09private void addTermFilters(ExpressionBuilder queryBuilder) %7B%0A%09%09if (containsKey(OptionKey.TITLE)) %7B%0A%09%09%09final String searchTerm = getString(OptionKey.TITLE);%0A%09%09%09System.out.println(%22ResourceSearchRequest.addTermFilters() %22 + searchTerm);%0A%09%09%09final ExpressionBuilder termFilter = Expressions.builder();%0A%0A%09%09%09termFilter.should(Expressions.dismaxWithScoreCategories(ResourceDocument.Expressions.matchTitleExact(searchTerm),%0A%09%09%09%09%09ResourceDocument.Expressions.matchTitleAllTermsPresent(searchTerm),%0A%09%09%09%09%09ResourceDocument.Expressions.matchTitleAllPrefixesPresent(searchTerm)));%0A%09%09%09%0A%09%09%09termFilter.should(Expressions.boost(ResourceDocument.Expressions.id(searchTerm), 1000.0f));%0A%09%09%09%0A%09%09%09queryBuilder.must(termFilter.build());%0A%09%09%7D
 %0A%09%7D%0A
@@ -3519,10 +3519,6 @@
 %09%7D%0A%0A
-%09%0A%09%0A
 %7D%0A

Add termFilters to ResourceSearchRequest