Search issues: Revert to using queryStringBuilder (#20337)

* Search issues: Revert to using queryStringBuilder

* Search issues: Revert to using queryStringBuilder

* Search issues: Revert to using queryStringBuilder
This commit is contained in:
Sriharsha Chintalapani 2025-03-19 03:57:00 -07:00 committed by GitHub
parent 0f714f21a5
commit e2cce8372e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 52 additions and 3 deletions

View File

@ -8,6 +8,7 @@ import static org.openmetadata.service.search.SearchUtil.isTimeSeriesIndex;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import org.openmetadata.schema.api.search.AssetTypeConfiguration;
import org.openmetadata.schema.api.search.SearchSettings;
import org.openmetadata.service.Entity;
@ -23,6 +24,23 @@ import org.openmetadata.service.Entity;
*/
public interface SearchSourceBuilderFactory<S, Q, H, F> {
Pattern QUERY_SYNTAX_PATTERN =
Pattern.compile(
"\\w+\\s*:\\s*\\w+|"
+ // Field queries (field:value)
"\\b(?i)(?:AND|OR|NOT)\\b|"
+ // Boolean operators
"[*?]|"
+ // Wildcards
"[()]|"
+ // Parentheses
"\"|"
+ // Quotes
"\\[.+\\s+TO\\s+.+\\]|"
+ // Range queries
"[+\\-~\\^]" // Special operators
);
/**
* Get the appropriate search source builder based on the index name.
*
@ -207,4 +225,12 @@ public interface SearchSourceBuilderFactory<S, Q, H, F> {
S searchBuilder(Q queryBuilder, H highlightBuilder, int from, int size);
S addAggregationsToNLQQuery(S searchSourceBuilder, String indexName);
default boolean containsQuerySyntax(String query) {
if (query == null || query.isEmpty()) {
return false;
}
query = query.replace("%20", " ").trim();
return QUERY_SYNTAX_PATTERN.matcher(query).find();
}
}

View File

@ -153,6 +153,17 @@ public class ElasticSearchSourceBuilderFactory
if (query == null || query.trim().isEmpty() || query.trim().equals("*")) {
baseQuery.must(QueryBuilders.matchAllQuery());
} else if (containsQuerySyntax(query)) {
QueryStringQueryBuilder queryStringBuilder =
QueryBuilders.queryStringQuery(query)
.fields(fields)
.defaultOperator(Operator.AND)
.type(MultiMatchQueryBuilder.Type.MOST_FIELDS)
.fuzziness(Fuzziness.AUTO)
.fuzzyPrefixLength(1)
.tieBreaker(0.3f);
baseQuery.must(queryStringBuilder);
} else {
MultiMatchQueryBuilder multiMatchQueryBuilder =
QueryBuilders.multiMatchQuery(query)

View File

@ -3,6 +3,7 @@ package org.openmetadata.service.search.opensearch;
import static org.openmetadata.common.utils.CommonUtil.listOrEmpty;
import static org.openmetadata.service.search.EntityBuilderConstant.POST_TAG;
import static org.openmetadata.service.search.EntityBuilderConstant.PRE_TAG;
import static os.org.opensearch.index.query.MultiMatchQueryBuilder.Type.MOST_FIELDS;
import java.util.ArrayList;
import java.util.HashMap;
@ -47,7 +48,7 @@ public class OpenSearchSourceBuilderFactory
public QueryStringQueryBuilder buildSearchQueryBuilder(String query, Map<String, Float> fields) {
return QueryBuilders.queryStringQuery(query)
.fields(fields)
.type(MultiMatchQueryBuilder.Type.MOST_FIELDS)
.type(MOST_FIELDS)
.defaultOperator(Operator.AND)
.fuzziness(Fuzziness.AUTO)
.fuzzyPrefixLength(3)
@ -152,6 +153,17 @@ public class OpenSearchSourceBuilderFactory
BoolQueryBuilder baseQuery = QueryBuilders.boolQuery();
if (query == null || query.trim().isEmpty() || query.trim().equals("*")) {
baseQuery.must(QueryBuilders.matchAllQuery());
} else if (containsQuerySyntax(query)) {
QueryStringQueryBuilder queryStringBuilder =
QueryBuilders.queryStringQuery(query)
.fields(fields)
.defaultOperator(Operator.AND)
.type(MultiMatchQueryBuilder.Type.MOST_FIELDS)
.fuzziness(Fuzziness.AUTO)
.fuzzyPrefixLength(1)
.tieBreaker(0.3f);
baseQuery.must(queryStringBuilder);
} else {
MultiMatchQueryBuilder multiMatchQueryBuilder =
QueryBuilders.multiMatchQuery(query)

View File

@ -56,12 +56,12 @@
"field": "databaseSchema.name.keyword"
},
{
"name": "database.displayName.keyword",
"name": "database.displayName",
"type": "terms",
"field": "database.displayName.keyword"
},
{
"name": "databaseSchema.displayName.keyword",
"name": "databaseSchema.displayName",
"type": "terms",
"field": "databaseSchema.displayName.keyword"
}