From 857eec4d8bce02481952ac0a76cbcbaf6e1675d5 Mon Sep 17 00:00:00 2001 From: gibert Date: Thu, 23 May 2024 15:04:09 +0200 Subject: [PATCH] =?UTF-8?q?Bug=20avec=20QueryString=20car=20les=20termes?= =?UTF-8?q?=20de=20recherche=20s=C3=A9par=C3=A9s=20par=20un=20espace=20doi?= =?UTF-8?q?vent=20se=20trouver=20dans=20un=20seul=20champ=20(ex=20:=20mot?= =?UTF-8?q?=20du=20titre=20+=20nom=20de=20l'auteur=20ne=20renvoie=20rien)?= =?UTF-8?q?=20=3D>=20traitement=20de=20la=20chaine=20de=20recherche.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../theses/builder/SearchQueryBuilder.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/main/java/fr/abes/thesesapirecherche/theses/builder/SearchQueryBuilder.java b/src/main/java/fr/abes/thesesapirecherche/theses/builder/SearchQueryBuilder.java index f8c4679..23fb60d 100644 --- a/src/main/java/fr/abes/thesesapirecherche/theses/builder/SearchQueryBuilder.java +++ b/src/main/java/fr/abes/thesesapirecherche/theses/builder/SearchQueryBuilder.java @@ -31,6 +31,8 @@ import java.util.Iterator; import java.util.List; import java.util.Optional; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import static fr.abes.thesesapirecherche.commons.builder.FacetQueryBuilder.addFilters; import static fr.abes.thesesapirecherche.commons.builder.FacetQueryBuilder.buildFilter; @@ -53,6 +55,9 @@ public class SearchQueryBuilder { private FacetProps facetProps; private Query buildQuery(String chaine) { + chaine = replaceAndOutsideQuotes(chaine); + chaine = replaceSpacesOutsideQuotes(chaine); + QueryStringQuery.Builder builderQuery = new QueryStringQuery.Builder(); builderQuery.query(chaine); builderQuery.defaultOperator(Operator.And); @@ -98,6 +103,49 @@ private Query buildQuery(String chaine) { return builderQuery.build()._toQuery(); } + private String replaceSpacesOutsideQuotes(String input) { + StringBuilder result = new StringBuilder(); + boolean insideQuotes = false; + + for (int i = 0; i < input.length(); i++) { + char c = input.charAt(i); + + if (c == '"') { + insideQuotes = !insideQuotes; + result.append(c); + } else if (c == ' ' && !insideQuotes) { + result.append(" AND "); + } else { + result.append(c); + } + } + + return result.toString(); + } + public static String replaceAndOutsideQuotes(String input) { + StringBuilder result = new StringBuilder(); + boolean insideQuotes = false; + int i = 0; + + while (i < input.length()) { + char currentChar = input.charAt(i); + + if (currentChar == '\"') { + insideQuotes = !insideQuotes; + result.append(currentChar); + i++; + } else if (!insideQuotes && i + 2 < input.length() && input.startsWith(" AND ", i)) { + result.append(" "); + i += 5; // Skip past the "AND" + } else { + result.append(currentChar); + i++; + } + } + + return result.toString(); + } + public ResponseTheseLiteDto simple(String chaine, Integer debut, Integer nombre, String tri, String filtres) throws Exception { SearchResponse response = ElasticClient.getElasticsearchClient().search( s -> s