Last active
February 4, 2025 07:59
-
-
Save unclebean/4817c07c0735fbdf818de703d0ecd391 to your computer and use it in GitHub Desktop.
AI Search
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.service; | |
import com.azure.search.documents.SearchClient; | |
import com.azure.search.documents.models.SearchOptions; | |
import com.azure.search.documents.models.SearchResult; | |
import org.springframework.cache.annotation.Cacheable; | |
import org.springframework.stereotype.Service; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.stream.Collectors; | |
@Service | |
public class AliasMappingService { | |
private final SearchClient searchClient; | |
public AliasMappingService(SearchClient searchClient) { | |
this.searchClient = searchClient; | |
} | |
@Cacheable(value = "aliasMappingCache") | |
public Map<String, String> getAliasMappings() { | |
List<SearchResult> results = searchClient.search("*", new SearchOptions()).getResults().stream().toList(); | |
Map<String, String> aliasMapping = new HashMap<>(); | |
for (SearchResult result : results) { | |
String alias = result.getDocument().get("alias").toString(); | |
String indexName = result.getDocument().get("indexName").toString(); | |
aliasMapping.put(alias, indexName); | |
} | |
return aliasMapping; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.config; | |
import com.azure.core.credential.AzureKeyCredential; | |
import com.azure.core.http.HttpRequest; | |
import com.azure.core.http.policy.HttpPipelinePolicy; | |
import com.azure.search.documents.SearchClient; | |
import com.azure.search.documents.SearchClientBuilder; | |
import com.azure.core.http.policy.HttpPipelineCallContext; | |
import com.azure.core.http.policy.HttpPipelineNextPolicy; | |
import com.azure.core.http.HttpResponse; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import reactor.core.publisher.Mono; | |
import java.net.URI; | |
import java.net.URISyntaxException; | |
import java.util.regex.Pattern; | |
@Configuration | |
public class AzureSearchConfig { | |
private static final String SEARCH_SERVICE_NAME = "your-search-service-name"; | |
private static final String INDEX_NAME = "your-index-name"; | |
private static final String API_KEY = "your-api-key"; | |
private static final String API_VERSION = "2024-05-01-preview"; // Target API version | |
@Bean | |
public SearchClient searchClient() { | |
return new SearchClientBuilder() | |
.endpoint("https://" + SEARCH_SERVICE_NAME + ".search.windows.net") | |
.credential(new AzureKeyCredential(API_KEY)) | |
.indexName(INDEX_NAME) | |
.addPolicy(new ApiVersionPolicy()) // Add Custom Policy | |
.buildClient(); | |
} | |
// Custom Policy to replace api-version in query parameters using Regex | |
private static class ApiVersionPolicy implements HttpPipelinePolicy { | |
private static final Pattern API_VERSION_PATTERN = Pattern.compile("api-version=[^&]+"); | |
@Override | |
public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineNextPolicy next) { | |
modifyRequestUrl(context.getHttpRequest()); | |
return next.process(); | |
} | |
private void modifyRequestUrl(HttpRequest request) { | |
try { | |
URI originalUri = request.getUrl().toURI(); | |
String query = originalUri.getQuery(); | |
// Replace existing api-version or add it if missing | |
String updatedQuery; | |
if (query == null || query.isEmpty()) { | |
updatedQuery = "api-version=" + API_VERSION; | |
} else if (API_VERSION_PATTERN.matcher(query).find()) { | |
updatedQuery = API_VERSION_PATTERN.matcher(query).replaceFirst("api-version=" + API_VERSION); | |
} else { | |
updatedQuery = query + "&api-version=" + API_VERSION; | |
} | |
// Reconstruct URI with updated query | |
URI updatedUri = new URI(originalUri.getScheme(), originalUri.getAuthority(), | |
originalUri.getPath(), updatedQuery, originalUri.getFragment()); | |
request.setUrl(updatedUri.toURL()); // Set modified URL back to request | |
} catch (URISyntaxException | java.net.MalformedURLException e) { | |
throw new RuntimeException("Failed to update API version in request URL", e); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
az search document index --service-name <YOUR-SEARCH-SERVICE> \ | |
--resource-group <YOUR_RESOURCE_GROUP> \ | |
--index-name <NEW_INDEX_NAME> \ | |
--documents "@old-index-data.json" | |
curl -X GET "https://<YOUR-SEARCH-SERVICE>.search.windows.net/indexes/<OLD_INDEX_NAME>/docs?api-version=2023-07-01-Preview&search=*" \ | |
-H "Content-Type: application/json" \ | |
-H "api-key: <YOUR-ADMIN-API-KEY>" > old-index-data.json | |
az search admin-key show --service-name <YOUR-SEARCH-SERVICE> --resource-group <YOUR-RESOURCE-GROUP> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment