Skip to content

Commit

Permalink
GH-5149 Rename MAX_QUERY_DOCUMENTS_KEY to MAX_DOCUMENTS_KEY and MAX_D…
Browse files Browse the repository at this point in the history
…OCUMENTS_KEY to DEFAULT_NUM_DOCS_KEY
  • Loading branch information
ate47 committed Nov 14, 2024
1 parent 3e6454e commit 42a331d
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> wktFields = Collections.singleton(SearchFields.getPropertyField(GEO.AS_WKT));

Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,18 +290,18 @@ public class LuceneSail extends NotifyingSailWrapper {
public static final String LUCENE_RAMDIR_KEY = "useramdir";

/**
* Set the key "maxDocuments=&lt;n&gt;" 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=&lt;n&gt;" 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=&lt;n&gt;" 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=&lt;n&gt;" 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 42a331d

Please sign in to comment.