diff options
author | Jon Bratseth <bratseth@oath.com> | 2022-01-17 11:21:53 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-17 11:21:53 +0100 |
commit | e210c0f8ab9c2739dd545964b21a25ac7c1cbc8f (patch) | |
tree | 6a6cd1b1e7b55021d9838eb1b0e875042691d208 /container-search/src/main/java/com/yahoo/prelude/query | |
parent | af0de1790ddcec36d90821fa2fa15d8364ac312e (diff) | |
parent | f367dfb51f08b2bce4b1b4816bcf2fc3a9f0a0ec (diff) |
Merge pull request #20241 from vespa-engine/olaa/add-query-item-limit
Add query item limit
Diffstat (limited to 'container-search/src/main/java/com/yahoo/prelude/query')
-rw-r--r-- | container-search/src/main/java/com/yahoo/prelude/query/QueryCanonicalizer.java | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/container-search/src/main/java/com/yahoo/prelude/query/QueryCanonicalizer.java b/container-search/src/main/java/com/yahoo/prelude/query/QueryCanonicalizer.java index 8c4c5c84a28..a93dd1b9de4 100644 --- a/container-search/src/main/java/com/yahoo/prelude/query/QueryCanonicalizer.java +++ b/container-search/src/main/java/com/yahoo/prelude/query/QueryCanonicalizer.java @@ -1,6 +1,7 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.prelude.query; +import com.yahoo.processing.request.CompoundName; import com.yahoo.search.Query; import com.yahoo.search.query.QueryTree; @@ -19,6 +20,8 @@ public class QueryCanonicalizer { /** The name of the operation performed by this, for use in search chain ordering */ public static final String queryCanonicalization = "queryCanonicalization"; + private static final CompoundName MAX_QUERY_ITEMS = new CompoundName("maxQueryItems"); + /** * Validates this query and carries out possible operations on this query * which simplifies it without changing its semantics. @@ -26,7 +29,17 @@ public class QueryCanonicalizer { * @return null if the query is valid, an error message if it is invalid */ public static String canonicalize(Query query) { - return canonicalize(query.getModel().getQueryTree()); + Integer maxQueryItems = query.properties().getInteger(MAX_QUERY_ITEMS, Integer.MAX_VALUE); + return canonicalize(query.getModel().getQueryTree(), maxQueryItems); + } + + /** + * Canonicalizes this query, allowing any query tree size + * + * @return null if the query is valid, an error message if it is invalid + */ + public static String canonicalize(QueryTree queryTree) { + return canonicalize(queryTree, Integer.MAX_VALUE); } /** @@ -34,10 +47,12 @@ public class QueryCanonicalizer { * * @return null if the query is valid, an error message if it is invalid */ - public static String canonicalize(QueryTree query) { + private static String canonicalize(QueryTree query, Integer maxQueryItems) { ListIterator<Item> rootItemIterator = query.getItemIterator(); CanonicalizationResult result = recursivelyCanonicalize(rootItemIterator.next(), rootItemIterator); if (query.isEmpty() && ! result.isError()) result = CanonicalizationResult.error("No query"); + int itemCount = query.treeSize(); + if (itemCount > maxQueryItems) result = CanonicalizationResult.error(String.format("Query tree exceeds allowed item count. Configured limit: %d - Item count: %d", maxQueryItems, itemCount)); return result.error().orElse(null); // preserve old API, unfortunately } |