From 42a331d31c13c6d265fdb8f982dd5c1f3b9f3b0a Mon Sep 17 00:00:00 2001 From: qaate47 Date: Thu, 14 Nov 2024 12:01:31 +0100 Subject: [PATCH] GH-5149 Rename MAX_QUERY_DOCUMENTS_KEY to MAX_DOCUMENTS_KEY and MAX_DOCUMENTS_KEY to DEFAULT_NUM_DOCS_KEY --- .../elasticsearch/ElasticsearchIndex.java | 8 ++--- .../sail/lucene/AbstractSearchIndex.java | 10 +++---- .../eclipse/rdf4j/sail/lucene/LuceneSail.java | 12 ++++---- .../rdf4j/sail/lucene/impl/LuceneIndex.java | 8 ++--- .../eclipse/rdf4j/sail/solr/SolrIndex.java | 8 ++--- .../sail/lucene/AbstractLuceneSailTest.java | 29 ++++++++++++++++++- 6 files changed, 51 insertions(+), 24 deletions(-) diff --git a/core/sail/elasticsearch/src/main/java/org/eclipse/rdf4j/sail/elasticsearch/ElasticsearchIndex.java b/core/sail/elasticsearch/src/main/java/org/eclipse/rdf4j/sail/elasticsearch/ElasticsearchIndex.java index 65e5e8a150..19846bfc82 100644 --- a/core/sail/elasticsearch/src/main/java/org/eclipse/rdf4j/sail/elasticsearch/ElasticsearchIndex.java +++ b/core/sail/elasticsearch/src/main/java/org/eclipse/rdf4j/sail/elasticsearch/ElasticsearchIndex.java @@ -737,13 +737,13 @@ public SearchHits search(SearchRequestBuilder request, QueryBuilder query, int n String[] types = getTypes(); int nDocs; if (numDocs > 0) { - if (maxQueryDocs > 0 && maxQueryDocs < numDocs) { - nDocs = maxQueryDocs; + if (maxDocs > 0 && maxDocs < numDocs) { + nDocs = maxDocs; } else { nDocs = numDocs; } - } else if (maxDocs > 0) { - nDocs = maxDocs; + } else if (defaultNumDocs > 0) { + nDocs = defaultNumDocs; } else { long docCount = client.prepareSearch(indexName) .setTypes(types) diff --git a/core/sail/lucene-api/src/main/java/org/eclipse/rdf4j/sail/lucene/AbstractSearchIndex.java b/core/sail/lucene-api/src/main/java/org/eclipse/rdf4j/sail/lucene/AbstractSearchIndex.java index 9423e9dd95..e52fc63ab8 100644 --- a/core/sail/lucene-api/src/main/java/org/eclipse/rdf4j/sail/lucene/AbstractSearchIndex.java +++ b/core/sail/lucene-api/src/main/java/org/eclipse/rdf4j/sail/lucene/AbstractSearchIndex.java @@ -65,8 +65,8 @@ public abstract class AbstractSearchIndex implements SearchIndex { REJECTED_DATATYPES.add("http://www.w3.org/2001/XMLSchema#float"); } + protected int defaultNumDocs; protected int maxDocs; - protected int maxQueryDocs; protected Set wktFields = Collections.singleton(SearchFields.getPropertyField(GEO.AS_WKT)); @@ -76,10 +76,10 @@ public abstract class AbstractSearchIndex implements SearchIndex { @Override public void initialize(Properties parameters) throws Exception { - String maxDocParam = parameters.getProperty(LuceneSail.MAX_DOCUMENTS_KEY); - maxDocs = (maxDocParam != null) ? Integer.parseInt(maxDocParam) : -1; - String maxQueryDocParam = parameters.getProperty(LuceneSail.MAX_QUERY_DOCUMENTS_KEY); - maxQueryDocs = (maxQueryDocParam != null) ? Integer.parseInt(maxQueryDocParam) : maxDocs; + String maxDocumentsParam = parameters.getProperty(LuceneSail.MAX_DOCUMENTS_KEY); + maxDocs = (maxDocumentsParam != null) ? Integer.parseInt(maxDocumentsParam) : -1; + String defaultNumDocsParam = parameters.getProperty(LuceneSail.DEFAULT_NUM_DOCS_KEY); + defaultNumDocs = (defaultNumDocsParam != null) ? Integer.parseInt(defaultNumDocsParam) : defaultNumDocs; String wktFieldParam = parameters.getProperty(LuceneSail.WKT_FIELDS); if (wktFieldParam != null) { diff --git a/core/sail/lucene-api/src/main/java/org/eclipse/rdf4j/sail/lucene/LuceneSail.java b/core/sail/lucene-api/src/main/java/org/eclipse/rdf4j/sail/lucene/LuceneSail.java index c3b0d90a66..f2d96ecc43 100644 --- a/core/sail/lucene-api/src/main/java/org/eclipse/rdf4j/sail/lucene/LuceneSail.java +++ b/core/sail/lucene-api/src/main/java/org/eclipse/rdf4j/sail/lucene/LuceneSail.java @@ -290,18 +290,18 @@ public class LuceneSail extends NotifyingSailWrapper { public static final String LUCENE_RAMDIR_KEY = "useramdir"; /** - * Set the key "maxDocuments=<n>" as sail parameter to limit the maximum number of documents to return from a - * search query. The default is to return all documents. NB: this may involve extra cost for some SearchIndex + * Set the key "defaultNumDocs=<n>" as sail parameter to limit the maximum number of documents to return from + * a search query. The default is to return all documents. NB: this may involve extra cost for some SearchIndex * implementations as they may have to determine this number. */ - public static final String MAX_DOCUMENTS_KEY = "maxDocuments"; + public static final String DEFAULT_NUM_DOCS_KEY = "defaultNumDocs"; /** - * Set the key "maxQueryDocuments=<n>" as sail parameter to limit the maximum number of documents the user can - * query at a time to return from a search query. The default is the value of the {@link #MAX_DOCUMENTS_KEY} + * Set the key "maxDocuments=<n>" as sail parameter to limit the maximum number of documents the user can + * query at a time to return from a search query. The default is the value of the {@link #DEFAULT_NUM_DOCS_KEY} * parameter. */ - public static final String MAX_QUERY_DOCUMENTS_KEY = "maxQueryDocuments"; + public static final String MAX_DOCUMENTS_KEY = "maxDocuments"; /** * Set this key to configure which fields contain WKT and should be spatially indexed. The value should be a diff --git a/core/sail/lucene/src/main/java/org/eclipse/rdf4j/sail/lucene/impl/LuceneIndex.java b/core/sail/lucene/src/main/java/org/eclipse/rdf4j/sail/lucene/impl/LuceneIndex.java index 1688522923..5e17fa2b36 100644 --- a/core/sail/lucene/src/main/java/org/eclipse/rdf4j/sail/lucene/impl/LuceneIndex.java +++ b/core/sail/lucene/src/main/java/org/eclipse/rdf4j/sail/lucene/impl/LuceneIndex.java @@ -1006,13 +1006,13 @@ public synchronized TopDocs search(Query query) throws IOException { public synchronized TopDocs search(Query query, int numDocs) throws IOException { int nDocs; if (numDocs > 0) { - if (maxQueryDocs > 0 && maxQueryDocs < numDocs) { - nDocs = maxQueryDocs; + if (maxDocs > 0 && maxDocs < numDocs) { + nDocs = maxDocs; } else { nDocs = numDocs; } - } else if (maxDocs > 0) { - nDocs = maxDocs; + } else if (defaultNumDocs > 0) { + nDocs = defaultNumDocs; } else { nDocs = Math.max(getIndexReader().numDocs(), 1); } diff --git a/core/sail/solr/src/main/java/org/eclipse/rdf4j/sail/solr/SolrIndex.java b/core/sail/solr/src/main/java/org/eclipse/rdf4j/sail/solr/SolrIndex.java index ab9e728232..a7c54396cf 100644 --- a/core/sail/solr/src/main/java/org/eclipse/rdf4j/sail/solr/SolrIndex.java +++ b/core/sail/solr/src/main/java/org/eclipse/rdf4j/sail/solr/SolrIndex.java @@ -585,13 +585,13 @@ public QueryResponse search(SolrQuery query) throws SolrServerException, IOExcep public QueryResponse search(SolrQuery query, int numDocs) throws SolrServerException, IOException { int nDocs; if (numDocs > 0) { - if (maxQueryDocs > 0 && maxQueryDocs < numDocs) { - nDocs = maxQueryDocs; + if (maxDocs > 0 && maxDocs < numDocs) { + nDocs = maxDocs; } else { nDocs = numDocs; } - } else if (maxDocs > 0) { - nDocs = maxDocs; + } else if (defaultNumDocs > 0) { + nDocs = defaultNumDocs; } else { long docCount = client.query(query.setRows(0)).getResults().getNumFound(); nDocs = Math.max((int) Math.min(docCount, Integer.MAX_VALUE), 1); diff --git a/testsuites/lucene/src/main/java/org/eclipse/testsuite/rdf4j/sail/lucene/AbstractLuceneSailTest.java b/testsuites/lucene/src/main/java/org/eclipse/testsuite/rdf4j/sail/lucene/AbstractLuceneSailTest.java index 9d3bda29f3..fa01c118a3 100644 --- a/testsuites/lucene/src/main/java/org/eclipse/testsuite/rdf4j/sail/lucene/AbstractLuceneSailTest.java +++ b/testsuites/lucene/src/main/java/org/eclipse/testsuite/rdf4j/sail/lucene/AbstractLuceneSailTest.java @@ -1097,10 +1097,37 @@ public void run() { assertEquals(0, exceptions.size(), "Exceptions occurred during testMultithreadedAdd, see stacktraces above"); } + @ParameterizedTest + @ValueSource(ints = { 1, 2, 3 }) + public void testDefaultNumDocsResult(int numDoc) { + createTestSail(lc -> lc.setParameter(LuceneSail.DEFAULT_NUM_DOCS_KEY, String.valueOf(numDoc))); + Repositories.consumeNoTransaction(repository, conn -> { + try (TupleQueryResult res = conn.prepareTupleQuery( + "SELECT ?Resource {\n" + + " ?Resource <" + MATCHES + "> [\n " + + " <" + QUERY + "> \"one\"\n " + + " ]. } " + ).evaluate()) { + for (int k = 0; k < numDoc; k++) { + assertTrue(res.hasNext(), "missing result #" + k); + res.next(); + } + if (res.hasNext()) { + StringBuilder b = new StringBuilder(); + int r = 0; + do { + b.append("\n#").append(r++).append(res.next()); + } while (res.hasNext()); + fail("can't have more than " + numDoc + " result(s)" + b); + } + } + }); + } + @ParameterizedTest @ValueSource(ints = { 1, 2, 3 }) public void testMaxNumDocsResult(int numDoc) { - createTestSail(lc -> lc.setParameter(LuceneSail.MAX_QUERY_DOCUMENTS_KEY, String.valueOf(numDoc))); + createTestSail(lc -> lc.setParameter(LuceneSail.MAX_DOCUMENTS_KEY, String.valueOf(numDoc))); Repositories.consumeNoTransaction(repository, conn -> { try (TupleQueryResult res = conn.prepareTupleQuery( "SELECT ?Resource {\n"