Skip to content

Commit

Permalink
Bug avec QueryString car les termes de recherche séparés par un espac…
Browse files Browse the repository at this point in the history
…e doivent se trouver dans un seul champ (ex : mot du titre + nom de l'auteur ne renvoie rien) => traitement de la chaine de recherche.
  • Loading branch information
julg committed May 23, 2024
1 parent b95218a commit 857eec4
Showing 1 changed file with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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<These> response = ElasticClient.getElasticsearchClient().search(
s -> s
Expand Down

0 comments on commit 857eec4

Please # to comment.