From dd25972c8bb779b52af1ff6cf1c0dea775b92418 Mon Sep 17 00:00:00 2001 From: Egor Date: Sat, 10 May 2025 16:05:11 +0300 Subject: [PATCH 01/17] save version commit// add simple QueryMapper test --- transport/build.gradle | 22 +++++-- .../com/omnia/transport/OmniaEndpoint.java | 35 +---------- .../java/com/omnia/transport/QueryMapper.java | 45 +++++++++++++ .../com/omnia/transport/QueryMapperTest.java | 63 +++++++++++++++++++ 4 files changed, 128 insertions(+), 37 deletions(-) create mode 100644 transport/src/main/java/com/omnia/transport/QueryMapper.java create mode 100644 transport/src/test/java/com/omnia/transport/QueryMapperTest.java diff --git a/transport/build.gradle b/transport/build.gradle index 672221e..c87f4d8 100644 --- a/transport/build.gradle +++ b/transport/build.gradle @@ -12,20 +12,32 @@ repositories { ext { junitVersion = '5.12.0' opensearchVersion = '2.9.0' + testcontainersVersion = '1.20.6' + httpClientVersion = '4.5.13' + postgresqlDriverVersion = '42.7.3' + opensearchtestcontainersVersion = '2.1.3' } - dependencies { implementation project(":common") implementation project(":sdk") - - // OpenSearch implementation "org.opensearch.client:opensearch-java:$opensearchVersion" - - // JUnit 5 + implementation "org.opensearch.client:opensearch-rest-client:$opensearchVersion" + implementation "org.apache.httpcomponents:httpclient:$httpClientVersion" testImplementation "org.junit.jupiter:junit-jupiter-api:$junitVersion" testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junitVersion" + testRuntimeOnly "org.junit.platform:junit-platform-launcher:1.12.0" + testImplementation "org.testcontainers:junit-jupiter:$testcontainersVersion" + testImplementation "org.testcontainers:testcontainers:$testcontainersVersion" + testImplementation "org.testcontainers:postgresql:$testcontainersVersion" + testImplementation "org.opensearch:opensearch-testcontainers:$opensearchtestcontainersVersion" } +java { + toolchain { + languageVersion = JavaLanguageVersion.of(21) + } +} + test { useJUnitPlatform() } diff --git a/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java b/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java index 51becb7..02a354b 100644 --- a/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java +++ b/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java @@ -37,43 +37,14 @@ public String requestUrl(RequestT request) throws IllegalArgumentException { @Override public Map queryParameters(RequestT request) { Map params = endpoint.queryParameters(request); - Query query = Query.of(q -> q - .bool(builder -> { - for (Map.Entry entry : params.entrySet()) { - String key = entry.getKey(); - String value = entry.getValue(); - builder.filter(f -> f.term(t -> t - .field(key) - .value(v -> v.stringValue(value)) - )); - } - return builder; - }) - ); + QueryMapper mapper = new QueryMapper(); + Query query = mapper.mapToQuery(params); Map answer = new HashMap<>(); query = sdk.addIndexFilter(query, endpoint.requestUrl(request)); - processQuery(query,answer); + mapper.queryToMap(query,answer); return answer; } - - private void processQuery(Query query, Map result) { - if (query.isTerm()) { - TermQuery term = query.term(); - String value = term.value().toString(); - result.put(term.field(), value); - } - else if (query.isMatch()) { - MatchQuery match = query.match(); - result.put(match.field(), match.query().toString()); - } - else if (query.isBool()) { - BoolQuery bool = query.bool(); - bool.must().forEach(q -> processQuery(q, result)); - bool.should().forEach(q -> processQuery(q, result)); - bool.filter().forEach(q -> processQuery(q, result)); - } - } @Override public boolean hasRequestBody() { return endpoint.hasRequestBody(); diff --git a/transport/src/main/java/com/omnia/transport/QueryMapper.java b/transport/src/main/java/com/omnia/transport/QueryMapper.java new file mode 100644 index 0000000..6fd59bf --- /dev/null +++ b/transport/src/main/java/com/omnia/transport/QueryMapper.java @@ -0,0 +1,45 @@ +package com.omnia.transport; + +import org.opensearch.client.opensearch._types.query_dsl.BoolQuery; +import org.opensearch.client.opensearch._types.query_dsl.MatchQuery; +import org.opensearch.client.opensearch._types.query_dsl.Query; +import org.opensearch.client.opensearch._types.query_dsl.TermQuery; + +import java.util.Map; + +public class QueryMapper { + + public Query mapToQuery(Map params){ + return Query.of(q -> q + .bool(builder -> { + for (Map.Entry entry : params.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + builder.filter(f -> f.term(t -> t + .field(key) + .value(v -> v.stringValue(value)) + )); + } + return builder; + }) + ); + } + + public void queryToMap(Query query, Map result) { + if (query.isTerm()) { + TermQuery term = query.term(); + String value = term.value().stringValue(); + result.put(term.field(), value); + } + else if (query.isMatch()) { + MatchQuery match = query.match(); + result.put(match.field(), match.query().stringValue()); + } + else if (query.isBool()) { + BoolQuery bool = query.bool(); + bool.must().forEach(q -> queryToMap(q, result)); + bool.should().forEach(q -> queryToMap(q, result)); + bool.filter().forEach(q -> queryToMap(q, result)); + } + } +} diff --git a/transport/src/test/java/com/omnia/transport/QueryMapperTest.java b/transport/src/test/java/com/omnia/transport/QueryMapperTest.java new file mode 100644 index 0000000..b541dcc --- /dev/null +++ b/transport/src/test/java/com/omnia/transport/QueryMapperTest.java @@ -0,0 +1,63 @@ +package com.omnia.transport; +import org.junit.jupiter.api.Test; +import org.opensearch.client.opensearch._types.FieldValue; +import org.opensearch.client.opensearch._types.query_dsl.Query; +import org.opensearch.client.opensearch._types.query_dsl.BoolQuery; +import org.opensearch.client.opensearch._types.query_dsl.TermQuery; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.*; + +public class QueryMapperTest { + QueryMapper mapper = new QueryMapper(); + @Test + void mapToQuery_simpleTest(){ + Map params = new HashMap<>(); + params.put("name", "Alice"); + params.put("age", "18"); + Query query = mapper.mapToQuery(params); + BoolQuery boolQuery = query.bool(); + assertEquals(2, boolQuery.filter().size()); + Query firstFilter = boolQuery.filter().get(0); + assertTrue(firstFilter.isTerm()); + TermQuery firstTerm = firstFilter.term(); + assertEquals("name", firstTerm.field()); + assertEquals("Alice", firstTerm.value().stringValue()); + Query secondFilter = boolQuery.filter().get(1); + assertTrue(secondFilter.isTerm()); + TermQuery secondTerm = secondFilter.term(); + assertEquals("age", secondTerm.field()); + assertEquals("18", secondTerm.value().stringValue()); + } + + @Test + void mapToQuery_emptyTest() { + Map params = new HashMap<>(); + QueryMapper mapper = new QueryMapper(); + Query query = mapper.mapToQuery(params); + assertTrue(query.isBool()); + assertTrue(query.bool().filter().isEmpty()); + } + + + @Test + void queryToMap_simpleBoolQueries() { + QueryMapper mapper = new QueryMapper(); + Map result = new HashMap<>(); + Query nestedBool = Query.of(b -> b.bool(bb -> bb + .must(m -> m.term(t -> t.field("level").value(FieldValue.of("debug")))) + )); + Query mainQuery = Query.of(b -> b.bool(bb -> bb + .must(nestedBool) + .filter(f -> f.match(m -> m.field("type").query(FieldValue.of("log")))) + )); + + mapper.queryToMap(mainQuery, result); + assertEquals(2, result.size()); + assertEquals("debug", result.get("level")); + assertEquals("log", result.get("type")); + } + +} From e72cd7a55dff8017b53504a16f1663670fa9f762 Mon Sep 17 00:00:00 2001 From: Egor Date: Sat, 10 May 2025 16:22:55 +0300 Subject: [PATCH 02/17] more test --- .../com/omnia/transport/QueryMapperTest.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/transport/src/test/java/com/omnia/transport/QueryMapperTest.java b/transport/src/test/java/com/omnia/transport/QueryMapperTest.java index b541dcc..9dadfea 100644 --- a/transport/src/test/java/com/omnia/transport/QueryMapperTest.java +++ b/transport/src/test/java/com/omnia/transport/QueryMapperTest.java @@ -1,6 +1,7 @@ package com.omnia.transport; import org.junit.jupiter.api.Test; import org.opensearch.client.opensearch._types.FieldValue; +import org.opensearch.client.opensearch._types.query_dsl.MatchQuery; import org.opensearch.client.opensearch._types.query_dsl.Query; import org.opensearch.client.opensearch._types.query_dsl.BoolQuery; import org.opensearch.client.opensearch._types.query_dsl.TermQuery; @@ -59,5 +60,33 @@ void queryToMap_simpleBoolQueries() { assertEquals("debug", result.get("level")); assertEquals("log", result.get("type")); } + @Test + void queryToMap_MatchQueryTest() { + MatchQuery matchQuery = new MatchQuery.Builder() + .field("title") + .query(q -> q.stringValue("OpenSearch")) + .build(); + Query query = new Query.Builder() + .match(matchQuery) + .build(); + Map resultMap = new HashMap<>(); + mapper.queryToMap(query, resultMap); + assertEquals(1, resultMap.size()); + assertEquals("OpenSearch", resultMap.get("title")); + } + @Test + void queryToMap_termQuerytest() { + Query query = new Query.Builder() + .term(t -> t + .field("title") + .value(v -> v.stringValue("OpenSearch")) + ) + .build(); + + Map resultMap = new HashMap<>(); + mapper.queryToMap(query, resultMap); + assertEquals(1, resultMap.size()); + assertEquals("OpenSearch", resultMap.get("title")); + } } From 50327af429e6d17697bf628a83131f4d30159230 Mon Sep 17 00:00:00 2001 From: Egor Date: Sat, 10 May 2025 16:24:27 +0300 Subject: [PATCH 03/17] reformat code --- .../src/main/java/com/omnia/transport/OmniaEndpoint.java | 2 +- .../src/main/java/com/omnia/transport/OmniaTransport.java | 1 + .../src/main/java/com/omnia/transport/QueryMapper.java | 8 +++----- .../test/java/com/omnia/transport/QueryMapperTest.java | 7 +++++-- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java b/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java index 02a354b..26a9cc0 100644 --- a/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java +++ b/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java @@ -41,7 +41,7 @@ public Map queryParameters(RequestT request) { Query query = mapper.mapToQuery(params); Map answer = new HashMap<>(); query = sdk.addIndexFilter(query, endpoint.requestUrl(request)); - mapper.queryToMap(query,answer); + mapper.queryToMap(query, answer); return answer; } diff --git a/transport/src/main/java/com/omnia/transport/OmniaTransport.java b/transport/src/main/java/com/omnia/transport/OmniaTransport.java index 10e01b3..14fb869 100644 --- a/transport/src/main/java/com/omnia/transport/OmniaTransport.java +++ b/transport/src/main/java/com/omnia/transport/OmniaTransport.java @@ -6,6 +6,7 @@ import org.opensearch.client.transport.TransportOptions; import com.omnia.sdk.OmniaSDK; + import javax.annotation.Nullable; import java.io.IOException; import java.util.concurrent.CompletableFuture; diff --git a/transport/src/main/java/com/omnia/transport/QueryMapper.java b/transport/src/main/java/com/omnia/transport/QueryMapper.java index 6fd59bf..39709e2 100644 --- a/transport/src/main/java/com/omnia/transport/QueryMapper.java +++ b/transport/src/main/java/com/omnia/transport/QueryMapper.java @@ -9,7 +9,7 @@ public class QueryMapper { - public Query mapToQuery(Map params){ + public Query mapToQuery(Map params) { return Query.of(q -> q .bool(builder -> { for (Map.Entry entry : params.entrySet()) { @@ -30,12 +30,10 @@ public void queryToMap(Query query, Map result) { TermQuery term = query.term(); String value = term.value().stringValue(); result.put(term.field(), value); - } - else if (query.isMatch()) { + } else if (query.isMatch()) { MatchQuery match = query.match(); result.put(match.field(), match.query().stringValue()); - } - else if (query.isBool()) { + } else if (query.isBool()) { BoolQuery bool = query.bool(); bool.must().forEach(q -> queryToMap(q, result)); bool.should().forEach(q -> queryToMap(q, result)); diff --git a/transport/src/test/java/com/omnia/transport/QueryMapperTest.java b/transport/src/test/java/com/omnia/transport/QueryMapperTest.java index 9dadfea..7972ce0 100644 --- a/transport/src/test/java/com/omnia/transport/QueryMapperTest.java +++ b/transport/src/test/java/com/omnia/transport/QueryMapperTest.java @@ -1,4 +1,5 @@ package com.omnia.transport; + import org.junit.jupiter.api.Test; import org.opensearch.client.opensearch._types.FieldValue; import org.opensearch.client.opensearch._types.query_dsl.MatchQuery; @@ -11,10 +12,11 @@ import static org.junit.jupiter.api.Assertions.*; -public class QueryMapperTest { +public class QueryMapperTest { QueryMapper mapper = new QueryMapper(); + @Test - void mapToQuery_simpleTest(){ + void mapToQuery_simpleTest() { Map params = new HashMap<>(); params.put("name", "Alice"); params.put("age", "18"); @@ -60,6 +62,7 @@ void queryToMap_simpleBoolQueries() { assertEquals("debug", result.get("level")); assertEquals("log", result.get("type")); } + @Test void queryToMap_MatchQueryTest() { MatchQuery matchQuery = new MatchQuery.Builder() From a563062cfaf47b7883547e0dfead0ca0ac0245b2 Mon Sep 17 00:00:00 2001 From: Egor Date: Tue, 13 May 2025 03:07:35 +0300 Subject: [PATCH 04/17] pass sdk test with bad codestyle and problem with queryParameters:c --- transport/build.gradle | 8 +- .../com/omnia/transport/OmniaEndpoint.java | 67 +++++++- .../com/omnia/transport/OmniaTransport.java | 3 +- .../omnia/transport/OmniaEndpointTest.java | 160 ++++++++++++++++++ .../com/omnia/transport/QueryMapperTest.java | 5 +- 5 files changed, 231 insertions(+), 12 deletions(-) create mode 100644 transport/src/test/java/com/omnia/transport/OmniaEndpointTest.java diff --git a/transport/build.gradle b/transport/build.gradle index c87f4d8..bed264d 100644 --- a/transport/build.gradle +++ b/transport/build.gradle @@ -10,17 +10,19 @@ repositories { } ext { + jooqVersion = '3.19.7' junitVersion = '5.12.0' - opensearchVersion = '2.9.0' - testcontainersVersion = '1.20.6' httpClientVersion = '4.5.13' postgresqlDriverVersion = '42.7.3' opensearchtestcontainersVersion = '2.1.3' + opensearchVersion = '2.9.0' + testcontainersVersion = '1.20.6' } dependencies { implementation project(":common") implementation project(":sdk") + implementation "org.jooq:jooq:$jooqVersion" implementation "org.opensearch.client:opensearch-java:$opensearchVersion" implementation "org.opensearch.client:opensearch-rest-client:$opensearchVersion" implementation "org.apache.httpcomponents:httpclient:$httpClientVersion" @@ -31,7 +33,9 @@ dependencies { testImplementation "org.testcontainers:testcontainers:$testcontainersVersion" testImplementation "org.testcontainers:postgresql:$testcontainersVersion" testImplementation "org.opensearch:opensearch-testcontainers:$opensearchtestcontainersVersion" + testRuntimeOnly "org.postgresql:postgresql:$postgresqlDriverVersion" } + java { toolchain { languageVersion = JavaLanguageVersion.of(21) diff --git a/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java b/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java index 26a9cc0..e734e9e 100644 --- a/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java +++ b/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java @@ -1,23 +1,28 @@ package com.omnia.transport; +import org.jooq.Null; import org.opensearch.client.json.JsonpDeserializer; +import org.opensearch.client.json.UnexpectedJsonEventException; import org.opensearch.client.opensearch._types.FieldValue; +import org.opensearch.client.opensearch._types.OpenSearchException; import org.opensearch.client.opensearch._types.query_dsl.*; import org.opensearch.client.transport.Endpoint; import com.omnia.sdk.OmniaSDK; +import org.opensearch.client.transport.JsonEndpoint; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; -public class OmniaEndpoint implements Endpoint { +public class OmniaEndpoint implements Endpoint, JsonEndpoint { private final Endpoint endpoint; private final OmniaSDK sdk; - public OmniaEndpoint(Endpoint endpoint, OmniaSDK sdk) { + public OmniaEndpoint(Endpoint endpoint, OmniaSDK sdk) { this.endpoint = endpoint; this.sdk = sdk; } @@ -29,22 +34,62 @@ public String method(RequestT request) { @Override public String requestUrl(RequestT request) throws IllegalArgumentException { + List AA = List.of(endpoint.requestUrl(request).split("/")); List Indecies = parseUrl(endpoint.requestUrl(request)); - Indecies = Indecies.stream().map(index -> sdk.transformIndexId(index)).collect(Collectors.toList()); - return "/" + String.join("%2C", Indecies); + List newIndecies = new ArrayList<>(); + for(var x: Indecies){ + String newIndex = sdk.transformIndexId(x); + if(newIndex== null){ + newIndecies.add(x); + continue; + } + newIndecies.add(newIndex); + } + String answer = "/" + String.join("%2C", newIndecies); + if(AA.size()<=2){ + return answer; + } + answer+="/"; + for(int i=2;i queryParameters(RequestT request) { + Map a =endpoint.queryParameters(request); + for (var x: a.keySet()){ + System.out.println(x +" "+ a.get(x)); + } Map params = endpoint.queryParameters(request); QueryMapper mapper = new QueryMapper(); Query query = mapper.mapToQuery(params); Map answer = new HashMap<>(); - query = sdk.addIndexFilter(query, endpoint.requestUrl(request)); + List Indecies = parseUrl(endpoint.requestUrl(request)); + for(var x: Indecies) { + System.out.println(x+"cacab"); + // query = sdk.addIndexFilter(query,x); + } mapper.queryToMap(query, answer); + System.out.println(answer.keySet()); + for (var x: answer.keySet()){ + System.out.println(x +" "+ answer.get(x)); + } return answer; } + @Override + public Map headers(RequestT request) { + if( endpoint instanceof JsonEndpoint) { + return endpoint.headers(request); + } + else{ + throw new IllegalArgumentException("Expected JsonEndpooint"); + } + } + @Override public boolean hasRequestBody() { return endpoint.hasRequestBody(); @@ -66,6 +111,16 @@ private List parseUrl(String path) { if (parsePath.getFirst() == null) { throw new IllegalArgumentException(); } - return List.of(parsePath.getFirst().split("%2C")); + return List.of(parsePath.get(1).split("%2C")); + } + + @Override + public JsonpDeserializer responseDeserializer() { + if( endpoint instanceof JsonEndpoint) { + return ((JsonEndpoint) endpoint).responseDeserializer(); + } + else{ + throw new IllegalArgumentException("Expected JsonEndpooint"); + } } } diff --git a/transport/src/main/java/com/omnia/transport/OmniaTransport.java b/transport/src/main/java/com/omnia/transport/OmniaTransport.java index 14fb869..2e61b88 100644 --- a/transport/src/main/java/com/omnia/transport/OmniaTransport.java +++ b/transport/src/main/java/com/omnia/transport/OmniaTransport.java @@ -2,6 +2,7 @@ import org.opensearch.client.json.JsonpMapper; import org.opensearch.client.transport.Endpoint; +import org.opensearch.client.transport.OpenSearchTransport; import org.opensearch.client.transport.Transport; import org.opensearch.client.transport.TransportOptions; @@ -11,7 +12,7 @@ import java.io.IOException; import java.util.concurrent.CompletableFuture; -public class OmniaTransport implements Transport { +public class OmniaTransport implements OpenSearchTransport { private final Transport delegate; private final OmniaSDK sdk; diff --git a/transport/src/test/java/com/omnia/transport/OmniaEndpointTest.java b/transport/src/test/java/com/omnia/transport/OmniaEndpointTest.java new file mode 100644 index 0000000..835c1d7 --- /dev/null +++ b/transport/src/test/java/com/omnia/transport/OmniaEndpointTest.java @@ -0,0 +1,160 @@ +package com.omnia.transport; + + +import com.omnia.common.config.AppConfig; +import com.omnia.common.config.Config; +import com.omnia.common.config.db.Database; +import com.omnia.common.config.db.PostgresqlParams; +import com.omnia.sdk.OmniaSDKPostgreSQL; +import org.apache.http.HttpHost; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.opensearch.client.RestClient; +import org.opensearch.client.opensearch.OpenSearchClient; +import org.opensearch.client.opensearch._types.ErrorResponse; +import org.opensearch.client.opensearch._types.query_dsl.Query; +import org.opensearch.client.opensearch.core.GetRequest; +import org.opensearch.client.opensearch.core.IndexRequest; +import org.opensearch.client.opensearch.core.SearchRequest; +import org.opensearch.client.opensearch.core.SearchResponse; +import org.opensearch.client.opensearch.indices.CreateIndexRequest; +import org.opensearch.client.opensearch.indices.CreateIndexResponse; +import org.opensearch.client.opensearch.indices.OpenSearchIndicesClient; +import org.opensearch.client.opensearch.indices.RefreshRequest; +import org.opensearch.client.transport.JsonEndpoint; +import org.opensearch.client.transport.OpenSearchTransport; +import org.opensearch.client.transport.rest_client.RestClientTransport; +import org.opensearch.testcontainers.OpensearchContainer; +import org.testcontainers.containers.PostgreSQLContainer; +import org.testcontainers.junit.jupiter.Container; + + +import java.io.IOException; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.HashMap; +import java.util.Map; +import org.testcontainers.junit.jupiter.Testcontainers; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@Testcontainers +public class OmniaEndpointTest { + + private static final String FEATURE_NAME = "test-feature"; + @Container + public static PostgreSQLContainer postgres = new PostgreSQLContainer<>("postgres:13") + .withDatabaseName("testdb") + .withUsername("testuser") + .withPassword("testpass"); + @Container + public static OpensearchContainer opensearch = (OpensearchContainer) new OpensearchContainer("opensearchproject/opensearch:2.9.0") + .withStartupTimeout(java.time.Duration.ofMinutes(5)); + private static OmniaSDKPostgreSQL sdk; + private static OpenSearchClient openSearchClient; + + @BeforeAll + static void setUp() throws SQLException, IOException { + // Setup PostgreSQL schema and data + try (Connection conn = DriverManager.getConnection( + postgres.getJdbcUrl(), + postgres.getUsername(), + postgres.getPassword()); + Statement stmt = conn.createStatement()) { + + stmt.execute("CREATE TABLE \"INDEX_TO_COMMUNE\" (\"index\" VARCHAR PRIMARY KEY, commune VARCHAR)"); + stmt.execute("INSERT INTO \"INDEX_TO_COMMUNE\" (\"index\", commune) VALUES ('123', 'commune')"); + stmt.execute("INSERT INTO \"INDEX_TO_COMMUNE\" (\"index\", commune) VALUES ('456', 'commune')"); + } + + AppConfig appConfig = createTestAppConfig(); + sdk = new OmniaSDKPostgreSQL(appConfig); + openSearchClient = createOpenSearchClient(); + + createOpenSearchIndex(); + indexTestDocument("123", "1"); + indexTestDocument("456", "2"); + } + + private static AppConfig createTestAppConfig() { + PostgresqlParams postgresqlParams = new PostgresqlParams(); + postgresqlParams.setHost(postgres.getHost()); + postgresqlParams.setPort(postgres.getFirstMappedPort()); + postgresqlParams.setDatabaseName(postgres.getDatabaseName()); + postgresqlParams.setUsername(postgres.getUsername()); + postgresqlParams.setPassword(postgres.getPassword()); + + Database database = new Database(); + database.setType("postgresql"); + database.setParams(postgresqlParams.toParams()); + + AppConfig appConfig = new AppConfig(); + appConfig.setConfig(new Config()); + appConfig.setDatabase(database); + appConfig.getConfig().setFeatureName(FEATURE_NAME); + + return appConfig; + } + + private static OpenSearchClient createOpenSearchClient() throws IOException { + RestClient restClient = RestClient.builder( + new HttpHost(opensearch.getHost(), opensearch.getMappedPort(9200)) + ).build(); + OpenSearchTransport transport = new RestClientTransport(restClient, new org.opensearch.client.json.jackson.JacksonJsonpMapper()); + OpenSearchTransport omniaTransport = new OmniaTransport(transport, sdk); + return new OpenSearchClient(omniaTransport); + } + + private static void createOpenSearchIndex() throws IOException { + CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder() + .index("commune") + .build(); + OpenSearchIndicesClient a = openSearchClient.indices(); + a.create(createIndexRequest); + } + + private static void indexTestDocument(String name, String id) throws IOException { + Map document = new HashMap<>(); + document.put(FEATURE_NAME, name); + + IndexRequest> indexRequest = new IndexRequest.Builder>() + .index("commune") + .id(id) + .document(document) + .build(); + + openSearchClient.index(indexRequest); + openSearchClient.indices().refresh(b -> b.index("commune")); + } + + @Test + void testCreateSearchRequestBuilder() throws IOException { + SearchRequest.Builder requestBuilder = sdk.createSearchRequestBuilder("123"); + SearchRequest request = requestBuilder.build(); + + SearchResponse response = openSearchClient.search(request, Object.class); + assertEquals(1, response.hits().hits().size(), "Should find one document"); + } + + @Test + void testAddIndexFilter() throws IOException { + Query baseQuery = new Query.Builder().matchAll(m -> m).build(); + Query combinedQuery = sdk.addIndexFilter(baseQuery, "123"); + + SearchRequest request = new SearchRequest.Builder() + .index("commune") + .query(combinedQuery) + .build(); + + SearchResponse response = openSearchClient.search(request, Object.class); + assertEquals(1, response.hits().hits().size(), "Combined query should find one document"); + } + + @Test + void testGetFilterField() { + assertEquals(FEATURE_NAME, sdk.getFilterField(), "Filter field should match feature name from config"); + } + +} diff --git a/transport/src/test/java/com/omnia/transport/QueryMapperTest.java b/transport/src/test/java/com/omnia/transport/QueryMapperTest.java index 7972ce0..79bf60f 100644 --- a/transport/src/test/java/com/omnia/transport/QueryMapperTest.java +++ b/transport/src/test/java/com/omnia/transport/QueryMapperTest.java @@ -46,7 +46,7 @@ void mapToQuery_emptyTest() { @Test - void queryToMap_simpleBoolQueries() { + void queryToMap_simpleBoolQueryTest() { QueryMapper mapper = new QueryMapper(); Map result = new HashMap<>(); Query nestedBool = Query.of(b -> b.bool(bb -> bb @@ -79,14 +79,13 @@ void queryToMap_MatchQueryTest() { } @Test - void queryToMap_termQuerytest() { + void queryToMap_termQueryTest() { Query query = new Query.Builder() .term(t -> t .field("title") .value(v -> v.stringValue("OpenSearch")) ) .build(); - Map resultMap = new HashMap<>(); mapper.queryToMap(query, resultMap); assertEquals(1, resultMap.size()); From 00f7459da9a125b1eca379a68e5c1dab063a2b7b Mon Sep 17 00:00:00 2001 From: Egor Date: Tue, 13 May 2025 03:08:37 +0300 Subject: [PATCH 05/17] delete logs --- .../main/java/com/omnia/transport/OmniaEndpoint.java | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java b/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java index e734e9e..999ce66 100644 --- a/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java +++ b/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java @@ -60,23 +60,15 @@ public String requestUrl(RequestT request) throws IllegalArgumentException { @Override public Map queryParameters(RequestT request) { Map a =endpoint.queryParameters(request); - for (var x: a.keySet()){ - System.out.println(x +" "+ a.get(x)); - } Map params = endpoint.queryParameters(request); QueryMapper mapper = new QueryMapper(); Query query = mapper.mapToQuery(params); Map answer = new HashMap<>(); List Indecies = parseUrl(endpoint.requestUrl(request)); for(var x: Indecies) { - System.out.println(x+"cacab"); - // query = sdk.addIndexFilter(query,x); + // query = sdk.addIndexFilter(query,x); } mapper.queryToMap(query, answer); - System.out.println(answer.keySet()); - for (var x: answer.keySet()){ - System.out.println(x +" "+ answer.get(x)); - } return answer; } From b2409a6c46d7ddf623b73364a22739a583b778bc Mon Sep 17 00:00:00 2001 From: Egor Date: Tue, 13 May 2025 03:09:30 +0300 Subject: [PATCH 06/17] refactor code --- .../com/omnia/transport/OmniaEndpoint.java | 30 +++++++++---------- .../omnia/transport/OmniaEndpointTest.java | 1 + 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java b/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java index 999ce66..4994042 100644 --- a/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java +++ b/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java @@ -22,7 +22,7 @@ public class OmniaEndpoint implements Endpoint endpoint; private final OmniaSDK sdk; - public OmniaEndpoint(Endpoint endpoint, OmniaSDK sdk) { + public OmniaEndpoint(Endpoint endpoint, OmniaSDK sdk) { this.endpoint = endpoint; this.sdk = sdk; } @@ -37,20 +37,20 @@ public String requestUrl(RequestT request) throws IllegalArgumentException { List AA = List.of(endpoint.requestUrl(request).split("/")); List Indecies = parseUrl(endpoint.requestUrl(request)); List newIndecies = new ArrayList<>(); - for(var x: Indecies){ + for (var x : Indecies) { String newIndex = sdk.transformIndexId(x); - if(newIndex== null){ + if (newIndex == null) { newIndecies.add(x); continue; } newIndecies.add(newIndex); } String answer = "/" + String.join("%2C", newIndecies); - if(AA.size()<=2){ + if (AA.size() <= 2) { return answer; } - answer+="/"; - for(int i=2;i queryParameters(RequestT request) { - Map a =endpoint.queryParameters(request); + Map a = endpoint.queryParameters(request); Map params = endpoint.queryParameters(request); QueryMapper mapper = new QueryMapper(); Query query = mapper.mapToQuery(params); Map answer = new HashMap<>(); List Indecies = parseUrl(endpoint.requestUrl(request)); - for(var x: Indecies) { - // query = sdk.addIndexFilter(query,x); + for (var x : Indecies) { + // query = sdk.addIndexFilter(query,x); } mapper.queryToMap(query, answer); return answer; @@ -74,11 +74,10 @@ public Map queryParameters(RequestT request) { @Override public Map headers(RequestT request) { - if( endpoint instanceof JsonEndpoint) { + if (endpoint instanceof JsonEndpoint) { return endpoint.headers(request); - } - else{ - throw new IllegalArgumentException("Expected JsonEndpooint"); + } else { + throw new IllegalArgumentException("Expected JsonEndpooint"); } } @@ -108,10 +107,9 @@ private List parseUrl(String path) { @Override public JsonpDeserializer responseDeserializer() { - if( endpoint instanceof JsonEndpoint) { + if (endpoint instanceof JsonEndpoint) { return ((JsonEndpoint) endpoint).responseDeserializer(); - } - else{ + } else { throw new IllegalArgumentException("Expected JsonEndpooint"); } } diff --git a/transport/src/test/java/com/omnia/transport/OmniaEndpointTest.java b/transport/src/test/java/com/omnia/transport/OmniaEndpointTest.java index 835c1d7..dff8a50 100644 --- a/transport/src/test/java/com/omnia/transport/OmniaEndpointTest.java +++ b/transport/src/test/java/com/omnia/transport/OmniaEndpointTest.java @@ -36,6 +36,7 @@ import java.sql.Statement; import java.util.HashMap; import java.util.Map; + import org.testcontainers.junit.jupiter.Testcontainers; import static org.junit.jupiter.api.Assertions.assertEquals; From 5275ebb0af8e611437f14932a0cc10346fdfd046 Mon Sep 17 00:00:00 2001 From: Egor Date: Tue, 13 May 2025 16:35:08 +0300 Subject: [PATCH 07/17] =?UTF-8?q?=D0=94=D0=BE=20=D1=81=D0=B8=D1=85=20?= =?UTF-8?q?=D0=BF=D0=BE=D1=80=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BE?= =?UTF-8?q?=D1=82=20SDK,=20=D0=BD=D0=BE=20=D1=83=D1=81=D1=82=D1=80=D0=B0?= =?UTF-8?q?=D0=BD=D0=B5=D0=BD=D0=B0=20=D0=BF=D1=80=D0=BE=D0=B1=D0=BB=D0=B5?= =?UTF-8?q?=D0=BC=D0=B0=20=D1=81=20queryParameters=20=D0=B4=D0=BB=D1=8F=20?= =?UTF-8?q?SearchRequest(=D0=BE=D1=81=D1=82=D0=B0=D0=BB=D1=8C=D0=BD=D1=8B?= =?UTF-8?q?=D0=BC=20=D0=BA=D0=B0=D0=B6=D0=B5=D1=82=D1=81=D1=8F=20=D0=B8=20?= =?UTF-8?q?=D0=BD=D0=B5=20=D0=BD=D1=83=D0=B6=D0=BD=D0=BE)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/omnia/transport/OmniaEndpoint.java | 56 ++++++++++++------- .../com/omnia/transport/OmniaTransport.java | 14 ++++- .../omnia/transport/OmniaEndpointTest.java | 52 ++++++----------- 3 files changed, 66 insertions(+), 56 deletions(-) diff --git a/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java b/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java index 4994042..633baf6 100644 --- a/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java +++ b/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java @@ -7,6 +7,7 @@ import org.opensearch.client.opensearch._types.FieldValue; import org.opensearch.client.opensearch._types.OpenSearchException; import org.opensearch.client.opensearch._types.query_dsl.*; +import org.opensearch.client.opensearch.core.SearchRequest; import org.opensearch.client.transport.Endpoint; import com.omnia.sdk.OmniaSDK; @@ -36,40 +37,46 @@ public String method(RequestT request) { public String requestUrl(RequestT request) throws IllegalArgumentException { List AA = List.of(endpoint.requestUrl(request).split("/")); List Indecies = parseUrl(endpoint.requestUrl(request)); - List newIndecies = new ArrayList<>(); - for (var x : Indecies) { - String newIndex = sdk.transformIndexId(x); - if (newIndex == null) { - newIndecies.add(x); - continue; - } - newIndecies.add(newIndex); - } - String answer = "/" + String.join("%2C", newIndecies); + StringBuilder answer = new StringBuilder("/" + String.join("%2C", Indecies)); if (AA.size() <= 2) { - return answer; + return answer.toString(); } - answer += "/"; + answer.append("/"); for (int i = 2; i < AA.size() - 1; i++) { - answer += AA.get(i) + "/"; + answer.append(AA.get(i)).append("/"); } - answer += AA.getLast(); - return answer; + answer.append(AA.getLast()); + String a = answer.toString(); + return a; } @Override public Map queryParameters(RequestT request) { + /* Map a = endpoint.queryParameters(request); + for(var x: a.keySet()){ + System.out.println(x+" "+a.get(x)); + } Map params = endpoint.queryParameters(request); QueryMapper mapper = new QueryMapper(); Query query = mapper.mapToQuery(params); Map answer = new HashMap<>(); - List Indecies = parseUrl(endpoint.requestUrl(request)); + List parsePath = List.of(endpoint.requestUrl(request).split("/")); + List Indecies = List.of(parsePath.get(1).split("%2C")); for (var x : Indecies) { - // query = sdk.addIndexFilter(query,x); + if(request instanceof SearchRequest) { + query = sdk.addIndexFilter(query, x); + // Проблема. Кажется, что endpoint.queryParameters не все параметры Query -> если добавлять в endpoint.queryParameters лишнее падаем с исключением от RestTransport + } } + mapper.queryToMap(query, answer); + for(var x: answer.keySet()){ + System.out.println("Answer: "+x+" "+answer.get(x)); + } return answer; + */ + return endpoint.queryParameters(request); } @Override @@ -96,14 +103,25 @@ public JsonpDeserializer errorDeserializer(int statusCode) { return endpoint.errorDeserializer(statusCode); } - //Вероятно это не правда... я не уверен - private List parseUrl(String path) { + public List getIndex(String path){ List parsePath = List.of(path.split("/")); if (parsePath.getFirst() == null) { throw new IllegalArgumentException(); } return List.of(parsePath.get(1).split("%2C")); } + private List parseUrl(String path) { + List answer = new ArrayList<>(); + for (var x : getIndex(path)) { + String newIndex = sdk.transformIndexId(x); + if (newIndex == null) { + answer.add(x); + continue; + } + answer.add(newIndex); + } + return answer; + } @Override public JsonpDeserializer responseDeserializer() { diff --git a/transport/src/main/java/com/omnia/transport/OmniaTransport.java b/transport/src/main/java/com/omnia/transport/OmniaTransport.java index 2e61b88..8ce4e8f 100644 --- a/transport/src/main/java/com/omnia/transport/OmniaTransport.java +++ b/transport/src/main/java/com/omnia/transport/OmniaTransport.java @@ -1,6 +1,8 @@ package com.omnia.transport; import org.opensearch.client.json.JsonpMapper; +import org.opensearch.client.opensearch._types.query_dsl.Query; +import org.opensearch.client.opensearch.core.SearchRequest; import org.opensearch.client.transport.Endpoint; import org.opensearch.client.transport.OpenSearchTransport; import org.opensearch.client.transport.Transport; @@ -9,7 +11,9 @@ import com.omnia.sdk.OmniaSDK; import javax.annotation.Nullable; +import javax.crypto.SealedObject; import java.io.IOException; +import java.util.List; import java.util.concurrent.CompletableFuture; public class OmniaTransport implements OpenSearchTransport { @@ -24,7 +28,15 @@ public OmniaTransport(Transport delegate, OmniaSDK sdk) { @Override public ResponseT performRequest(RequestT request, Endpoint endpoint, @Nullable TransportOptions options) throws IOException { final OmniaEndpoint customEndpoint = new OmniaEndpoint<>(endpoint, sdk); - return delegate.performRequest(request, customEndpoint, options); + if(request instanceof SearchRequest){ + Query combinedQuery = ((SearchRequest) request).query(); + List Indexes = customEndpoint.getIndex(endpoint.requestUrl(request)); + for(var x: Indexes) { + combinedQuery = sdk.addIndexFilter(combinedQuery,x ); + } + return delegate.performRequest(((RequestT)new SearchRequest.Builder().query(combinedQuery).index(((SearchRequest) request).index()).build()), customEndpoint, options); + } + return delegate.performRequest(request,customEndpoint,options); } @Override diff --git a/transport/src/test/java/com/omnia/transport/OmniaEndpointTest.java b/transport/src/test/java/com/omnia/transport/OmniaEndpointTest.java index dff8a50..0d2d30f 100644 --- a/transport/src/test/java/com/omnia/transport/OmniaEndpointTest.java +++ b/transport/src/test/java/com/omnia/transport/OmniaEndpointTest.java @@ -1,33 +1,26 @@ package com.omnia.transport; - +import com.omnia.sdk.*; import com.omnia.common.config.AppConfig; import com.omnia.common.config.Config; import com.omnia.common.config.db.Database; import com.omnia.common.config.db.PostgresqlParams; -import com.omnia.sdk.OmniaSDKPostgreSQL; import org.apache.http.HttpHost; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.opensearch.client.RestClient; import org.opensearch.client.opensearch.OpenSearchClient; -import org.opensearch.client.opensearch._types.ErrorResponse; import org.opensearch.client.opensearch._types.query_dsl.Query; -import org.opensearch.client.opensearch.core.GetRequest; import org.opensearch.client.opensearch.core.IndexRequest; import org.opensearch.client.opensearch.core.SearchRequest; import org.opensearch.client.opensearch.core.SearchResponse; import org.opensearch.client.opensearch.indices.CreateIndexRequest; -import org.opensearch.client.opensearch.indices.CreateIndexResponse; -import org.opensearch.client.opensearch.indices.OpenSearchIndicesClient; -import org.opensearch.client.opensearch.indices.RefreshRequest; -import org.opensearch.client.transport.JsonEndpoint; import org.opensearch.client.transport.OpenSearchTransport; import org.opensearch.client.transport.rest_client.RestClientTransport; import org.opensearch.testcontainers.OpensearchContainer; import org.testcontainers.containers.PostgreSQLContainer; import org.testcontainers.junit.jupiter.Container; - +import org.testcontainers.junit.jupiter.Testcontainers; import java.io.IOException; import java.sql.Connection; @@ -37,8 +30,6 @@ import java.util.HashMap; import java.util.Map; -import org.testcontainers.junit.jupiter.Testcontainers; - import static org.junit.jupiter.api.Assertions.assertEquals; @Testcontainers @@ -58,7 +49,6 @@ public class OmniaEndpointTest { @BeforeAll static void setUp() throws SQLException, IOException { - // Setup PostgreSQL schema and data try (Connection conn = DriverManager.getConnection( postgres.getJdbcUrl(), postgres.getUsername(), @@ -66,14 +56,12 @@ static void setUp() throws SQLException, IOException { Statement stmt = conn.createStatement()) { stmt.execute("CREATE TABLE \"INDEX_TO_COMMUNE\" (\"index\" VARCHAR PRIMARY KEY, commune VARCHAR)"); - stmt.execute("INSERT INTO \"INDEX_TO_COMMUNE\" (\"index\", commune) VALUES ('123', 'commune')"); - stmt.execute("INSERT INTO \"INDEX_TO_COMMUNE\" (\"index\", commune) VALUES ('456', 'commune')"); + stmt.execute("INSERT INTO \"INDEX_TO_COMMUNE\" (\"index\", commune) VALUES ('123', 'commune'), ('456', 'commune') ,('123A', 'commune')"); } AppConfig appConfig = createTestAppConfig(); sdk = new OmniaSDKPostgreSQL(appConfig); openSearchClient = createOpenSearchClient(); - createOpenSearchIndex(); indexTestDocument("123", "1"); indexTestDocument("456", "2"); @@ -86,34 +74,32 @@ private static AppConfig createTestAppConfig() { postgresqlParams.setDatabaseName(postgres.getDatabaseName()); postgresqlParams.setUsername(postgres.getUsername()); postgresqlParams.setPassword(postgres.getPassword()); - Database database = new Database(); database.setType("postgresql"); database.setParams(postgresqlParams.toParams()); - AppConfig appConfig = new AppConfig(); appConfig.setConfig(new Config()); appConfig.setDatabase(database); appConfig.getConfig().setFeatureName(FEATURE_NAME); - return appConfig; } - private static OpenSearchClient createOpenSearchClient() throws IOException { + private static OpenSearchClient createOpenSearchClient() { RestClient restClient = RestClient.builder( new HttpHost(opensearch.getHost(), opensearch.getMappedPort(9200)) ).build(); + + OpenSearchTransport transport = new RestClientTransport(restClient, new org.opensearch.client.json.jackson.JacksonJsonpMapper()); - OpenSearchTransport omniaTransport = new OmniaTransport(transport, sdk); - return new OpenSearchClient(omniaTransport); + OpenSearchTransport omniatransport = new OmniaTransport(transport,sdk); + return new OpenSearchClient(omniatransport); } private static void createOpenSearchIndex() throws IOException { CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder() .index("commune") .build(); - OpenSearchIndicesClient a = openSearchClient.indices(); - a.create(createIndexRequest); + openSearchClient.indices().create(createIndexRequest); } private static void indexTestDocument(String name, String id) throws IOException { @@ -127,14 +113,14 @@ private static void indexTestDocument(String name, String id) throws IOException .build(); openSearchClient.index(indexRequest); - openSearchClient.indices().refresh(b -> b.index("commune")); + openSearchClient.indices().refresh(b -> b.index("commune")); // Refresh to make document searchable } @Test void testCreateSearchRequestBuilder() throws IOException { - SearchRequest.Builder requestBuilder = sdk.createSearchRequestBuilder("123"); - SearchRequest request = requestBuilder.build(); - + Query baseQuery = new Query.Builder().matchAll(m -> m).build(); + SearchRequest request =new SearchRequest.Builder() + .index("123").query(baseQuery).build(); SearchResponse response = openSearchClient.search(request, Object.class); assertEquals(1, response.hits().hits().size(), "Should find one document"); } @@ -142,20 +128,14 @@ void testCreateSearchRequestBuilder() throws IOException { @Test void testAddIndexFilter() throws IOException { Query baseQuery = new Query.Builder().matchAll(m -> m).build(); - Query combinedQuery = sdk.addIndexFilter(baseQuery, "123"); SearchRequest request = new SearchRequest.Builder() - .index("commune") - .query(combinedQuery) + .index("123") + .query(baseQuery) .build(); SearchResponse response = openSearchClient.search(request, Object.class); assertEquals(1, response.hits().hits().size(), "Combined query should find one document"); } - @Test - void testGetFilterField() { - assertEquals(FEATURE_NAME, sdk.getFilterField(), "Filter field should match feature name from config"); - } - -} +} \ No newline at end of file From 543b54ee01d3a73e4167e27b6b0c59f7442b45dd Mon Sep 17 00:00:00 2001 From: Egor Date: Tue, 13 May 2025 16:35:44 +0300 Subject: [PATCH 08/17] :c --- .../src/test/java/com/omnia/transport/OmniaEndpointTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/transport/src/test/java/com/omnia/transport/OmniaEndpointTest.java b/transport/src/test/java/com/omnia/transport/OmniaEndpointTest.java index 0d2d30f..e8e563b 100644 --- a/transport/src/test/java/com/omnia/transport/OmniaEndpointTest.java +++ b/transport/src/test/java/com/omnia/transport/OmniaEndpointTest.java @@ -56,7 +56,7 @@ static void setUp() throws SQLException, IOException { Statement stmt = conn.createStatement()) { stmt.execute("CREATE TABLE \"INDEX_TO_COMMUNE\" (\"index\" VARCHAR PRIMARY KEY, commune VARCHAR)"); - stmt.execute("INSERT INTO \"INDEX_TO_COMMUNE\" (\"index\", commune) VALUES ('123', 'commune'), ('456', 'commune') ,('123A', 'commune')"); + stmt.execute("INSERT INTO \"INDEX_TO_COMMUNE\" (\"index\", commune) VALUES ('123', 'commune'), ('456', 'commune') ,('123A', 'commune')"); } AppConfig appConfig = createTestAppConfig(); From 4f5714be4a1a949d18d21d3580f49568e98d4e1b Mon Sep 17 00:00:00 2001 From: Egor Date: Tue, 13 May 2025 16:36:51 +0300 Subject: [PATCH 09/17] =?UTF-8?q?refactor=20code.=20=D0=9E=D0=B4=D0=BD?= =?UTF-8?q?=D0=B0=D0=B6=D0=B4=D1=8B=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D1=8E=20code=20style=20=D0=B8=20=D1=81=D0=BE=D0=B7=D0=B4?= =?UTF-8?q?=D0=B0=D0=BC=20pr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/omnia/transport/OmniaEndpoint.java | 3 ++- .../main/java/com/omnia/transport/OmniaTransport.java | 10 +++++----- .../java/com/omnia/transport/OmniaEndpointTest.java | 4 ++-- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java b/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java index 633baf6..af6c1da 100644 --- a/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java +++ b/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java @@ -103,13 +103,14 @@ public JsonpDeserializer errorDeserializer(int statusCode) { return endpoint.errorDeserializer(statusCode); } - public List getIndex(String path){ + public List getIndex(String path) { List parsePath = List.of(path.split("/")); if (parsePath.getFirst() == null) { throw new IllegalArgumentException(); } return List.of(parsePath.get(1).split("%2C")); } + private List parseUrl(String path) { List answer = new ArrayList<>(); for (var x : getIndex(path)) { diff --git a/transport/src/main/java/com/omnia/transport/OmniaTransport.java b/transport/src/main/java/com/omnia/transport/OmniaTransport.java index 8ce4e8f..9e80ad4 100644 --- a/transport/src/main/java/com/omnia/transport/OmniaTransport.java +++ b/transport/src/main/java/com/omnia/transport/OmniaTransport.java @@ -28,15 +28,15 @@ public OmniaTransport(Transport delegate, OmniaSDK sdk) { @Override public ResponseT performRequest(RequestT request, Endpoint endpoint, @Nullable TransportOptions options) throws IOException { final OmniaEndpoint customEndpoint = new OmniaEndpoint<>(endpoint, sdk); - if(request instanceof SearchRequest){ + if (request instanceof SearchRequest) { Query combinedQuery = ((SearchRequest) request).query(); List Indexes = customEndpoint.getIndex(endpoint.requestUrl(request)); - for(var x: Indexes) { - combinedQuery = sdk.addIndexFilter(combinedQuery,x ); + for (var x : Indexes) { + combinedQuery = sdk.addIndexFilter(combinedQuery, x); } - return delegate.performRequest(((RequestT)new SearchRequest.Builder().query(combinedQuery).index(((SearchRequest) request).index()).build()), customEndpoint, options); + return delegate.performRequest(((RequestT) new SearchRequest.Builder().query(combinedQuery).index(((SearchRequest) request).index()).build()), customEndpoint, options); } - return delegate.performRequest(request,customEndpoint,options); + return delegate.performRequest(request, customEndpoint, options); } @Override diff --git a/transport/src/test/java/com/omnia/transport/OmniaEndpointTest.java b/transport/src/test/java/com/omnia/transport/OmniaEndpointTest.java index e8e563b..556f488 100644 --- a/transport/src/test/java/com/omnia/transport/OmniaEndpointTest.java +++ b/transport/src/test/java/com/omnia/transport/OmniaEndpointTest.java @@ -91,7 +91,7 @@ private static OpenSearchClient createOpenSearchClient() { OpenSearchTransport transport = new RestClientTransport(restClient, new org.opensearch.client.json.jackson.JacksonJsonpMapper()); - OpenSearchTransport omniatransport = new OmniaTransport(transport,sdk); + OpenSearchTransport omniatransport = new OmniaTransport(transport, sdk); return new OpenSearchClient(omniatransport); } @@ -119,7 +119,7 @@ private static void indexTestDocument(String name, String id) throws IOException @Test void testCreateSearchRequestBuilder() throws IOException { Query baseQuery = new Query.Builder().matchAll(m -> m).build(); - SearchRequest request =new SearchRequest.Builder() + SearchRequest request = new SearchRequest.Builder() .index("123").query(baseQuery).build(); SearchResponse response = openSearchClient.search(request, Object.class); assertEquals(1, response.hits().hits().size(), "Should find one document"); From f4a1f24070bb72baeacd9f4e3c5854675baa598d Mon Sep 17 00:00:00 2001 From: Egor Date: Tue, 13 May 2025 16:46:36 +0300 Subject: [PATCH 10/17] performRequestAsync --- .../src/main/java/com/omnia/transport/OmniaTransport.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/transport/src/main/java/com/omnia/transport/OmniaTransport.java b/transport/src/main/java/com/omnia/transport/OmniaTransport.java index 9e80ad4..45100df 100644 --- a/transport/src/main/java/com/omnia/transport/OmniaTransport.java +++ b/transport/src/main/java/com/omnia/transport/OmniaTransport.java @@ -42,6 +42,14 @@ public ResponseT performRequest(RequestT request, @Override public CompletableFuture performRequestAsync(RequestT request, Endpoint endpoint, @Nullable TransportOptions options) { final OmniaEndpoint customEndpoint = new OmniaEndpoint<>(endpoint, sdk); + if (request instanceof SearchRequest) { + Query combinedQuery = ((SearchRequest) request).query(); + List Indexes = customEndpoint.getIndex(endpoint.requestUrl(request)); + for (var x : Indexes) { + combinedQuery = sdk.addIndexFilter(combinedQuery, x); + } + return delegate.performRequestAsync(((RequestT) new SearchRequest.Builder().query(combinedQuery).index(((SearchRequest) request).index()).build()), customEndpoint, options); + } return delegate.performRequestAsync(request, customEndpoint, options); } From ca08227181c8ca00edb1f1c54e9188858af8bf21 Mon Sep 17 00:00:00 2001 From: Egor Date: Tue, 13 May 2025 16:51:56 +0300 Subject: [PATCH 11/17] small stylefix --- .../java/com/omnia/transport/OmniaEndpoint.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java b/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java index af6c1da..0e48cdc 100644 --- a/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java +++ b/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java @@ -35,19 +35,18 @@ public String method(RequestT request) { @Override public String requestUrl(RequestT request) throws IllegalArgumentException { - List AA = List.of(endpoint.requestUrl(request).split("/")); + List splitedPath = List.of(endpoint.requestUrl(request).split("/")); List Indecies = parseUrl(endpoint.requestUrl(request)); StringBuilder answer = new StringBuilder("/" + String.join("%2C", Indecies)); - if (AA.size() <= 2) { + if (splitedPath.size() <= 2) { return answer.toString(); } answer.append("/"); - for (int i = 2; i < AA.size() - 1; i++) { - answer.append(AA.get(i)).append("/"); + for (int i = 2; i < splitedPath.size() - 1; i++) { + answer.append(splitedPath.get(i)).append("/"); } - answer.append(AA.getLast()); - String a = answer.toString(); - return a; + answer.append(splitedPath.getLast()); + return answer.toString(); } @Override From 6b457702b9586ff052deb16fd2d1f7600305de39 Mon Sep 17 00:00:00 2001 From: Egor Date: Wed, 4 Jun 2025 04:00:45 +0300 Subject: [PATCH 12/17] add reflection --- .../com/omnia/transport/OmniaEndpoint.java | 24 -------------- .../com/omnia/transport/OmniaTransport.java | 16 ++++++++-- .../java/com/omnia/transport/QueryMapper.java | 31 +++++++++++++++++++ 3 files changed, 45 insertions(+), 26 deletions(-) diff --git a/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java b/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java index 0e48cdc..73f3c5e 100644 --- a/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java +++ b/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java @@ -51,30 +51,6 @@ public String requestUrl(RequestT request) throws IllegalArgumentException { @Override public Map queryParameters(RequestT request) { - /* - Map a = endpoint.queryParameters(request); - for(var x: a.keySet()){ - System.out.println(x+" "+a.get(x)); - } - Map params = endpoint.queryParameters(request); - QueryMapper mapper = new QueryMapper(); - Query query = mapper.mapToQuery(params); - Map answer = new HashMap<>(); - List parsePath = List.of(endpoint.requestUrl(request).split("/")); - List Indecies = List.of(parsePath.get(1).split("%2C")); - for (var x : Indecies) { - if(request instanceof SearchRequest) { - query = sdk.addIndexFilter(query, x); - // Проблема. Кажется, что endpoint.queryParameters не все параметры Query -> если добавлять в endpoint.queryParameters лишнее падаем с исключением от RestTransport - } - } - - mapper.queryToMap(query, answer); - for(var x: answer.keySet()){ - System.out.println("Answer: "+x+" "+answer.get(x)); - } - return answer; - */ return endpoint.queryParameters(request); } diff --git a/transport/src/main/java/com/omnia/transport/OmniaTransport.java b/transport/src/main/java/com/omnia/transport/OmniaTransport.java index 45100df..29bfa41 100644 --- a/transport/src/main/java/com/omnia/transport/OmniaTransport.java +++ b/transport/src/main/java/com/omnia/transport/OmniaTransport.java @@ -28,13 +28,19 @@ public OmniaTransport(Transport delegate, OmniaSDK sdk) { @Override public ResponseT performRequest(RequestT request, Endpoint endpoint, @Nullable TransportOptions options) throws IOException { final OmniaEndpoint customEndpoint = new OmniaEndpoint<>(endpoint, sdk); + QueryMapper mapper = new QueryMapper(); if (request instanceof SearchRequest) { Query combinedQuery = ((SearchRequest) request).query(); List Indexes = customEndpoint.getIndex(endpoint.requestUrl(request)); for (var x : Indexes) { combinedQuery = sdk.addIndexFilter(combinedQuery, x); } - return delegate.performRequest(((RequestT) new SearchRequest.Builder().query(combinedQuery).index(((SearchRequest) request).index()).build()), customEndpoint, options); + try { + mapper.updatePrivateFields(request, combinedQuery); + return delegate.performRequest(request, customEndpoint, options); + } catch (IllegalAccessException |NoSuchFieldException e) { + throw new RuntimeException(e); + } } return delegate.performRequest(request, customEndpoint, options); } @@ -42,13 +48,19 @@ public ResponseT performRequest(RequestT request, @Override public CompletableFuture performRequestAsync(RequestT request, Endpoint endpoint, @Nullable TransportOptions options) { final OmniaEndpoint customEndpoint = new OmniaEndpoint<>(endpoint, sdk); + QueryMapper mapper = new QueryMapper(); if (request instanceof SearchRequest) { Query combinedQuery = ((SearchRequest) request).query(); List Indexes = customEndpoint.getIndex(endpoint.requestUrl(request)); for (var x : Indexes) { combinedQuery = sdk.addIndexFilter(combinedQuery, x); } - return delegate.performRequestAsync(((RequestT) new SearchRequest.Builder().query(combinedQuery).index(((SearchRequest) request).index()).build()), customEndpoint, options); + try { + mapper.updatePrivateFields(request, combinedQuery); + return delegate.performRequestAsync(request, customEndpoint, options); + } catch (IllegalAccessException |NoSuchFieldException e) { + throw new RuntimeException(e); + } } return delegate.performRequestAsync(request, customEndpoint, options); } diff --git a/transport/src/main/java/com/omnia/transport/QueryMapper.java b/transport/src/main/java/com/omnia/transport/QueryMapper.java index 39709e2..f3dcdee 100644 --- a/transport/src/main/java/com/omnia/transport/QueryMapper.java +++ b/transport/src/main/java/com/omnia/transport/QueryMapper.java @@ -1,10 +1,12 @@ package com.omnia.transport; +import org.opensearch.client.Request; import org.opensearch.client.opensearch._types.query_dsl.BoolQuery; import org.opensearch.client.opensearch._types.query_dsl.MatchQuery; import org.opensearch.client.opensearch._types.query_dsl.Query; import org.opensearch.client.opensearch._types.query_dsl.TermQuery; +import java.lang.reflect.Field; import java.util.Map; public class QueryMapper { @@ -40,4 +42,33 @@ public void queryToMap(Query query, Map result) { bool.filter().forEach(q -> queryToMap(q, result)); } } + + public void updatePrivateFields(Object target, Query fieldValues) + throws NoSuchFieldException, IllegalAccessException { + Class clazz = target.getClass(); + Field field = findField(clazz); + if (field == null) { + throw new NoSuchFieldException( + "Field '" + field.getName() + "' not found in class hierarchy"); + } + try { + field.setAccessible(true); + field.set(target, fieldValues); + } catch (SecurityException e) { + throw new IllegalAccessException("Security manager blocked access to field: " + e.getMessage()); + } + } + + private static Field findField(Class clazz) { + Class currentClass = clazz; + while (currentClass != null) { + try { + return currentClass.getDeclaredField("query"); + } catch (NoSuchFieldException e) { + currentClass = currentClass.getSuperclass(); + } + } + return null; + } + } From 9f82c2761f65f344f02385db5908134fa73f2fa1 Mon Sep 17 00:00:00 2001 From: Egor Date: Wed, 4 Jun 2025 05:12:41 +0300 Subject: [PATCH 13/17] add reflection for all RequestT --- .../com/omnia/transport/OmniaTransport.java | 53 +++++++++--------- .../java/com/omnia/transport/QueryMapper.java | 56 +++++++++++++++---- 2 files changed, 73 insertions(+), 36 deletions(-) diff --git a/transport/src/main/java/com/omnia/transport/OmniaTransport.java b/transport/src/main/java/com/omnia/transport/OmniaTransport.java index 29bfa41..87bb481 100644 --- a/transport/src/main/java/com/omnia/transport/OmniaTransport.java +++ b/transport/src/main/java/com/omnia/transport/OmniaTransport.java @@ -3,6 +3,7 @@ import org.opensearch.client.json.JsonpMapper; import org.opensearch.client.opensearch._types.query_dsl.Query; import org.opensearch.client.opensearch.core.SearchRequest; +import org.opensearch.client.opensearch.indices.CreateIndexRequest; import org.opensearch.client.transport.Endpoint; import org.opensearch.client.transport.OpenSearchTransport; import org.opensearch.client.transport.Transport; @@ -29,40 +30,40 @@ public OmniaTransport(Transport delegate, OmniaSDK sdk) { public ResponseT performRequest(RequestT request, Endpoint endpoint, @Nullable TransportOptions options) throws IOException { final OmniaEndpoint customEndpoint = new OmniaEndpoint<>(endpoint, sdk); QueryMapper mapper = new QueryMapper(); - if (request instanceof SearchRequest) { - Query combinedQuery = ((SearchRequest) request).query(); - List Indexes = customEndpoint.getIndex(endpoint.requestUrl(request)); - for (var x : Indexes) { - combinedQuery = sdk.addIndexFilter(combinedQuery, x); - } - try { - mapper.updatePrivateFields(request, combinedQuery); - return delegate.performRequest(request, customEndpoint, options); - } catch (IllegalAccessException |NoSuchFieldException e) { - throw new RuntimeException(e); - } + Query combinedQuery = (Query) mapper.executeQuery(request); + if(combinedQuery == null){ + return delegate.performRequest(request, customEndpoint, options); + } + List Indexes = customEndpoint.getIndex(endpoint.requestUrl(request)); + for (var x : Indexes) { + combinedQuery = sdk.addIndexFilter(combinedQuery, x); + } + try { + mapper.updatePrivateFields(request, combinedQuery); + return delegate.performRequest(request, customEndpoint, options); + } catch (IllegalAccessException |NoSuchFieldException e) { + throw new RuntimeException(e); } - return delegate.performRequest(request, customEndpoint, options); } @Override public CompletableFuture performRequestAsync(RequestT request, Endpoint endpoint, @Nullable TransportOptions options) { final OmniaEndpoint customEndpoint = new OmniaEndpoint<>(endpoint, sdk); QueryMapper mapper = new QueryMapper(); - if (request instanceof SearchRequest) { - Query combinedQuery = ((SearchRequest) request).query(); - List Indexes = customEndpoint.getIndex(endpoint.requestUrl(request)); - for (var x : Indexes) { - combinedQuery = sdk.addIndexFilter(combinedQuery, x); - } - try { - mapper.updatePrivateFields(request, combinedQuery); - return delegate.performRequestAsync(request, customEndpoint, options); - } catch (IllegalAccessException |NoSuchFieldException e) { - throw new RuntimeException(e); - } + Query combinedQuery = (Query) mapper.executeQuery(request); + if(combinedQuery == null){ + return delegate.performRequestAsync(request, customEndpoint, options); + } + List Indexes = customEndpoint.getIndex(endpoint.requestUrl(request)); + for (var x : Indexes) { + combinedQuery = sdk.addIndexFilter(combinedQuery, x); + } + try { + mapper.updatePrivateFields(request, combinedQuery); + return delegate.performRequestAsync(request, customEndpoint, options); + } catch (IllegalAccessException |NoSuchFieldException e) { + throw new RuntimeException(e); } - return delegate.performRequestAsync(request, customEndpoint, options); } @Override diff --git a/transport/src/main/java/com/omnia/transport/QueryMapper.java b/transport/src/main/java/com/omnia/transport/QueryMapper.java index f3dcdee..44623ff 100644 --- a/transport/src/main/java/com/omnia/transport/QueryMapper.java +++ b/transport/src/main/java/com/omnia/transport/QueryMapper.java @@ -7,6 +7,8 @@ import org.opensearch.client.opensearch._types.query_dsl.TermQuery; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.Map; public class QueryMapper { @@ -47,17 +49,17 @@ public void updatePrivateFields(Object target, Query fieldValues) throws NoSuchFieldException, IllegalAccessException { Class clazz = target.getClass(); Field field = findField(clazz); - if (field == null) { - throw new NoSuchFieldException( - "Field '" + field.getName() + "' not found in class hierarchy"); - } - try { - field.setAccessible(true); - field.set(target, fieldValues); - } catch (SecurityException e) { - throw new IllegalAccessException("Security manager blocked access to field: " + e.getMessage()); - } + if (field == null) { + throw new NoSuchFieldException( + "Field '" + field.getName() + "' not found in class hierarchy"); + } + try { + field.setAccessible(true); + field.set(target, fieldValues); + } catch (SecurityException e) { + throw new IllegalAccessException("Security manager blocked access to field: " + e.getMessage()); } + } private static Field findField(Class clazz) { Class currentClass = clazz; @@ -71,4 +73,38 @@ private static Field findField(Class clazz) { return null; } + public Object executeQuery(Object obj) { + if (obj == null) { + return null; + } + try { + Method queryMethod = findQueryMethod(obj.getClass()); + if (queryMethod != null) { + queryMethod.setAccessible(true); + return queryMethod.invoke(obj); + } + } catch (InvocationTargetException e) { + Throwable cause = e.getCause(); + if (cause instanceof RuntimeException) { + throw (RuntimeException) cause; + } else { + throw new RuntimeException("Query method execution failed", cause); + } + } catch (IllegalAccessException e) { + throw new RuntimeException("Access denied for query method", e); + } + return null; + } + + private static Method findQueryMethod(Class clazz) { + while (clazz != null) { + for (Method method : clazz.getDeclaredMethods()) { + if ("query".equals(method.getName()) && method.getParameterCount() == 0) { + return method; + } + } + clazz = clazz.getSuperclass(); + } + return null; + } } From 80130c2b2f4672645ef1e005a30df6a538b7e614 Mon Sep 17 00:00:00 2001 From: Egor Date: Wed, 4 Jun 2025 05:15:57 +0300 Subject: [PATCH 14/17] reformat code --- .../main/java/com/omnia/transport/OmniaEndpoint.java | 8 -------- .../main/java/com/omnia/transport/OmniaTransport.java | 11 ++++------- .../main/java/com/omnia/transport/QueryMapper.java | 1 - 3 files changed, 4 insertions(+), 16 deletions(-) diff --git a/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java b/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java index 73f3c5e..f0a2568 100644 --- a/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java +++ b/transport/src/main/java/com/omnia/transport/OmniaEndpoint.java @@ -1,23 +1,15 @@ package com.omnia.transport; -import org.jooq.Null; import org.opensearch.client.json.JsonpDeserializer; -import org.opensearch.client.json.UnexpectedJsonEventException; -import org.opensearch.client.opensearch._types.FieldValue; -import org.opensearch.client.opensearch._types.OpenSearchException; -import org.opensearch.client.opensearch._types.query_dsl.*; -import org.opensearch.client.opensearch.core.SearchRequest; import org.opensearch.client.transport.Endpoint; import com.omnia.sdk.OmniaSDK; import org.opensearch.client.transport.JsonEndpoint; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.stream.Collectors; public class OmniaEndpoint implements Endpoint, JsonEndpoint { private final Endpoint endpoint; diff --git a/transport/src/main/java/com/omnia/transport/OmniaTransport.java b/transport/src/main/java/com/omnia/transport/OmniaTransport.java index 87bb481..604bb4b 100644 --- a/transport/src/main/java/com/omnia/transport/OmniaTransport.java +++ b/transport/src/main/java/com/omnia/transport/OmniaTransport.java @@ -2,8 +2,6 @@ import org.opensearch.client.json.JsonpMapper; import org.opensearch.client.opensearch._types.query_dsl.Query; -import org.opensearch.client.opensearch.core.SearchRequest; -import org.opensearch.client.opensearch.indices.CreateIndexRequest; import org.opensearch.client.transport.Endpoint; import org.opensearch.client.transport.OpenSearchTransport; import org.opensearch.client.transport.Transport; @@ -12,7 +10,6 @@ import com.omnia.sdk.OmniaSDK; import javax.annotation.Nullable; -import javax.crypto.SealedObject; import java.io.IOException; import java.util.List; import java.util.concurrent.CompletableFuture; @@ -31,7 +28,7 @@ public ResponseT performRequest(RequestT request, final OmniaEndpoint customEndpoint = new OmniaEndpoint<>(endpoint, sdk); QueryMapper mapper = new QueryMapper(); Query combinedQuery = (Query) mapper.executeQuery(request); - if(combinedQuery == null){ + if (combinedQuery == null) { return delegate.performRequest(request, customEndpoint, options); } List Indexes = customEndpoint.getIndex(endpoint.requestUrl(request)); @@ -41,7 +38,7 @@ public ResponseT performRequest(RequestT request, try { mapper.updatePrivateFields(request, combinedQuery); return delegate.performRequest(request, customEndpoint, options); - } catch (IllegalAccessException |NoSuchFieldException e) { + } catch (IllegalAccessException | NoSuchFieldException e) { throw new RuntimeException(e); } } @@ -51,7 +48,7 @@ public CompletableFuture performRequest final OmniaEndpoint customEndpoint = new OmniaEndpoint<>(endpoint, sdk); QueryMapper mapper = new QueryMapper(); Query combinedQuery = (Query) mapper.executeQuery(request); - if(combinedQuery == null){ + if (combinedQuery == null) { return delegate.performRequestAsync(request, customEndpoint, options); } List Indexes = customEndpoint.getIndex(endpoint.requestUrl(request)); @@ -61,7 +58,7 @@ public CompletableFuture performRequest try { mapper.updatePrivateFields(request, combinedQuery); return delegate.performRequestAsync(request, customEndpoint, options); - } catch (IllegalAccessException |NoSuchFieldException e) { + } catch (IllegalAccessException | NoSuchFieldException e) { throw new RuntimeException(e); } } diff --git a/transport/src/main/java/com/omnia/transport/QueryMapper.java b/transport/src/main/java/com/omnia/transport/QueryMapper.java index 44623ff..982a524 100644 --- a/transport/src/main/java/com/omnia/transport/QueryMapper.java +++ b/transport/src/main/java/com/omnia/transport/QueryMapper.java @@ -1,6 +1,5 @@ package com.omnia.transport; -import org.opensearch.client.Request; import org.opensearch.client.opensearch._types.query_dsl.BoolQuery; import org.opensearch.client.opensearch._types.query_dsl.MatchQuery; import org.opensearch.client.opensearch._types.query_dsl.Query; From 8ad56b73e96e8f64506dc714e80ad7087da7a9d5 Mon Sep 17 00:00:00 2001 From: Egor Date: Mon, 16 Jun 2025 20:05:46 +0300 Subject: [PATCH 15/17] small fix? --- .../main/java/com/omnia/transport/QueryMapper.java | 12 ++++++------ .../java/com/omnia/transport/OmniaEndpointTest.java | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/transport/src/main/java/com/omnia/transport/QueryMapper.java b/transport/src/main/java/com/omnia/transport/QueryMapper.java index 982a524..7e3ea37 100644 --- a/transport/src/main/java/com/omnia/transport/QueryMapper.java +++ b/transport/src/main/java/com/omnia/transport/QueryMapper.java @@ -47,20 +47,20 @@ public void queryToMap(Query query, Map result) { public void updatePrivateFields(Object target, Query fieldValues) throws NoSuchFieldException, IllegalAccessException { Class clazz = target.getClass(); - Field field = findField(clazz); - if (field == null) { + Field queryField = findQueryField(clazz); + if (queryField == null) { throw new NoSuchFieldException( - "Field '" + field.getName() + "' not found in class hierarchy"); + "Field 'query' not found in class hierarchy"); } try { - field.setAccessible(true); - field.set(target, fieldValues); + queryField.setAccessible(true); + queryField.set(target, fieldValues); } catch (SecurityException e) { throw new IllegalAccessException("Security manager blocked access to field: " + e.getMessage()); } } - private static Field findField(Class clazz) { + private static Field findQueryField(Class clazz) { Class currentClass = clazz; while (currentClass != null) { try { diff --git a/transport/src/test/java/com/omnia/transport/OmniaEndpointTest.java b/transport/src/test/java/com/omnia/transport/OmniaEndpointTest.java index 556f488..36c72c5 100644 --- a/transport/src/test/java/com/omnia/transport/OmniaEndpointTest.java +++ b/transport/src/test/java/com/omnia/transport/OmniaEndpointTest.java @@ -91,13 +91,13 @@ private static OpenSearchClient createOpenSearchClient() { OpenSearchTransport transport = new RestClientTransport(restClient, new org.opensearch.client.json.jackson.JacksonJsonpMapper()); - OpenSearchTransport omniatransport = new OmniaTransport(transport, sdk); - return new OpenSearchClient(omniatransport); + OpenSearchTransport omniaTransport = new OmniaTransport(transport, sdk); + return new OpenSearchClient(omniaTransport); } private static void createOpenSearchIndex() throws IOException { CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder() - .index("commune") + .index("123") .build(); openSearchClient.indices().create(createIndexRequest); } @@ -107,13 +107,13 @@ private static void indexTestDocument(String name, String id) throws IOException document.put(FEATURE_NAME, name); IndexRequest> indexRequest = new IndexRequest.Builder>() - .index("commune") + .index("123") .id(id) .document(document) .build(); openSearchClient.index(indexRequest); - openSearchClient.indices().refresh(b -> b.index("commune")); // Refresh to make document searchable + openSearchClient.indices().refresh(b -> b.index("123")); } @Test @@ -130,7 +130,7 @@ void testAddIndexFilter() throws IOException { Query baseQuery = new Query.Builder().matchAll(m -> m).build(); SearchRequest request = new SearchRequest.Builder() - .index("123") + .index("456") .query(baseQuery) .build(); From 6bc1e1fc8434bd6d0b3fe0fe0405254eed599349 Mon Sep 17 00:00:00 2001 From: Egor Date: Mon, 16 Jun 2025 22:51:26 +0300 Subject: [PATCH 16/17] fix double resource create --- .../com/omnia/transport/OmniaTransport.java | 32 +++++++++++++++++-- .../omnia/transport/OmniaEndpointTest.java | 4 +++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/transport/src/main/java/com/omnia/transport/OmniaTransport.java b/transport/src/main/java/com/omnia/transport/OmniaTransport.java index 604bb4b..69cfe0d 100644 --- a/transport/src/main/java/com/omnia/transport/OmniaTransport.java +++ b/transport/src/main/java/com/omnia/transport/OmniaTransport.java @@ -1,6 +1,8 @@ package com.omnia.transport; import org.opensearch.client.json.JsonpMapper; +import org.opensearch.client.opensearch._types.ErrorResponse; +import org.opensearch.client.opensearch._types.OpenSearchException; import org.opensearch.client.opensearch._types.query_dsl.Query; import org.opensearch.client.transport.Endpoint; import org.opensearch.client.transport.OpenSearchTransport; @@ -12,6 +14,7 @@ import javax.annotation.Nullable; import java.io.IOException; import java.util.List; +import java.util.Objects; import java.util.concurrent.CompletableFuture; public class OmniaTransport implements OpenSearchTransport { @@ -29,7 +32,15 @@ public ResponseT performRequest(RequestT request, QueryMapper mapper = new QueryMapper(); Query combinedQuery = (Query) mapper.executeQuery(request); if (combinedQuery == null) { - return delegate.performRequest(request, customEndpoint, options); + try { + return delegate.performRequest(request, customEndpoint, options); + } catch (OpenSearchException e) { + if (Objects.equals(e.error().type(), "resource_already_exists_exception")) { + return null; + } + throw new OpenSearchException(e.response()); + } + } List Indexes = customEndpoint.getIndex(endpoint.requestUrl(request)); for (var x : Indexes) { @@ -40,6 +51,11 @@ public ResponseT performRequest(RequestT request, return delegate.performRequest(request, customEndpoint, options); } catch (IllegalAccessException | NoSuchFieldException e) { throw new RuntimeException(e); + } catch (OpenSearchException e) { + if (Objects.equals(e.error().type(), "resource_already_exists_exception")) { + return null; + } + throw new OpenSearchException(e.response()); } } @@ -49,7 +65,14 @@ public CompletableFuture performRequest QueryMapper mapper = new QueryMapper(); Query combinedQuery = (Query) mapper.executeQuery(request); if (combinedQuery == null) { - return delegate.performRequestAsync(request, customEndpoint, options); + try { + return delegate.performRequestAsync(request, customEndpoint, options); + } catch (OpenSearchException e) { + if (Objects.equals(e.error().type(), "resource_already_exists_exception")) { + return null; + } + throw new OpenSearchException(e.response()); + } } List Indexes = customEndpoint.getIndex(endpoint.requestUrl(request)); for (var x : Indexes) { @@ -60,6 +83,11 @@ public CompletableFuture performRequest return delegate.performRequestAsync(request, customEndpoint, options); } catch (IllegalAccessException | NoSuchFieldException e) { throw new RuntimeException(e); + } catch (OpenSearchException e) { + if (Objects.equals(e.error().type(), "resource_already_exists_exception")) { + return null; + } + throw new OpenSearchException(e.response()); } } diff --git a/transport/src/test/java/com/omnia/transport/OmniaEndpointTest.java b/transport/src/test/java/com/omnia/transport/OmniaEndpointTest.java index 36c72c5..6cc3fcc 100644 --- a/transport/src/test/java/com/omnia/transport/OmniaEndpointTest.java +++ b/transport/src/test/java/com/omnia/transport/OmniaEndpointTest.java @@ -100,6 +100,10 @@ private static void createOpenSearchIndex() throws IOException { .index("123") .build(); openSearchClient.indices().create(createIndexRequest); + createIndexRequest = new CreateIndexRequest.Builder() + .index("456") + .build(); + openSearchClient.indices().create(createIndexRequest); } private static void indexTestDocument(String name, String id) throws IOException { From 95d4b8b97f28de0f15ee2e3b13aec1f40ec9a078 Mon Sep 17 00:00:00 2001 From: Egor Date: Tue, 17 Jun 2025 13:22:36 +0300 Subject: [PATCH 17/17] small correctnes fix? --- .../main/java/com/omnia/transport/OmniaTransport.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/transport/src/main/java/com/omnia/transport/OmniaTransport.java b/transport/src/main/java/com/omnia/transport/OmniaTransport.java index 69cfe0d..31e23dc 100644 --- a/transport/src/main/java/com/omnia/transport/OmniaTransport.java +++ b/transport/src/main/java/com/omnia/transport/OmniaTransport.java @@ -4,6 +4,7 @@ import org.opensearch.client.opensearch._types.ErrorResponse; import org.opensearch.client.opensearch._types.OpenSearchException; import org.opensearch.client.opensearch._types.query_dsl.Query; +import org.opensearch.client.opensearch.indices.CreateIndexRequest; import org.opensearch.client.transport.Endpoint; import org.opensearch.client.transport.OpenSearchTransport; import org.opensearch.client.transport.Transport; @@ -35,7 +36,7 @@ public ResponseT performRequest(RequestT request, try { return delegate.performRequest(request, customEndpoint, options); } catch (OpenSearchException e) { - if (Objects.equals(e.error().type(), "resource_already_exists_exception")) { + if (Objects.equals(e.error().type(), "resource_already_exists_exception") && request instanceof CreateIndexRequest) { return null; } throw new OpenSearchException(e.response()); @@ -52,7 +53,7 @@ public ResponseT performRequest(RequestT request, } catch (IllegalAccessException | NoSuchFieldException e) { throw new RuntimeException(e); } catch (OpenSearchException e) { - if (Objects.equals(e.error().type(), "resource_already_exists_exception")) { + if (Objects.equals(e.error().type(), "resource_already_exists_exception") && request instanceof CreateIndexRequest) { return null; } throw new OpenSearchException(e.response()); @@ -68,7 +69,7 @@ public CompletableFuture performRequest try { return delegate.performRequestAsync(request, customEndpoint, options); } catch (OpenSearchException e) { - if (Objects.equals(e.error().type(), "resource_already_exists_exception")) { + if (Objects.equals(e.error().type(), "resource_already_exists_exception") && request instanceof CreateIndexRequest) { return null; } throw new OpenSearchException(e.response()); @@ -84,7 +85,7 @@ public CompletableFuture performRequest } catch (IllegalAccessException | NoSuchFieldException e) { throw new RuntimeException(e); } catch (OpenSearchException e) { - if (Objects.equals(e.error().type(), "resource_already_exists_exception")) { + if (Objects.equals(e.error().type(), "resource_already_exists_exception") && request instanceof CreateIndexRequest) { return null; } throw new OpenSearchException(e.response());