Remove old query terms from search results suggestions links.

Especially when old terms were misspelled, suggestions links then
provided most of the time empty results.
pull/167/head
luccioman 7 years ago
parent c71b545235
commit a9dc0874c0

@ -804,13 +804,9 @@ public class yacysearch {
try { try {
suggestion = meanIt.next().toString(); suggestion = meanIt.next().toString();
prop.put("didYouMean_suggestions_" + meanCount + "_word", suggestion); prop.put("didYouMean_suggestions_" + meanCount + "_word", suggestion);
prop.put( prop.put("didYouMean_suggestions_" + meanCount + "_url",
"didYouMean_suggestions_" + meanCount + "_url", QueryParams.navUrlWithNewQueryString(RequestHeader.FileType.HTML, 0, theQuery,
QueryParams.navurl( suggestion, authenticatedUserName != null));
RequestHeader.FileType.HTML,
0,
theQuery,
suggestion, true, authenticatedUserName != null).toString());
prop.put("didYouMean_suggestions_" + meanCount + "_sep", "|"); prop.put("didYouMean_suggestions_" + meanCount + "_sep", "|");
meanCount++; meanCount++;
} catch (final ConcurrentModificationException e) { } catch (final ConcurrentModificationException e) {

@ -881,17 +881,24 @@ public final class QueryParams {
} }
/** /**
* make a query anchor tag * Build a search query URL from the given parameters.
* *
* @param ext extension of the servlet to request (e.g. "html", "rss", "json"...)
* @param page index of the wanted page (first page is zero)
* @param theQuery holds the main query parameters. Must not be null.
* @param newModifier a eventual new modifier to append to the eventual ones already defined in theQuery QueryParams. Can be null.
* @param newModifierReplacesOld when newModifier is not null, it is appended in addition
* to existing modifier(s) - if it is empty it overwrites (clears) existing
* modifier(s)
* @param authenticatedFeatures * @param authenticatedFeatures
* when true, access to authentication protected search features is * when true, access to authentication protected search features is
* wanted * wanted
* @return the anchor url builder * @return a StringBuilder instance with the URL to the new search result page
*/ */
public static StringBuilder navurl(final RequestHeader.FileType ext, final int page, final QueryParams theQuery, public static StringBuilder navurl(final RequestHeader.FileType ext, final int page, final QueryParams theQuery,
final String newQueryString, boolean newModifierReplacesOld, final boolean authenticatedFeatures) { final String newModifier, boolean newModifierReplacesOld, final boolean authenticatedFeatures) {
final StringBuilder sb = navurlBase(ext, theQuery, newQueryString, newModifierReplacesOld, final StringBuilder sb = navurlBase(ext, theQuery, newModifier, newModifierReplacesOld,
authenticatedFeatures); authenticatedFeatures);
sb.append("&startRecord="); sb.append("&startRecord=");
@ -899,6 +906,31 @@ public final class QueryParams {
return sb; return sb;
} }
/**
* Build a search query URL with a new search query string, but keeping any already defined eventual modifiers.
*
* @param ext extension of the servlet to request (e.g. "html", "rss", "json"...)
* @param page index of the wanted page (first page is zero)
* @param theQuery holds the main query parameters. Must not be null.
* @param authenticatedFeatures
* when true, access to authentication protected search features is
* wanted
* @return the URL to the new search result page
*/
public static String navUrlWithNewQueryString(final RequestHeader.FileType ext, final int page, final QueryParams theQuery,
final String newQueryString, final boolean authenticatedFeatures) {
final StringBuilder sb = new StringBuilder(120);
sb.append("yacysearch.");
sb.append(ext.name().toLowerCase(Locale.ROOT));
sb.append("?query=");
sb.append(new QueryGoal(newQueryString).getQueryString(true));
appendNavUrlQueryParams(sb, theQuery, null, false, authenticatedFeatures);
return sb.toString();
}
/** /**
* construct navigator url * construct navigator url
@ -907,11 +939,11 @@ public final class QueryParams {
* extension of servlet (e.g. html, rss) * extension of servlet (e.g. html, rss)
* @param theQuery * @param theQuery
* search query * search query
* @param newModifier * @param newModifier optional new modifier. - if null existing modifier(s) of theQuery are
* optional new modifier. - if null existing modifier of theQuery is
* appended - if not null this new modifier is appended in addition * appended - if not null this new modifier is appended in addition
* to existing modifier - if isEmpty overwrites (clears) existing * to eventually existing modifier(s) - if isEmpty overwrites (clears) any eventual existing
* modifier * modifier(s)
* @param newModifierReplacesOld considered only when newModifier is not null and not empty. When true, any existing modifiers with the same name are replaced with the new one.
* @param authenticatedFeatures * @param authenticatedFeatures
* when true, access to authentication protected search features is * when true, access to authentication protected search features is
* wanted * wanted
@ -920,24 +952,54 @@ public final class QueryParams {
public static StringBuilder navurlBase(final RequestHeader.FileType ext, final QueryParams theQuery, public static StringBuilder navurlBase(final RequestHeader.FileType ext, final QueryParams theQuery,
final String newModifier, final boolean newModifierReplacesOld, final boolean authenticatedFeatures) { final String newModifier, final boolean newModifierReplacesOld, final boolean authenticatedFeatures) {
StringBuilder sb = new StringBuilder(120); final StringBuilder sb = new StringBuilder(120);
sb.append("yacysearch."); sb.append("yacysearch.");
sb.append(ext.name().toLowerCase(Locale.ROOT)); sb.append(ext.name().toLowerCase(Locale.ROOT));
sb.append("?query="); sb.append("?query=");
sb.append(theQuery.getQueryGoal().getQueryString(true)); sb.append(theQuery.getQueryGoal().getQueryString(true));
if (newModifier == null) { appendNavUrlQueryParams(sb, theQuery, newModifier, newModifierReplacesOld, authenticatedFeatures);
return sb;
}
/**
* Append search query parameters to the URL builder already filled with the beginning of the URL.
*
* @param sb the URL string builder to fill. Must not be null.
* @param theQuery holds the main query parameters. Must not be null.
* @param newModifier optional new modifier. - if null existing modifier(s) of theQuery are
* appended - if not null this new modifier is appended in addition
* to eventually existing modifier(s) - if isEmpty overwrites (clears) any eventual existing
* modifier(s)
* @param newModifierReplacesOld considered only when newModifier is not null and not empty. When true, any existing modifiers with the same name are replaced with the new one.
* @param authenticatedFeatures
* when true, access to authentication protected search features is
* wanted
*/
protected static void appendNavUrlQueryParams(final StringBuilder sb, final QueryParams theQuery, final String newModifier,
final boolean newModifierReplacesOld, final boolean authenticatedFeatures) {
if (newModifier == null) {
if (!theQuery.modifier.isEmpty()) sb.append("+" + theQuery.modifier.toString()); if (!theQuery.modifier.isEmpty()) sb.append("+" + theQuery.modifier.toString());
} else { } else {
if (!newModifier.isEmpty()) { if (!newModifier.isEmpty()) {
if (!theQuery.modifier.isEmpty()) sb.append("+" + theQuery.modifier.toString()); if (!theQuery.modifier.isEmpty()) {
sb.append("+" + theQuery.modifier.toString());
}
if (newModifierReplacesOld) { if (newModifierReplacesOld) {
int nmpi = newModifier.indexOf(":"); int nmpi = newModifier.indexOf(":");
if (nmpi > 0) { if (nmpi > 0) {
String nmp = newModifier.substring(0, nmpi) + ":"; String nmp = newModifier.substring(0, nmpi) + ":";
int i = sb.indexOf(nmp); int i = sb.indexOf(nmp);
if (i > 0) sb = new StringBuilder(sb.substring(0, i).trim()); if (i > 0) {
if (sb.charAt(sb.length() - 1) == '+') sb.setLength(sb.length() - 1); sb.setLength(i);
if (sb.charAt(sb.length() - 1) == ' ') {
sb.setLength(sb.length() - 1);
}
}
if (sb.charAt(sb.length() - 1) == '+') {
sb.setLength(sb.length() - 1);
}
} }
} }
try { try {
@ -977,8 +1039,6 @@ public final class QueryParams {
if(authenticatedFeatures) { if(authenticatedFeatures) {
sb.append("&auth"); sb.append("&auth");
} }
}
return sb;
}
} }

Loading…
Cancel
Save