diff --git a/rest/src/main/groovy/whelk/rest/api/SiteSearch.java b/rest/src/main/groovy/whelk/rest/api/SiteSearch.java index 8c97eab911..1181234a49 100644 --- a/rest/src/main/groovy/whelk/rest/api/SiteSearch.java +++ b/rest/src/main/groovy/whelk/rest/api/SiteSearch.java @@ -135,7 +135,17 @@ Map findData(Map queryParameters, String baseUri, String path) Map findDesc = getAndIndexDescription(appId + "find"); queryParameters.put("_appConfig", new String[]{mapper.writeValueAsString(search2.buildAppConfig(findDesc))}); } - return search2.doSearch(queryParameters); + try { + return search2.doSearch(queryParameters); + } catch (InvalidQueryException re) { + // The query is broken. Often this means a bit of text (like a title) was copy-pasted as a query + // without regard for query language syntax. Let's just run the whole thing as a string search. + String[] qArr = queryParameters.get("_q"); + // Escape any un-escaped quotes. Totally intuitive, right? And yet not watertight, will actually fail on escaped backslash before quote in query. + qArr[0] = qArr[0].replaceAll("[^\\\\]\"", "\\\\\""); + qArr[0] = "\"" + qArr[0] + "\""; // quote as a whole + return search2.doSearch(queryParameters); // It throws again? So be it - let the crud code return an error. + } } else { if (queryParameters.get("_statsrepr") == null && searchSettings.get("statsfind") != null) { queryParameters.put("_statsrepr", new String[]{mapper.writeValueAsString(searchSettings.get("statsfind"))});