Skip to content

Commit

Permalink
Added support of removeLastWordsIfNoResult & removeFirstWordsIfNoResult
Browse files Browse the repository at this point in the history
  • Loading branch information
speedblue committed Aug 13, 2014
1 parent 83741fa commit a5bad47
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions src/main/java/com/algolia/search/saas/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ public enum QueryType
protected boolean replaceSynonyms;
protected boolean typoTolerance;
protected boolean allowTyposOnNumericTokens;

protected boolean removeLastWordsIfNoResult;
protected boolean removeFirstWordsIfNoResult;

public Query(String query) {
minWordSizeForApprox1 = 3;
minWordSizeForApprox2 = 7;
Expand All @@ -80,6 +82,7 @@ public Query(String query) {
maxNumberOfFacets = -1;
advancedSyntax = false;
analytics = synonyms = replaceSynonyms = typoTolerance = allowTyposOnNumericTokens = true;
removeLastWordsIfNoResult = removeFirstWordsIfNoResult = false;
}

public Query() {
Expand All @@ -93,8 +96,29 @@ public Query() {
maxNumberOfFacets = -1;
advancedSyntax = false;
analytics = synonyms = replaceSynonyms = typoTolerance = allowTyposOnNumericTokens = true;
removeLastWordsIfNoResult = removeFirstWordsIfNoResult = false;
}


/**
* If set to true, when a query does not return any result, the final word will be removed until there is results.
* This option is particulary useful on e-commerce websites. (disabled by default)
*/
public Query removeLastWordsIfNoResult(boolean enable)
{
this.removeLastWordsIfNoResult = enable;
return this;
}

/**
* If set to true, when a query does not return any result, the first word will be removed until there is results.
* This option is useful on adress search. (disabled by default).
*/
public Query removeFirstWordsIfNoResult(boolean enable)
{
this.removeFirstWordsIfNoResult = enable;
return this;
}

/**
* List of object attributes you want to use for textual search (must be a subset of the attributesToIndex
* index setting). Attributes are separated with a comma (for example @"name,address").
Expand Down Expand Up @@ -482,6 +506,16 @@ protected String getQueryString() {
stringBuilder.append("minWordSizefor2Typos=");
stringBuilder.append(minWordSizeForApprox2);
}
if (removeFirstWordsIfNoResult) {
if (stringBuilder.length() > 0)
stringBuilder.append('&');
stringBuilder.append("removeFirstWordsIfNoResult=true");
}
if (removeLastWordsIfNoResult) {
if (stringBuilder.length() > 0)
stringBuilder.append('&');
stringBuilder.append("removeLastWordsIfNoResult=true");
}
if (getRankingInfo) {
if (stringBuilder.length() > 0)
stringBuilder.append('&');
Expand Down

0 comments on commit a5bad47

Please sign in to comment.