Skip to content

Commit

Permalink
Search Settings with Ranking config
Browse files Browse the repository at this point in the history
  • Loading branch information
harshach committed Dec 16, 2024
1 parent 254fce4 commit 26a6e92
Show file tree
Hide file tree
Showing 7 changed files with 1,068 additions and 13 deletions.
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
@@ -1,7 +1,5 @@
package org.openmetadata.service.search.indexes;

import static org.openmetadata.service.search.EntityBuilderConstant.ES_MESSAGE_SCHEMA_FIELD;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -135,7 +133,6 @@ private void parseSchemaFields(

public static Map<String, Float> getFields() {
Map<String, Float> fields = SearchIndex.getDefaultFields();
fields.put(ES_MESSAGE_SCHEMA_FIELD, 7.0f);
fields.put("responseSchema.schemaFields.name.keyword", 5.0f);
fields.put("responseSchema.schemaFields.description", 1.0f);
fields.put("responseSchema.schemaFields.children.name", 7.0f);
Expand Down
Loading

0 comments on commit 26a6e92

Please sign in to comment.