Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search Settings with Ranking config #19103

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.openmetadata.service.exception;

import javax.ws.rs.core.Response;
import org.openmetadata.sdk.exception.WebServiceException;

public class SystemSettingsException extends WebServiceException {
private static final String ERROR_TYPE = "SYSTEM_SETTINGS_EXCEPTION";

public SystemSettingsException(String message) {
super(Response.Status.BAD_REQUEST, ERROR_TYPE, message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.openmetadata.common.utils.CommonUtil.nullOrEmpty;
import static org.openmetadata.schema.settings.SettingsType.LINEAGE_SETTINGS;
import static org.openmetadata.schema.settings.SettingsType.SEARCH_SETTINGS;

import io.swagger.v3.oas.annotations.ExternalDocumentation;
import io.swagger.v3.oas.annotations.Hidden;
Expand All @@ -13,6 +14,8 @@
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import java.util.List;
import javax.json.JsonPatch;
import javax.validation.Valid;
import javax.ws.rs.Consumes;
Expand All @@ -30,6 +33,10 @@
import javax.ws.rs.core.SecurityContext;
import javax.ws.rs.core.UriInfo;
import lombok.extern.slf4j.Slf4j;
import org.openmetadata.common.utils.CommonUtil;
import org.openmetadata.schema.api.search.AssetTypeConfiguration;
import org.openmetadata.schema.api.search.GlobalSettings;
import org.openmetadata.schema.api.search.SearchSettings;
import org.openmetadata.schema.auth.EmailRequest;
import org.openmetadata.schema.settings.Settings;
import org.openmetadata.schema.settings.SettingsType;
Expand All @@ -42,14 +49,18 @@
import org.openmetadata.service.Entity;
import org.openmetadata.service.OpenMetadataApplicationConfig;
import org.openmetadata.service.clients.pipeline.PipelineServiceClientFactory;
import org.openmetadata.service.exception.SystemSettingsException;
import org.openmetadata.service.exception.UnhandledServerException;
import org.openmetadata.service.jdbi3.EntityRepository;
import org.openmetadata.service.jdbi3.ListFilter;
import org.openmetadata.service.jdbi3.SystemRepository;
import org.openmetadata.service.resources.Collection;
import org.openmetadata.service.security.Authorizer;
import org.openmetadata.service.security.JwtFilter;
import org.openmetadata.service.security.policyevaluator.OperationContext;
import org.openmetadata.service.security.policyevaluator.ResourceContext;
import org.openmetadata.service.util.EntityUtil;
import org.openmetadata.service.util.JsonUtils;
import org.openmetadata.service.util.ResultList;
import org.openmetadata.service.util.email.EmailUtil;

Expand All @@ -67,6 +78,7 @@ public class SystemResource {
private OpenMetadataApplicationConfig applicationConfig;
private PipelineServiceClientInterface pipelineServiceClient;
private JwtFilter jwtFilter;
private SearchSettings defaultSearchSettingsCache = new SearchSettings();

public SystemResource(Authorizer authorizer) {
this.systemRepository = Entity.getSystemRepository();
Expand All @@ -81,12 +93,50 @@ public void initialize(OpenMetadataApplicationConfig config) {

this.jwtFilter =
new JwtFilter(config.getAuthenticationConfiguration(), config.getAuthorizerConfiguration());
loadDefaultSearchSettings(false);
}

public static class SettingsList extends ResultList<Settings> {
/* Required for serde */
}

public SearchSettings readDefaultSearchSettings() {
if (defaultSearchSettingsCache != null) {
try {
List<String> jsonDataFiles =
EntityUtil.getJsonDataResources(".*/json/data/searchSettings/searchSettings.json");
if (!jsonDataFiles.isEmpty()) {
String json =
CommonUtil.getResourceAsStream(
EntityRepository.class.getClassLoader(), jsonDataFiles.get(0));
defaultSearchSettingsCache = JsonUtils.readValue(json, SearchSettings.class);
}
} catch (IOException e) {
LOG.error("Failed to read default search settings. Message: {}", e.getMessage(), e);
}
}
return defaultSearchSettingsCache;
}

public SearchSettings loadDefaultSearchSettings(boolean force) {
SearchSettings searchSettings = readDefaultSearchSettings();
if (!force) {
Settings existingSettings =
systemRepository.getConfigWithKey(String.valueOf(SEARCH_SETTINGS));
if (existingSettings != null && existingSettings.getConfigValue() != null) {
SearchSettings existingSearchSettings = (SearchSettings) existingSettings.getConfigValue();
if (existingSearchSettings.getGlobalSettings() != null) {
return searchSettings;
}
}
}
Settings settings =
new Settings().withConfigType(SettingsType.SEARCH_SETTINGS).withConfigValue(searchSettings);
systemRepository.createOrUpdate(settings);
LOG.info("Default searchSettings loaded successfully.");
return searchSettings;
}

@GET
@Path("/settings")
@Operation(
Expand Down Expand Up @@ -186,9 +236,93 @@ public Response createOrUpdateSetting(
@Context SecurityContext securityContext,
@Valid Settings settingName) {
authorizer.authorizeAdmin(securityContext);
if (SettingsType.SEARCH_SETTINGS
.value()
.equalsIgnoreCase(settingName.getConfigType().toString())) {
SearchSettings defaultSearchSettings = loadDefaultSearchSettings(false);
SearchSettings incomingSearchSettings =
JsonUtils.convertValue(settingName.getConfigValue(), SearchSettings.class);

GlobalSettings defaultGlobalSettings = defaultSearchSettings.getGlobalSettings();
GlobalSettings incomingGlobalSettings = incomingSearchSettings.getGlobalSettings();

GlobalSettings mergedGlobalSettings = new GlobalSettings();

mergedGlobalSettings.setMaxAggregateSize(
incomingGlobalSettings != null && incomingGlobalSettings.getMaxAggregateSize() != null
? incomingGlobalSettings.getMaxAggregateSize()
: defaultGlobalSettings.getMaxAggregateSize());

mergedGlobalSettings.setMaxResultHits(
incomingGlobalSettings != null && incomingGlobalSettings.getMaxResultHits() != null
? incomingGlobalSettings.getMaxResultHits()
: defaultGlobalSettings.getMaxResultHits());

mergedGlobalSettings.setMaxAnalyzedOffset(
incomingGlobalSettings != null && incomingGlobalSettings.getMaxAnalyzedOffset() != null
? incomingGlobalSettings.getMaxAnalyzedOffset()
: defaultGlobalSettings.getMaxAnalyzedOffset());

mergedGlobalSettings.setAggregations(defaultGlobalSettings.getAggregations());
mergedGlobalSettings.setHighlightFields(defaultGlobalSettings.getHighlightFields());

incomingSearchSettings.setGlobalSettings(mergedGlobalSettings);

if (incomingSearchSettings.getDefaultConfiguration() == null) {
incomingSearchSettings.setDefaultConfiguration(
defaultSearchSettings.getDefaultConfiguration());
}

List<AssetTypeConfiguration> defaultAssetTypes =
defaultSearchSettings.getAssetTypeConfigurations();
List<AssetTypeConfiguration> incomingAssetTypes =
incomingSearchSettings.getAssetTypeConfigurations();

for (AssetTypeConfiguration defaultConfig : defaultAssetTypes) {
String assetType = defaultConfig.getAssetType().toLowerCase();
boolean exists =
incomingAssetTypes.stream()
.anyMatch(config -> config.getAssetType().equalsIgnoreCase(assetType));
if (!exists) {
incomingAssetTypes.add(defaultConfig);
}
}

settingName.setConfigValue(incomingSearchSettings);
}
return systemRepository.createOrUpdate(settingName);
}

@PUT
@Path("/settings/reset/{name}")
@Operation(
operationId = "resetSettingToDefault",
summary = "Reset a setting to default",
description = "Reset the specified setting to its default value.",
responses = {
@ApiResponse(
responseCode = "200",
description = "Settings reset to default",
content =
@Content(
mediaType = "application/json",
schema = @Schema(implementation = Settings.class)))
})
public Response resetSettingToDefault(
@Context UriInfo uriInfo,
@Context SecurityContext securityContext,
@Parameter(description = "Name of the setting", schema = @Schema(type = "string"))
@PathParam("name")
String name) {
authorizer.authorizeAdmin(securityContext);

if (!SettingsType.SEARCH_SETTINGS.value().equalsIgnoreCase(name)) {
throw new SystemSettingsException("Resetting of setting '" + name + "' is not supported.");
}
SearchSettings settings = loadDefaultSearchSettings(true);
return Response.ok(settings).build();
}

@PUT
@Path("/email/test")
@Operation(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.openmetadata.service.search;

import es.org.elasticsearch.search.builder.SearchSourceBuilder;
import org.openmetadata.schema.api.search.SearchSettings;
import org.openmetadata.schema.settings.SettingsType;
import org.openmetadata.service.Entity;
import org.openmetadata.service.resources.settings.SettingsCache;

import java.io.IOException;

public interface SearchQueryBuilder {
SearchSourceBuilder getSearchSourceBuilder(String index, String q, int from, int size) throws IOException;
void applyDeletedLogic(SearchRequest request, SearchSourceBuilder searchSourceBuilder);
void applyGlossaryHierarchyLogic(SearchRequest request, SearchSourceBuilder searchSourceBuilder);
default String getAssetTypeFromIndex(String index) {
return switch (index) {
case "topic_search_index", "topic" -> "topic";
case "dashboard_search_index", "dashboard" -> "dashboard";
case "pipeline_search_index", "pipeline" -> "pipeline";
case "mlmodel_search_index", "mlmodel" -> "mlmodel";
case "table_search_index", "table" -> "table";
case "database_schema_search_index",
"databaseSchema",
"database_search_index",
"database" -> "dataAsset";
case "user_search_index", "user", "team_search_index", "team" -> "user";
case "glossary_term_search_index", "glossaryTerm" -> "glossaryTerm";
case "tag_search_index", "tag" -> "tag";
case "container_search_index", "container" -> "container";
case "query_search_index", "query" -> "query";
case "test_case_search_index",
"testCase",
"test_suite_search_index",
"testSuite" -> "testCase";
case "stored_procedure_search_index", "storedProcedure" -> "storedProcedure";
case "dashboard_data_model_search_index",
"dashboardDataModel" -> "dashboardDataModel";
case "search_entity_search_index", "searchIndex" -> "searchIndex";
case "domain_search_index", "domain" -> "domain";
case "raw_cost_analysis_report_data_index" -> "rawCostAnalysisReportData";
case "aggregated_cost_analysis_report_data_index" -> "aggregatedCostAnalysisReportData";
case "data_product_search_index" -> "dataProduct";
case "test_case_resolution_status_search_index" -> "test_case_resolution_status_search_index";
case "test_case_result_search_index" -> "testCase";
case "api_endpoint_search_index", "apiEndpoint" -> "apiEndpoint";
case "api_service_search_index",
"mlmodel_service_search_index",
"database_service_search_index",
"messaging_service_index",
"dashboard_service_index",
"pipeline_service_index",
"storage_service_index",
"search_service_index",
"metadata_service_index" -> "default";
case "dataAsset" -> "dataAsset";
case "all" -> "all";
default -> "dataAsset";
};
}

default SearchSettings getSearchSettings() {
return SettingsCache.getSetting(SettingsType.SEARCH_SETTINGS, SearchSettings.class);
}
}
Loading
Loading