diff --git a/src/main/java/org/apache/solr/mcp/server/schema/SchemaService.java b/src/main/java/org/apache/solr/mcp/server/schema/SchemaService.java index 26071ec7..4f05b842 100644 --- a/src/main/java/org/apache/solr/mcp/server/schema/SchemaService.java +++ b/src/main/java/org/apache/solr/mcp/server/schema/SchemaService.java @@ -591,7 +591,9 @@ public String viewSchemaPrompt( - List dynamic-field patterns (e.g. `*_s`, `*_txt`) and their types — these accept fields whose names are not predeclared. - The schema shows only the patterns. To see which dynamic fields are actually in - use (e.g. `genre_s`) and how many documents have each, call `list-fields`. + use (e.g. `genre_s`) and how many documents have each, call `list-fields`. Its + `docs` count is missing or 0 for numeric, date and docValues-only fields because + Solr cannot count them, not because they are empty. - List copyField rules (source → destination) — these duplicate content into aggregator fields, often used for catch-all search fields. diff --git a/src/main/java/org/apache/solr/mcp/server/search/SearchService.java b/src/main/java/org/apache/solr/mcp/server/search/SearchService.java index 6051dbf8..7a4bf35f 100644 --- a/src/main/java/org/apache/solr/mcp/server/search/SearchService.java +++ b/src/main/java/org/apache/solr/mcp/server/search/SearchService.java @@ -363,8 +363,9 @@ private static RuntimeException withRemediationHint(SolrException e, String coll return new SolrException( SolrException.ErrorCode.BAD_REQUEST, "Search failed: Solr rejected the request. Check query syntax, filter queries, facet fields," - + " sort clauses, and pagination; call list-fields to verify the field names, and facet and" - + " sort only on single-valued fields with D (docValues) in their schema flags."); + + " sort clauses, and pagination; call list-fields to verify the field names. Facet only on" + + " fields whose flags include DOC_VALUES, and sort only on those that are also not" + + " MULTI_VALUED."); } if (e.code() == SolrException.ErrorCode.UNAUTHORIZED.code || e.code() == SolrException.ErrorCode.FORBIDDEN.code) { @@ -413,10 +414,11 @@ public String searchCollectionPrompt( - Call `list-fields` on `%s`. It lists the fields that hold data, dynamic-field instances included. From each field's `type` identify which are searchable text (`text_general` or similar tokenized types), which are filterable exact-match - (`string`/`strings`), and which are numeric/date ranges. A `D` in a field's - `schema` flags (decoded by `info.key`) means docValues: those are the fields you - can facet on, and sort on when they are single-valued (no `M`). `dynamicBase` - shows which pattern a field came from (e.g. `*_s`). + (`string`/`strings`), and which are numeric/date ranges. Fields whose `flags` + include `DOC_VALUES` are the ones you can facet on; you can sort on them when + `flags` does not also include `MULTI_VALUED`. `dynamicBase` shows which pattern a + field came from (e.g. `*_s`). A missing or 0 `docs` on a numeric, date or + docValues-only field means Solr could not count it, not that it is empty. 2. Translate the question into Solr query parts. - Pick the most informative tokens from the question. Map them to fields: diff --git a/src/test/java/org/apache/solr/mcp/server/schema/SchemaServiceTest.java b/src/test/java/org/apache/solr/mcp/server/schema/SchemaServiceTest.java index 28c738a8..e7214c92 100644 --- a/src/test/java/org/apache/solr/mcp/server/schema/SchemaServiceTest.java +++ b/src/test/java/org/apache/solr/mcp/server/schema/SchemaServiceTest.java @@ -462,6 +462,13 @@ void viewSchemaPrompt_isReadOnlyAndReferencesGetSchema() { "View prompt should cross-reference design-schema for follow-up modification"); } + @Test + void viewSchemaPrompt_saysUncountedFieldsAreNotEmpty() { + String body = schemaService.viewSchemaPrompt("products"); + + assertTrue(body.contains("cannot count them, not because they are empty"), body); + } + // ===== list-fields ===== private static final String COLLECTION = "shows"; diff --git a/src/test/java/org/apache/solr/mcp/server/search/SearchServiceTest.java b/src/test/java/org/apache/solr/mcp/server/search/SearchServiceTest.java index 5fb1e9d8..3c95ccb5 100644 --- a/src/test/java/org/apache/solr/mcp/server/search/SearchServiceTest.java +++ b/src/test/java/org/apache/solr/mcp/server/search/SearchServiceTest.java @@ -39,6 +39,7 @@ import org.apache.solr.common.SolrDocument; import org.apache.solr.common.SolrDocumentList; import org.apache.solr.common.SolrException; +import org.apache.solr.common.luke.FieldFlag; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.DisabledInNativeImage; @@ -682,4 +683,26 @@ void searchCollectionPrompt_includesKeyWorkflowSteps() { assertTrue(body.contains("filterQueries"), "Prompt should explain filterQueries"); assertTrue(body.contains("numFound"), "Prompt should mention numFound interpretation"); } + + @Test + void searchGuidance_namesTheFlagsListFieldsReturns() throws Exception { + // list-fields writes FieldFlag names; Luke's raw schema letters and info.key never reach it. + String prompt = + new SearchService(mock(SolrClient.class)).searchCollectionPrompt("shows", "question"); + String hint = + assertThrows( + SolrException.class, + () -> + serviceThrowing( + new SolrException(SolrException.ErrorCode.BAD_REQUEST, "bad request")) + .search("test_collection", "*:*", null, null, null, null, null)) + .getMessage(); + + for (String text : List.of(prompt, hint)) { + assertTrue(text.contains(FieldFlag.DOC_VALUES.name()), text); + assertTrue(text.contains(FieldFlag.MULTI_VALUED.name()), text); + assertFalse(text.contains("info.key"), text); + assertFalse(text.contains("schema flags"), text); + } + } }