Permalink
Browse files

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.
  • Loading branch information...
luccioman committed Feb 6, 2018
1 parent c71b545 commit a9dc0874c0042cd4518e61507f6187f1fc2c595e
Showing with 79 additions and 23 deletions.
  1. +3 −7 htroot/yacysearch.java
  2. +76 −16 source/net/yacy/search/query/QueryParams.java
@@ -804,13 +804,9 @@ public static serverObjects respond(
try {
suggestion = meanIt.next().toString();
prop.put("didYouMean_suggestions_" + meanCount + "_word", suggestion);
prop.put(
"didYouMean_suggestions_" + meanCount + "_url",
QueryParams.navurl(
RequestHeader.FileType.HTML,
0,
theQuery,
suggestion, true, authenticatedUserName != null).toString());
prop.put("didYouMean_suggestions_" + meanCount + "_url",
QueryParams.navUrlWithNewQueryString(RequestHeader.FileType.HTML, 0, theQuery,
suggestion, authenticatedUserName != null));
prop.put("didYouMean_suggestions_" + meanCount + "_sep", "|");
meanCount++;
} catch (final ConcurrentModificationException e) {
@@ -881,24 +881,56 @@ public String id(final boolean anonymized) {
}
/**
* 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
* when true, access to authentication protected search features is
* 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,
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);
sb.append("&startRecord=");
sb.append(page * theQuery.itemsPerPage());
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
@@ -907,11 +939,11 @@ public static StringBuilder navurl(final RequestHeader.FileType ext, final int p
* extension of servlet (e.g. html, rss)
* @param theQuery
* search query
* @param newModifier
* optional new modifier. - if null existing modifier of theQuery is
* @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 existing modifier - if isEmpty overwrites (clears) existing
* modifier
* 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
@@ -920,24 +952,54 @@ public static StringBuilder navurl(final RequestHeader.FileType ext, final int p
public static StringBuilder navurlBase(final RequestHeader.FileType ext, final QueryParams theQuery,
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(ext.name().toLowerCase(Locale.ROOT));
sb.append("?query=");
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());
} else {
if (!newModifier.isEmpty()) {
if (!theQuery.modifier.isEmpty()) sb.append("+" + theQuery.modifier.toString());
if (!theQuery.modifier.isEmpty()) {
sb.append("+" + theQuery.modifier.toString());
}
if (newModifierReplacesOld) {
int nmpi = newModifier.indexOf(":");
if (nmpi > 0) {
String nmp = newModifier.substring(0, nmpi) + ":";
int i = sb.indexOf(nmp);
if (i > 0) sb = new StringBuilder(sb.substring(0, i).trim());
if (sb.charAt(sb.length() - 1) == '+') sb.setLength(sb.length() - 1);
if (i > 0) {
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 {
@@ -977,8 +1039,6 @@ public static StringBuilder navurlBase(final RequestHeader.FileType ext, final Q
if(authenticatedFeatures) {
sb.append("&auth");
}
return sb;
}
}
}

0 comments on commit a9dc087

Please sign in to comment.