-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
(cherry picked from commit 811f4a5) Signed-off-by: Siddhant Deshmukh <[email protected]> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
f2ba6ca
commit 2f706af
Showing
17 changed files
with
1,088 additions
and
46 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
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
102 changes: 102 additions & 0 deletions
102
src/main/java/org/opensearch/plugin/insights/core/service/categorizer/QueryShapeVisitor.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,102 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.plugin.insights.core.service.categorizer; | ||
|
||
import org.apache.lucene.search.BooleanClause; | ||
import org.opensearch.common.SetOnce; | ||
import org.opensearch.index.query.QueryBuilder; | ||
import org.opensearch.index.query.QueryBuilderVisitor; | ||
|
||
import java.util.ArrayList; | ||
import java.util.EnumMap; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
|
||
/** | ||
* Class to traverse the QueryBuilder tree and capture the query shape | ||
*/ | ||
public final class QueryShapeVisitor implements QueryBuilderVisitor { | ||
private final SetOnce<String> queryType = new SetOnce<>(); | ||
private final Map<BooleanClause.Occur, List<QueryShapeVisitor>> childVisitors = new EnumMap<>(BooleanClause.Occur.class); | ||
|
||
@Override | ||
public void accept(QueryBuilder qb) { | ||
queryType.set(qb.getName()); | ||
} | ||
|
||
@Override | ||
public QueryBuilderVisitor getChildVisitor(BooleanClause.Occur occur) { | ||
// Should get called once per Occur value | ||
if (childVisitors.containsKey(occur)) { | ||
throw new IllegalStateException("child visitor already called for " + occur); | ||
} | ||
final List<QueryShapeVisitor> childVisitorList = new ArrayList<>(); | ||
QueryBuilderVisitor childVisitorWrapper = new QueryBuilderVisitor() { | ||
QueryShapeVisitor currentChild; | ||
|
||
@Override | ||
public void accept(QueryBuilder qb) { | ||
currentChild = new QueryShapeVisitor(); | ||
childVisitorList.add(currentChild); | ||
currentChild.accept(qb); | ||
} | ||
|
||
@Override | ||
public QueryBuilderVisitor getChildVisitor(BooleanClause.Occur occur) { | ||
return currentChild.getChildVisitor(occur); | ||
} | ||
}; | ||
childVisitors.put(occur, childVisitorList); | ||
return childVisitorWrapper; | ||
} | ||
|
||
/** | ||
* Convert query builder tree to json | ||
* @return json query builder tree as a string | ||
*/ | ||
public String toJson() { | ||
StringBuilder outputBuilder = new StringBuilder("{\"type\":\"").append(queryType.get()).append("\""); | ||
for (Map.Entry<BooleanClause.Occur, List<QueryShapeVisitor>> entry : childVisitors.entrySet()) { | ||
outputBuilder.append(",\"").append(entry.getKey().name().toLowerCase(Locale.ROOT)).append("\"["); | ||
boolean first = true; | ||
for (QueryShapeVisitor child : entry.getValue()) { | ||
if (!first) { | ||
outputBuilder.append(","); | ||
} | ||
outputBuilder.append(child.toJson()); | ||
first = false; | ||
} | ||
outputBuilder.append("]"); | ||
} | ||
outputBuilder.append("}"); | ||
return outputBuilder.toString(); | ||
} | ||
|
||
/** | ||
* Pretty print the query builder tree | ||
* @param indent indent size | ||
* @return Query builder tree as a pretty string | ||
*/ | ||
public String prettyPrintTree(String indent) { | ||
StringBuilder outputBuilder = new StringBuilder(indent).append(queryType.get()).append("\n"); | ||
for (Map.Entry<BooleanClause.Occur, List<QueryShapeVisitor>> entry : childVisitors.entrySet()) { | ||
outputBuilder.append(indent).append(" ").append(entry.getKey().name().toLowerCase(Locale.ROOT)).append(":\n"); | ||
for (QueryShapeVisitor child : entry.getValue()) { | ||
outputBuilder.append(child.prettyPrintTree(indent + " ")); | ||
} | ||
} | ||
return outputBuilder.toString(); | ||
} | ||
|
||
/** | ||
* Default constructor | ||
*/ | ||
public QueryShapeVisitor() {} | ||
} |
Oops, something went wrong.