Skip to content
This repository has been archived by the owner on Jun 1, 2021. It is now read-only.

Commit

Permalink
Merge pull request #209 from luaks/feature/204-term-suggestions-canno…
Browse files Browse the repository at this point in the history
…t-be-limited-by-a-full-text-search

#204: limit suggestions based on an existing fulltext search
  • Loading branch information
tkurz authored Mar 30, 2021
2 parents da8da20 + d6b4713 commit 8147a5e
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
*/
public class FulltextSearch {

private String searchString = null;
private Filter filter = null;
private List<Sort> sorting = new ArrayList<>();
private int facetMinCount = SearchConfiguration.get(SearchConfiguration.SEARCH_RESULT_FACET_INCLUDE_EMPTY, false)? 0 : 1;
Expand All @@ -43,17 +42,16 @@ public class FulltextSearch {
private DocumentFactory childrenFactory = null;
private String timeZone = null;
private Distance geoDistance = null;
private String minimumShouldMatch = "1";
private String searchContext = null;
private boolean strict = true;
private boolean spellcheck = false;
private boolean smartParsing = false;
private FulltextTerm fulltextSearchTerm = new FulltextTerm("*", "1");

/**
* Creates a new basic full text search query object.
*/
FulltextSearch() {
this.searchString = "*";
this.resultSet = new Page(1, SearchConfiguration.get(SearchConfiguration.SEARCH_RESULT_PAGESIZE,10));
}

Expand All @@ -64,7 +62,7 @@ public class FulltextSearch {
public FulltextSearch copy() {
final FulltextSearch copy = new FulltextSearch();

copy.searchString = new String(this.searchString);
copy.fulltextSearchTerm = fulltextSearchTerm.copy();
copy.resultSet = resultSet.copy();
if (Objects.nonNull(this.getFilter())) {
copy.filter = this.getFilter().clone();
Expand All @@ -84,7 +82,7 @@ public FulltextSearch copy() {
* @return This {@link FulltextSearch} instance with the new text query.
*/
public FulltextSearch text(String fullText) {
this.searchString = fullText;
this.fulltextSearchTerm = new FulltextTerm(fullText, this.fulltextSearchTerm.getMinimumMatch());
return this;
}

Expand Down Expand Up @@ -500,15 +498,15 @@ public FulltextSearch geoDistance(MultiValueFieldDescriptor.LocationFieldDescrip
* @return String containing the query target.
*/
public String getSearchString() {
return searchString;
return fulltextSearchTerm.getFulltextSearchTerm();
}

/**
* Gets the text of the search query.
* @return String containing the query target.
*/
public String getEscapedSearchString() {
return StringEscapeUtils.escapeJava(searchString);
return StringEscapeUtils.escapeJava(fulltextSearchTerm.getFulltextSearchTerm());
}

/**
Expand Down Expand Up @@ -638,11 +636,11 @@ public FulltextSearch setStrict(boolean strict) {
}

public String getMinimumShouldMatch() {
return minimumShouldMatch;
return fulltextSearchTerm.getMinimumMatch();
}

public FulltextSearch setMinimumShouldMatch(String minimumShouldMatch) {
this.minimumShouldMatch = minimumShouldMatch;
this.fulltextSearchTerm = new FulltextTerm(fulltextSearchTerm.getFulltextSearchTerm(), minimumShouldMatch);
return this;
}

Expand Down Expand Up @@ -706,7 +704,7 @@ public String toString(){
"}";

return String.format(searchString,
this.searchString,
this.fulltextSearchTerm.getFulltextSearchTerm(),
this.filter,
this.timeZone,
CollectionUtils.isNotEmpty(this.sorting) ? "[" + this.sorting.stream().map(f -> f.toString()).collect(Collectors.joining(", ")) +"]": "[]",
Expand Down Expand Up @@ -750,6 +748,14 @@ public FulltextSearch cursor(String searchAfter, long aliveMinutes) {
return this;
}

public FulltextTerm getFulltextSearchTerm() {
return this.fulltextSearchTerm;
}

public void setFulltextSearchTerm(FulltextTerm fulltextSearchTerm) {
this.fulltextSearchTerm = fulltextSearchTerm;
}

public enum Operators {
AND, OR
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.rbmhtechnology.vind.api.query;

public class FulltextTerm {
private final String fulltextSearchTerm;
private final String minimumMatch;

public FulltextTerm(String fulltextSearchTerm, String minimumMatch) {
this.fulltextSearchTerm = fulltextSearchTerm;
this.minimumMatch = minimumMatch;
}

public String getFulltextSearchTerm() {
return fulltextSearchTerm;
}

public String getMinimumMatch() {
return minimumMatch;
}

public FulltextTerm copy() {
return new FulltextTerm(getFulltextSearchTerm(), getMinimumMatch());
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.rbmhtechnology.vind.api.query.suggestion;

import com.rbmhtechnology.vind.api.query.FulltextSearch;
import com.rbmhtechnology.vind.api.query.FulltextTerm;
import com.rbmhtechnology.vind.api.query.filter.Filter;
import com.rbmhtechnology.vind.model.FieldDescriptor;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

import static java.util.Optional.empty;

/**
* Class to configure suggestions based on field descriptors.
*
Expand All @@ -21,21 +26,23 @@ public class DescriptorSuggestionSearch implements ExecutableSuggestionSearch {
private Filter filter = null;
private Set<FieldDescriptor> suggestionFields = new HashSet<>();
private String searchContext = null;
private Optional<FulltextTerm> fulltextTerm = empty();

/**
* Creates a new instance of {@link DescriptorSuggestionSearch}.
* @param input String text to find suggestion for.
* @param filter {@link Filter} to apply to the suggestion search.
* @param field {@link FieldDescriptor} fields where to find the suggestions.
*/
protected DescriptorSuggestionSearch(String input, int limit, Filter filter, FieldDescriptor ... field) {
Objects.requireNonNull(field);
this.input = input;
this.limit = limit;
this.filter = filter;
suggestionFields.addAll(Arrays.asList(field));
Objects.requireNonNull(field);
this.input = input;
this.limit = limit;
this.filter = filter;
suggestionFields.addAll(Arrays.asList(field));
}

/**
/**
* Set the text to find suggestions for.
* @param input String text to find suggestion for.
* @return {@link DescriptorSuggestionSearch} with the new text.
Expand Down Expand Up @@ -162,4 +169,13 @@ public boolean hasFilter() {
public boolean isStringSuggestion() {
return false;
}

public Optional<FulltextTerm> getFulltextTerm() {
return fulltextTerm;
}

public DescriptorSuggestionSearch fulltextTerm(final FulltextTerm fulltextTerm) {
this.fulltextTerm = Optional.ofNullable(fulltextTerm);
return this;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.rbmhtechnology.vind.api.query.suggestion;

import com.rbmhtechnology.vind.api.query.FulltextTerm;
import com.rbmhtechnology.vind.api.query.filter.Filter;

import java.util.Optional;

/**
* @author Thomas Kurz (tkurz@apache.org)
* @since 07.07.16.
Expand All @@ -23,6 +26,10 @@ public interface ExecutableSuggestionSearch {

public ExecutableSuggestionSearch context(String context);

public Optional<FulltextTerm> getFulltextTerm();

public ExecutableSuggestionSearch fulltextTerm(FulltextTerm fulltextTerm);

public int getLimit();

String getSearchContext();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.rbmhtechnology.vind.api.query.suggestion;

import com.rbmhtechnology.vind.api.query.FulltextTerm;
import com.rbmhtechnology.vind.api.query.filter.Filter;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

import static java.util.Optional.empty;

/**
* Class to configure suggestions based on field String names.
*
Expand All @@ -20,6 +24,7 @@ public class StringSuggestionSearch implements ExecutableSuggestionSearch {
private Filter filter = null;
private Set<String> suggestionFields = new HashSet<>();
private String searchContext = null;
private Optional<FulltextTerm> fulltextTerm = empty();

/**
* Creates a new instance of {@link StringSuggestionSearch}.
Expand Down Expand Up @@ -61,6 +66,17 @@ public StringSuggestionSearch context(String context) {
return this;
}

@Override
public Optional<FulltextTerm> getFulltextTerm() {
return fulltextTerm;
}

@Override
public StringSuggestionSearch fulltextTerm(final FulltextTerm fulltextTerm) {
this.fulltextTerm = Optional.ofNullable(fulltextTerm);
return this;
}

@Override
public String getSearchContext() {
return this.searchContext;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.rbmhtechnology.vind.api.query.suggestion;

import com.rbmhtechnology.vind.api.query.FulltextTerm;
import com.rbmhtechnology.vind.api.query.filter.Filter;
import com.rbmhtechnology.vind.model.DocumentFactoryBuilder;
import com.rbmhtechnology.vind.model.FieldDescriptor;

import java.util.HashSet;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import static java.util.Optional.empty;

/**
* Class to prepare suggestion queries.
*/
Expand All @@ -19,6 +24,7 @@ public class SuggestionSearch {
private Set<FieldDescriptor> suggestionFields = new HashSet<>();
private Set<String> suggestionStringFields = new HashSet<>();
private String searchContext = null;
private Optional<FulltextTerm> fulltextTerm = empty();

/**
* Creates a new instance of {@link SuggestionSearch}.
Expand All @@ -42,6 +48,7 @@ public SuggestionSearch copy() {
copy.suggestionFields = this.suggestionFields;
copy.suggestionStringFields = this.suggestionStringFields;
copy.searchContext = this.searchContext;
copy.fulltextTerm = this.fulltextTerm;
return copy;
}

Expand Down Expand Up @@ -123,7 +130,7 @@ public Set<String> getSuggestionStringFields() {
*/
//FIXME: this supports also: fields()
public StringSuggestionSearch fields(String... fields) {
return new StringSuggestionSearch(input,limit,filter,fields).context(this.searchContext);
return new StringSuggestionSearch(input,limit,filter,fields).context(this.searchContext).fulltextTerm(fulltextTerm.orElse(null));
}

/**
Expand All @@ -132,7 +139,7 @@ public StringSuggestionSearch fields(String... fields) {
* @return {@link DescriptorSuggestionSearch} with the added field.
*/
public DescriptorSuggestionSearch addField(FieldDescriptor field) {
return new DescriptorSuggestionSearch(input,limit,filter,field).context(this.searchContext);
return new DescriptorSuggestionSearch(input,limit,filter,field).context(this.searchContext).fulltextTerm(fulltextTerm.orElse(null));
}
/**
* Adds the fields to search for suggestions in.
Expand All @@ -141,7 +148,7 @@ public DescriptorSuggestionSearch addField(FieldDescriptor field) {
*/
//FIXME: this supports also: fields()
public DescriptorSuggestionSearch fields(FieldDescriptor... fields) {
return new DescriptorSuggestionSearch(input,limit,filter,fields).context(this.searchContext);
return new DescriptorSuggestionSearch(input,limit,filter,fields).context(this.searchContext).fulltextTerm(fulltextTerm.orElse(null));
}

/**
Expand Down Expand Up @@ -177,4 +184,18 @@ public String getSearchContext() {
return this.searchContext;
}

/**
* Get the fulltext term, to base the search on.
* Useful when suggesting within an already existing fulltext search
*
* @return a fulltext term
*/
public Optional<FulltextTerm> getFulltextTerm() {
return fulltextTerm;
}

public SuggestionSearch fulltextTerm(final FulltextTerm fulltextTerm) {
this.fulltextTerm = Optional.ofNullable(fulltextTerm);
return this;
}
}
Loading

0 comments on commit 8147a5e

Please sign in to comment.