generated from amazon-archives/__template_Custom
-
Notifications
You must be signed in to change notification settings - Fork 180
Search command revamp. #4152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
penghuo
merged 1 commit into
opensearch-project:main
from
vamsimanohar:free_text_search
Sep 15, 2025
+3,768
−237
Merged
Search command revamp. #4152
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
34 changes: 34 additions & 0 deletions
34
core/src/main/java/org/opensearch/sql/ast/expression/SearchAnd.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.ast.expression; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import lombok.EqualsAndHashCode; | ||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.ToString; | ||
|
|
||
| /** Search expression for AND operator. */ | ||
| @Getter | ||
| @RequiredArgsConstructor | ||
| @EqualsAndHashCode(callSuper = false) | ||
| @ToString | ||
| public class SearchAnd extends SearchExpression { | ||
|
|
||
| private final SearchExpression left; | ||
| private final SearchExpression right; | ||
|
|
||
| @Override | ||
| public String toQueryString() { | ||
| return left.toQueryString() + " AND " + right.toQueryString(); | ||
| } | ||
|
|
||
| @Override | ||
| public List<? extends UnresolvedExpression> getChild() { | ||
| return Arrays.asList(left, right); | ||
| } | ||
| } |
70 changes: 70 additions & 0 deletions
70
core/src/main/java/org/opensearch/sql/ast/expression/SearchComparison.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.ast.expression; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import lombok.EqualsAndHashCode; | ||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.ToString; | ||
| import org.opensearch.sql.utils.QueryStringUtils; | ||
|
|
||
| /** Search expression for field comparisons. */ | ||
| @Getter | ||
| @RequiredArgsConstructor | ||
| @EqualsAndHashCode(callSuper = false) | ||
| @ToString | ||
| public class SearchComparison extends SearchExpression { | ||
|
|
||
| public enum Operator { | ||
| EQUALS("="), | ||
| NOT_EQUALS("!="), | ||
| LESS_THAN("<"), | ||
| LESS_OR_EQUAL("<="), | ||
| GREATER_THAN(">"), | ||
| GREATER_OR_EQUAL(">="); | ||
|
|
||
| private final String symbol; | ||
|
|
||
| Operator(String symbol) { | ||
| this.symbol = symbol; | ||
| } | ||
|
|
||
| public String getSymbol() { | ||
| return symbol; | ||
| } | ||
| } | ||
|
|
||
| private final Field field; | ||
| private final Operator operator; | ||
| private final SearchLiteral value; | ||
|
|
||
| @Override | ||
| public String toQueryString() { | ||
| String fieldName = QueryStringUtils.escapeFieldName(field.getField().toString()); | ||
vamsimanohar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| String valueStr = value.toQueryString(); | ||
| switch (operator) { | ||
| case NOT_EQUALS: | ||
| return "( _exists_:" + fieldName + " AND NOT " + fieldName + ":" + valueStr + " )"; | ||
| case GREATER_THAN: | ||
| return fieldName + ":>" + valueStr; | ||
| case GREATER_OR_EQUAL: | ||
| return fieldName + ":>=" + valueStr; | ||
| case LESS_THAN: | ||
| return fieldName + ":<" + valueStr; | ||
| case LESS_OR_EQUAL: | ||
| return fieldName + ":<=" + valueStr; | ||
| default: | ||
| return fieldName + ":" + valueStr; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public List<? extends UnresolvedExpression> getChild() { | ||
| return Arrays.asList(field, value); | ||
| } | ||
| } | ||
24 changes: 24 additions & 0 deletions
24
core/src/main/java/org/opensearch/sql/ast/expression/SearchExpression.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.ast.expression; | ||
|
|
||
| import org.opensearch.sql.ast.AbstractNodeVisitor; | ||
|
|
||
| /** Base class for search expressions that get converted to query_string syntax. */ | ||
| public abstract class SearchExpression extends UnresolvedExpression { | ||
|
|
||
| /** | ||
| * Convert this search expression to query_string syntax. | ||
| * | ||
| * @return the query string representation | ||
| */ | ||
| public abstract String toQueryString(); | ||
|
|
||
| @Override | ||
| public <R, C> R accept(AbstractNodeVisitor<R, C> nodeVisitor, C context) { | ||
| return nodeVisitor.visitChildren(this, context); | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
core/src/main/java/org/opensearch/sql/ast/expression/SearchGroup.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.ast.expression; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import lombok.EqualsAndHashCode; | ||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.ToString; | ||
|
|
||
| /** Search expression for grouped expressions (parentheses). */ | ||
| @Getter | ||
| @RequiredArgsConstructor | ||
| @EqualsAndHashCode(callSuper = false) | ||
| @ToString | ||
| public class SearchGroup extends SearchExpression { | ||
|
|
||
| private final SearchExpression expression; | ||
|
|
||
| @Override | ||
| public String toQueryString() { | ||
| return "(" + expression.toQueryString() + ")"; | ||
| } | ||
|
|
||
| @Override | ||
| public List<? extends UnresolvedExpression> getChild() { | ||
| return Collections.singletonList(expression); | ||
| } | ||
| } |
44 changes: 44 additions & 0 deletions
44
core/src/main/java/org/opensearch/sql/ast/expression/SearchIn.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.ast.expression; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
| import lombok.EqualsAndHashCode; | ||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.ToString; | ||
| import org.opensearch.sql.utils.QueryStringUtils; | ||
|
|
||
| /** Search expression for IN operator. */ | ||
| @Getter | ||
| @RequiredArgsConstructor | ||
| @EqualsAndHashCode(callSuper = false) | ||
| @ToString | ||
| public class SearchIn extends SearchExpression { | ||
|
|
||
| private final Field field; | ||
| private final List<SearchLiteral> values; | ||
|
|
||
| @Override | ||
| public String toQueryString() { | ||
| String fieldName = QueryStringUtils.escapeFieldName(field.getField().toString()); | ||
| String valueList = | ||
| values.stream().map(SearchLiteral::toQueryString).collect(Collectors.joining(" OR ")); | ||
|
|
||
| return fieldName + ":( " + valueList + " )"; | ||
| } | ||
|
|
||
| @Override | ||
| public List<? extends UnresolvedExpression> getChild() { | ||
| List<UnresolvedExpression> children = new ArrayList<>(); | ||
| children.add(field); | ||
| // SearchLiteral extends SearchExpression which extends UnresolvedExpression | ||
| children.addAll(values); | ||
| return children; | ||
| } | ||
| } |
62 changes: 62 additions & 0 deletions
62
core/src/main/java/org/opensearch/sql/ast/expression/SearchLiteral.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.ast.expression; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.EqualsAndHashCode; | ||
| import lombok.Getter; | ||
| import lombok.ToString; | ||
| import org.opensearch.sql.utils.QueryStringUtils; | ||
|
|
||
| /** Search expression for standalone literals. */ | ||
| @Getter | ||
| @AllArgsConstructor | ||
| @EqualsAndHashCode(callSuper = false) | ||
| @ToString | ||
| public class SearchLiteral extends SearchExpression { | ||
|
|
||
| private final UnresolvedExpression literal; | ||
| private final boolean isPhrase; | ||
|
|
||
| @Override | ||
| public String toQueryString() { | ||
| if (literal instanceof Literal) { | ||
| Literal lit = (Literal) literal; | ||
| Object val = lit.getValue(); | ||
|
|
||
| // Numbers don't need escaping | ||
| if (val instanceof Number) { | ||
| return val.toString(); | ||
| } | ||
|
|
||
| // Strings | ||
| if (val instanceof String) { | ||
| String str = (String) val; | ||
|
|
||
| // Phrase search - preserve quotes | ||
| if (isPhrase) { | ||
| // Escape special chars inside the phrase | ||
| str = QueryStringUtils.escapeLuceneSpecialCharacters(str); | ||
| return "\"" + str + "\""; | ||
| } | ||
|
|
||
| // Regular string - escape special characters | ||
| return QueryStringUtils.escapeLuceneSpecialCharacters(str); | ||
| } | ||
| } | ||
|
|
||
| // Default: escape the text representation | ||
| String text = literal.toString(); | ||
| return QueryStringUtils.escapeLuceneSpecialCharacters(text); | ||
| } | ||
|
|
||
| @Override | ||
| public List<? extends UnresolvedExpression> getChild() { | ||
| return Collections.singletonList(literal); | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
core/src/main/java/org/opensearch/sql/ast/expression/SearchNot.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.ast.expression; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import lombok.EqualsAndHashCode; | ||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.ToString; | ||
|
|
||
| /** Search expression for NOT operator. */ | ||
| @Getter | ||
| @RequiredArgsConstructor | ||
| @EqualsAndHashCode(callSuper = false) | ||
| @ToString | ||
| public class SearchNot extends SearchExpression { | ||
|
|
||
| private final SearchExpression expression; | ||
|
|
||
| @Override | ||
| public String toQueryString() { | ||
| return "NOT(" + expression.toQueryString() + ")"; | ||
| } | ||
|
|
||
| @Override | ||
| public List<? extends UnresolvedExpression> getChild() { | ||
| return Collections.singletonList(expression); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.