This is an automated email from the ASF dual-hosted git repository. dsmiley pushed a commit to branch branch_10x in repository https://gitbox.apache.org/repos/asf/solr.git
commit 42ed629da6116e4a5634ae1962490a6d520c73e8 Author: Xinyao Zhang <[email protected]> AuthorDate: Sun Sep 20 16:12:54 2026 -0400 SOLR-18175: Use HTTP status 400 (not 500) for unsupported field existence queries (#4915) (cherry picked from commit b50f0a7f578d6e6c87fda95972d8d7742ca3569e) --- .../unreleased/solr-18175-field-exists-400.yml | 8 ++++ .../src/java/org/apache/solr/schema/FieldType.java | 20 +++++++- .../solr/collection1/conf/schema-customfield.xml | 2 + .../solr/schema/MissingDocValuesFieldType.java | 31 ++++++++++++ .../search/FieldExistsQueryBadRequestTest.java | 55 ++++++++++++++++++++++ 5 files changed, 114 insertions(+), 2 deletions(-) diff --git a/changelog/unreleased/solr-18175-field-exists-400.yml b/changelog/unreleased/solr-18175-field-exists-400.yml new file mode 100644 index 00000000000..ce338d537b0 --- /dev/null +++ b/changelog/unreleased/solr-18175-field-exists-400.yml @@ -0,0 +1,8 @@ +title: > + Return a client error for unsupported field existence queries instead of an internal server error. +type: fixed +authors: + - name: Xinyao Zhang +links: + - name: SOLR-18175 + url: https://issues.apache.org/jira/browse/SOLR-18175 diff --git a/solr/core/src/java/org/apache/solr/schema/FieldType.java b/solr/core/src/java/org/apache/solr/schema/FieldType.java index 971235e07b5..f9d276935a6 100644 --- a/solr/core/src/java/org/apache/solr/schema/FieldType.java +++ b/solr/core/src/java/org/apache/solr/schema/FieldType.java @@ -1070,16 +1070,32 @@ public abstract class FieldType extends FieldProperties { */ public Query getExistenceQuery(QParser parser, SchemaField field) { if (field.hasDocValues()) { - return new FieldExistsQuery(field.getName()); + return validateFieldExistsQuery(parser, new FieldExistsQuery(field.getName())); } else if (!field.omitNorms() && !isPointField()) { // TODO: Remove !isPointField() for SOLR-14199 - return new FieldExistsQuery(field.getName()); + return validateFieldExistsQuery(parser, new FieldExistsQuery(field.getName())); } else { // Default to an unbounded range query return getSpecializedExistenceQuery(parser, field); } } + private static Query validateFieldExistsQuery(QParser parser, FieldExistsQuery query) { + if (parser == null || parser.getReq() == null || parser.getReq().getCore() == null) { + return query; + } + + try { + parser.getReq().getCore().withSearcher(searcher -> searcher.rewrite(query)); + } catch (IllegalStateException e) { + // FieldExistsQuery.rewrite throws this if the field lacks doc values, norms, and vectors. + throw new SolrException(ErrorCode.BAD_REQUEST, e); + } catch (IOException e) { + throw new SolrException(ErrorCode.SERVER_ERROR, e); + } + return query; + } + /** * Returns a Query instance for doing existence searches for a field without certain options, such * as docValues or norms. diff --git a/solr/core/src/test-files/solr/collection1/conf/schema-customfield.xml b/solr/core/src/test-files/solr/collection1/conf/schema-customfield.xml index 2b1f719c6dc..2d3b1e9e4e6 100644 --- a/solr/core/src/test-files/solr/collection1/conf/schema-customfield.xml +++ b/solr/core/src/test-files/solr/collection1/conf/schema-customfield.xml @@ -40,11 +40,13 @@ </fieldType> <fieldType name="customintfield" class="${solr.tests.CustomIntFieldType}" docValues="${solr.tests.numeric.dv}" /> + <fieldType name="missingdocvaluesfield" class="org.apache.solr.schema.MissingDocValuesFieldType" docValues="true"/> <field name="id" type="string" indexed="true" stored="true" multiValued="false" required="true"/> <field name="intfield" type="int" indexed="true" stored="true"/> <field name="swap_foo_bar_in_prefix_query" type="customfield" indexed="true" stored="true" multiValued="true"/> <field name="int_prefix_as_range" type="customintfield" indexed="true" stored="true"/> + <field name="bad_exists" type="missingdocvaluesfield" indexed="true" stored="false"/> <field name="_version_" type="long" indexed="true" stored="true" multiValued="false"/> diff --git a/solr/core/src/test/org/apache/solr/schema/MissingDocValuesFieldType.java b/solr/core/src/test/org/apache/solr/schema/MissingDocValuesFieldType.java new file mode 100644 index 00000000000..c09884868dc --- /dev/null +++ b/solr/core/src/test/org/apache/solr/schema/MissingDocValuesFieldType.java @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.solr.schema; + +import java.util.List; +import org.apache.lucene.document.Field; +import org.apache.lucene.document.StringField; +import org.apache.lucene.index.IndexableField; + +/** Test-only field type that omits the doc values it advertises in the schema. */ +public class MissingDocValuesFieldType extends StrField { + + @Override + public List<IndexableField> createFields(SchemaField field, Object value) { + return List.of(new StringField(field.getName(), value.toString(), Field.Store.NO)); + } +} diff --git a/solr/core/src/test/org/apache/solr/search/FieldExistsQueryBadRequestTest.java b/solr/core/src/test/org/apache/solr/search/FieldExistsQueryBadRequestTest.java new file mode 100644 index 00000000000..8878dfd80e1 --- /dev/null +++ b/solr/core/src/test/org/apache/solr/search/FieldExistsQueryBadRequestTest.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.solr.search; + +import org.apache.solr.SolrTestCaseJ4; +import org.apache.solr.common.SolrException; +import org.apache.solr.request.SolrQueryRequest; +import org.junit.BeforeClass; +import org.junit.Test; + +public class FieldExistsQueryBadRequestTest extends SolrTestCaseJ4 { + + @BeforeClass + public static void beforeClass() throws Exception { + System.setProperty( + "solr.tests.CustomIntFieldType", + Boolean.getBoolean(NUMERIC_POINTS_SYSPROP) + ? "org.apache.solr.schema.IntPointPrefixActsAsRangeQueryFieldType" + : "org.apache.solr.schema.TrieIntPrefixActsAsRangeQueryFieldType"); + initCore("solrconfig-basic.xml", "schema-customfield.xml"); + + assertU(adoc("id", "1", "bad_exists", "alpha")); + assertU(commit()); + } + + @Test + public void testQueryReturnsBadRequest() { + assertBadRequest(req("q", "bad_exists:*")); + } + + @Test + public void testFacetQueryReturnsBadRequest() { + assertBadRequest(req("q", "*:*", "rows", "0", "facet", "true", "facet.query", "bad_exists:*")); + } + + private void assertBadRequest(SolrQueryRequest req) { + SolrException exception = expectThrows(SolrException.class, () -> h.query(req)); + assertEquals(SolrException.ErrorCode.BAD_REQUEST.code, exception.code()); + assertTrue(exception.getCause() instanceof IllegalStateException); + } +}
