-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OpenSearch query builders and tests
- Loading branch information
Showing
9 changed files
with
449 additions
and
63 deletions.
There are no files selected for viewing
This file contains 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
151 changes: 151 additions & 0 deletions
151
.../main/java/org/openmetadata/service/search/opensearch/queries/OpenSearchQueryBuilder.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package org.openmetadata.service.search.opensearch.queries; | ||
|
||
import java.util.List; | ||
import org.openmetadata.service.search.queries.OMQueryBuilder; | ||
import os.org.opensearch.index.query.BoolQueryBuilder; | ||
import os.org.opensearch.index.query.MatchAllQueryBuilder; | ||
import os.org.opensearch.index.query.QueryBuilder; | ||
import os.org.opensearch.index.query.QueryBuilders; | ||
|
||
public class OpenSearchQueryBuilder implements OMQueryBuilder { | ||
private boolean isMustNot = false; | ||
private QueryBuilder query; | ||
|
||
public OpenSearchQueryBuilder() { | ||
// Default constructor | ||
} | ||
|
||
public OpenSearchQueryBuilder(QueryBuilder query) { | ||
this.query = query; | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return query == null; | ||
} | ||
|
||
@Override | ||
public boolean isMatchNone() { | ||
// Check if the query is a bool query with must_not match_all | ||
if (query instanceof BoolQueryBuilder) { | ||
BoolQueryBuilder boolQuery = (BoolQueryBuilder) query; | ||
return boolQuery.must().isEmpty() | ||
&& boolQuery.should().isEmpty() | ||
&& boolQuery.mustNot().size() == 1 | ||
&& boolQuery.mustNot().get(0) instanceof MatchAllQueryBuilder; | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean isMatchAll() { | ||
return query instanceof MatchAllQueryBuilder; | ||
} | ||
|
||
@Override | ||
public OMQueryBuilder must(List<OMQueryBuilder> queries) { | ||
BoolQueryBuilder boolQuery = getOrCreateBoolQuery(); | ||
for (OMQueryBuilder q : queries) { | ||
OpenSearchQueryBuilder eqb = (OpenSearchQueryBuilder) q; | ||
boolQuery.must(eqb.build()); | ||
} | ||
this.query = boolQuery; | ||
return this; | ||
} | ||
|
||
@Override | ||
public OMQueryBuilder should(List<OMQueryBuilder> queries) { | ||
BoolQueryBuilder boolQuery = getOrCreateBoolQuery(); | ||
for (OMQueryBuilder q : queries) { | ||
OpenSearchQueryBuilder eqb = (OpenSearchQueryBuilder) q; | ||
boolQuery.should(eqb.build()); | ||
} | ||
this.query = boolQuery; | ||
return this; | ||
} | ||
|
||
@Override | ||
public OMQueryBuilder mustNot(List<OMQueryBuilder> queries) { | ||
BoolQueryBuilder boolQuery = getOrCreateBoolQuery(); | ||
for (OMQueryBuilder q : queries) { | ||
OpenSearchQueryBuilder eqb = (OpenSearchQueryBuilder) q; | ||
boolQuery.mustNot(eqb.build()); | ||
} | ||
this.query = boolQuery; | ||
return this; | ||
} | ||
|
||
@Override | ||
public OMQueryBuilder must(OMQueryBuilder query) { | ||
return must(List.of(query)); | ||
} | ||
|
||
@Override | ||
public OMQueryBuilder should(OMQueryBuilder query) { | ||
return should(List.of(query)); | ||
} | ||
|
||
@Override | ||
public boolean hasClauses() { | ||
if (query instanceof BoolQueryBuilder) { | ||
BoolQueryBuilder boolQuery = (BoolQueryBuilder) query; | ||
return !boolQuery.must().isEmpty() | ||
|| !boolQuery.should().isEmpty() | ||
|| !boolQuery.mustNot().isEmpty(); | ||
} | ||
return query != null; | ||
} | ||
|
||
public QueryBuilder build() { | ||
return query; | ||
} | ||
|
||
public OpenSearchQueryBuilder setQuery(QueryBuilder query) { | ||
this.query = query; | ||
return this; | ||
} | ||
|
||
// Helper methods | ||
|
||
public OpenSearchQueryBuilder matchNoneQuery() { | ||
this.query = QueryBuilders.boolQuery().mustNot(QueryBuilders.matchAllQuery()); | ||
return this; | ||
} | ||
|
||
public OpenSearchQueryBuilder matchAllQuery() { | ||
this.query = QueryBuilders.matchAllQuery(); | ||
return this; | ||
} | ||
|
||
public OpenSearchQueryBuilder boolQuery() { | ||
this.query = QueryBuilders.boolQuery(); | ||
return this; | ||
} | ||
|
||
public OpenSearchQueryBuilder termQuery(String field, String value) { | ||
this.query = QueryBuilders.termQuery(field, value); | ||
return this; | ||
} | ||
|
||
public OpenSearchQueryBuilder termsQuery(String field, List<String> values) { | ||
this.query = QueryBuilders.termsQuery(field, values); | ||
return this; | ||
} | ||
|
||
public OpenSearchQueryBuilder existsQuery(String field) { | ||
this.query = QueryBuilders.existsQuery(field); | ||
return this; | ||
} | ||
|
||
private BoolQueryBuilder getOrCreateBoolQuery() { | ||
if (query instanceof BoolQueryBuilder) { | ||
return (BoolQueryBuilder) query; | ||
} else { | ||
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery(); | ||
if (query != null) { | ||
boolQuery.must(query); | ||
} | ||
return boolQuery; | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...ava/org/openmetadata/service/search/opensearch/queries/OpenSearchQueryBuilderFactory.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.openmetadata.service.search.opensearch.queries; | ||
|
||
import java.util.List; | ||
import org.openmetadata.service.search.queries.OMQueryBuilder; | ||
import org.openmetadata.service.search.queries.QueryBuilderFactory; | ||
|
||
public class OpenSearchQueryBuilderFactory implements QueryBuilderFactory { | ||
|
||
@Override | ||
public OMQueryBuilder matchNoneQuery() { | ||
return new OpenSearchQueryBuilder().matchNoneQuery(); | ||
} | ||
|
||
@Override | ||
public OMQueryBuilder matchAllQuery() { | ||
return new OpenSearchQueryBuilder().matchAllQuery(); | ||
} | ||
|
||
@Override | ||
public OMQueryBuilder boolQuery() { | ||
return new OpenSearchQueryBuilder().boolQuery(); | ||
} | ||
|
||
@Override | ||
public OMQueryBuilder termQuery(String field, String value) { | ||
return new OpenSearchQueryBuilder().termQuery(field, value); | ||
} | ||
|
||
@Override | ||
public OMQueryBuilder termsQuery(String field, List<String> values) { | ||
return new OpenSearchQueryBuilder().termsQuery(field, values); | ||
} | ||
|
||
@Override | ||
public OMQueryBuilder existsQuery(String field) { | ||
return new OpenSearchQueryBuilder().existsQuery(field); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.