support auto complete with facets

This commit is contained in:
Na Zhang 2018-10-02 13:24:41 -07:00
parent a8cd76f8bc
commit 7a60be5684
2 changed files with 22 additions and 11 deletions

View File

@ -34,20 +34,20 @@ public class SearchDao {
private static final ObjectMapper _OM = new ObjectMapper();
public List<String> getAutoCompleteList(String elasticSearchUrl, String input, int limit) {
return getAutoCompleteListDataset(elasticSearchUrl, input, limit / 3);
public List<String> getAutoCompleteList(String elasticSearchUrl, String input, String facet, int limit) {
return getAutoCompleteListDataset(elasticSearchUrl, input, facet, limit / 3);
}
public List<String> getAutoCompleteListDataset(String elasticSearchUrl, String input, int limit) {
String fieldName = "name_suggest";
return getAutoCompleteListbyES(elasticSearchUrl, fieldName, input, limit);
public List<String> getAutoCompleteListDataset(String elasticSearchUrl, String input, String facet, int limit) {
return getAutoCompleteListbyES(elasticSearchUrl, input, facet, limit);
}
public List<String> getAutoCompleteListbyES(String elasticSearchUrl, String fieldName, String input, int limit) {
public List<String> getAutoCompleteListbyES(String elasticSearchUrl, String input, String fieldName, int limit) {
// use elastic search completion suggester, ES will validate the input and limit
List<String> completionSuggestionList = new ArrayList<>();
Set<String> completionSuggestionSet = new HashSet<>();
fieldName = "name_suggest";
ObjectNode keywordNode = generateElasticSearchCompletionSuggesterQuery(fieldName, input, limit);
log.info("The completion suggester query sent to Elastic Search was: " + keywordNode);

View File

@ -40,12 +40,18 @@ public class Search extends Controller {
private static final String AUTOCOMPLETE_DATASET_KEY = "autocomplete.dataset";
private static final int DEFAULT_AUTOCOMPLETE_SIZE = 20;
private static final int DEFAULT_AUTOCOMPLETE_CACHE_TIME = 3600; // cache for an hour
private static final String DEFAULT_AUTOCOMPLETE_FIELD = "name";
private static final SearchDao SEARCH_DAO = Application.DAO_FACTORY.getSearchDao();
public static Result getSearchAutoComplete() {
// if not input, then get all search names (without limit).
String input = request().getQueryString("input");
String facet = request().getQueryString("facet");
if (isBlank(facet)) {
facet = DEFAULT_AUTOCOMPLETE_FIELD;
}
int size = 0; // size 0 means no limit
if (isNotBlank(input)) {
size = NumberUtils.toInt(request().getQueryString("size"), DEFAULT_AUTOCOMPLETE_SIZE);
@ -54,7 +60,7 @@ public class Search extends Controller {
String cacheKey = AUTOCOMPLETE_ALL_KEY + (isNotBlank(input) ? "." + input : "-all");
List<String> names = (List<String>) Cache.get(cacheKey);
if (names == null || names.size() == 0) {
names = SEARCH_DAO.getAutoCompleteList(ELASTICSEARCH_DATASET_URL, input, size);
names = SEARCH_DAO.getAutoCompleteList(ELASTICSEARCH_DATASET_URL, input, facet, size);
Cache.set(cacheKey, names, DEFAULT_AUTOCOMPLETE_CACHE_TIME);
}
@ -66,9 +72,14 @@ public class Search extends Controller {
}
public static Result getSearchAutoCompleteForDataset() {
// if not input, then get all search names (without limit).
String input = request().getQueryString("input");
int size = 0; // size 0 means no limit
String facet = request().getQueryString("facet");
if (isBlank(facet)) {
facet = DEFAULT_AUTOCOMPLETE_FIELD;
}
int size = 0; // 0 means no limit
if (isNotBlank(input)) {
size = NumberUtils.toInt(request().getQueryString("size"), DEFAULT_AUTOCOMPLETE_SIZE);
}
@ -76,7 +87,7 @@ public class Search extends Controller {
String cacheKey = AUTOCOMPLETE_DATASET_KEY + (isNotBlank(input) ? "." + input : "-all");
List<String> names = (List<String>) Cache.get(cacheKey);
if (names == null || names.size() == 0) {
names = SEARCH_DAO.getAutoCompleteListDataset(ELASTICSEARCH_DATASET_URL, input, size);
names = SEARCH_DAO.getAutoCompleteListDataset(ELASTICSEARCH_DATASET_URL, input, facet, size);
Cache.set(cacheKey, names, DEFAULT_AUTOCOMPLETE_CACHE_TIME);
}