forked from apache/atlas
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create the suggestions API for suggestions
- Loading branch information
Showing
2 changed files
with
109 additions
and
2 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
repository/src/main/java/org/apache/atlas/discovery/ESBasedSuggestionService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package org.apache.atlas.discovery; | ||
|
||
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | ||
import org.apache.atlas.type.AtlasType; | ||
import org.apache.http.util.EntityUtils; | ||
import org.codehaus.jackson.annotate.JsonAutoDetect; | ||
import org.codehaus.jackson.annotate.JsonIgnoreProperties; | ||
import org.elasticsearch.action.search.SearchRequest; | ||
import org.elasticsearch.client.Request; | ||
import org.elasticsearch.client.Response; | ||
import org.elasticsearch.client.RestClient; | ||
import org.elasticsearch.common.unit.Fuzziness; | ||
import org.elasticsearch.search.builder.SearchSourceBuilder; | ||
import org.elasticsearch.search.suggest.SuggestBuilder; | ||
import org.elasticsearch.search.suggest.SuggestBuilders; | ||
import org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class ESBasedSuggestionService { | ||
private static final Logger LOG = LoggerFactory.getLogger(ESBasedSuggestionService.class); | ||
|
||
private RestClient esRestClient; | ||
|
||
public static final String INDEX_NAME = "suggest"; | ||
|
||
public ESBasedSuggestionService(RestClient lowLevelClient) { | ||
this.esRestClient = lowLevelClient; | ||
} | ||
public static SearchRequest buildSuggestQuery(String userQuery, String field, String suggestion_name, int fuzziness) { | ||
SearchRequest searchRequest = new SearchRequest(INDEX_NAME); | ||
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); | ||
|
||
Fuzziness fuzzinessObj = Fuzziness.build(fuzziness); | ||
CompletionSuggestionBuilder completionSuggestionBuilder = SuggestBuilders.completionSuggestion(field) | ||
.prefix(userQuery, fuzzinessObj); | ||
|
||
SuggestBuilder suggestBuilder = new SuggestBuilder() | ||
.addSuggestion(suggestion_name, completionSuggestionBuilder); | ||
|
||
searchSourceBuilder.suggest(suggestBuilder); | ||
searchRequest.source(searchSourceBuilder); | ||
|
||
return searchRequest; | ||
} | ||
|
||
public SuggestionResponse searchSuggestions(String query, String fieldName, int fuzziness) throws IOException { | ||
// Build the suggestor query for ES | ||
String suggestorName = "suggest_keyword"; | ||
SearchRequest searchRequest = buildSuggestQuery(query, fieldName, suggestorName, fuzziness); | ||
String queryStr = searchRequest.source().toString(); | ||
Request queryRequest = new Request("POST", "/suggest/_search"); | ||
queryRequest.setJsonEntity(queryStr); | ||
Response response = esRestClient.performRequest(queryRequest); | ||
|
||
SuggestionResponse suggestionResponse = new SuggestionResponse(); | ||
String esResponseString = EntityUtils.toString(response.getEntity()); | ||
|
||
// Parse the response and return the suggestions | ||
Map<String, Object> responseMap = AtlasType.fromJson(esResponseString, Map.class); | ||
Map<String, Object> suggestMap = (Map<String, Object>) responseMap.get("suggest"); | ||
ArrayList<LinkedHashMap> suggestionMap = (ArrayList<LinkedHashMap>) suggestMap.get(suggestorName); | ||
LinkedHashMap suggestions = suggestionMap.get(0); | ||
List<LinkedHashMap> options = (List<LinkedHashMap>) suggestions.get("options"); | ||
for (LinkedHashMap option : options) { | ||
suggestionResponse.addSuggestion((String) option.get("text")); | ||
} | ||
|
||
return suggestionResponse; | ||
} | ||
|
||
@JsonAutoDetect(getterVisibility=JsonAutoDetect.Visibility.PUBLIC_ONLY, setterVisibility=JsonAutoDetect.Visibility.PUBLIC_ONLY, fieldVisibility=JsonAutoDetect.Visibility.PUBLIC_ONLY) | ||
@JsonSerialize(include=JsonSerialize.Inclusion.ALWAYS) | ||
@JsonIgnoreProperties(ignoreUnknown=true) | ||
public class SuggestionResponse { | ||
|
||
public SuggestionResponse() { } | ||
private List<String> suggestions = new ArrayList<>(); | ||
|
||
public List<String> getSuggestions() { | ||
return suggestions; | ||
} | ||
|
||
public void addSuggestion(String suggestion) { | ||
this.suggestions.add(suggestion); | ||
} | ||
|
||
public void setSuggestions(List<String> suggestions) { | ||
this.suggestions = suggestions; | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters