use of com.yelp.nrtsearch.server.grpc.SuggestLookupResponse in project nrtsearch by Yelp.
the class YelpSuggestTest method main.
// Run with args host && port && remote tmp dir in order
public static void main(String[] args) throws Exception {
// The dir where the indices will live in the remote server
Path yelp_suggest_test_base_path = Paths.get(System.getProperty("suggestTmp"));
// The client who will be talking to the remote server
LuceneServerClient standaloneServerClient = new LuceneServerClient(System.getProperty("suggestHost"), Integer.parseInt(System.getProperty("suggestPort")));
setUpIndex(standaloneServerClient, INDEX_NAME, SUGGESTIONS_FILE_PATH, new YelpSuggestTest.OneDocBuilderImpl());
// build Suggester
buildSuggester(standaloneServerClient);
// look up suggestions
SuggestLookupRequest.Builder suggestLookupBuilder = SuggestLookupRequest.newBuilder();
suggestLookupBuilder.setIndexName(INDEX_NAME);
suggestLookupBuilder.setText("a");
suggestLookupBuilder.setSuggestName("suggest_0");
suggestLookupBuilder.setHighlight(true);
// Set SF lat lon lookup
List<String> sanFranGeohashes = getGeoHashes(37.785371, -122.459446, 5, 7);
for (String geohash : sanFranGeohashes) {
suggestLookupBuilder.addContexts(geohash);
}
SuggestLookupResponse suggestResponse = standaloneServerClient.getBlockingStub().suggestLookup(suggestLookupBuilder.build());
List<OneSuggestLookupResponse> suggestResponseResultsList = suggestResponse.getResultsList();
System.out.println(suggestResponseResultsList);
System.exit(0);
}
use of com.yelp.nrtsearch.server.grpc.SuggestLookupResponse in project nrtsearch by Yelp.
the class SuggestLookupHandler method handle.
@Override
public SuggestLookupResponse handle(IndexState indexState, SuggestLookupRequest suggestLookupRequest) throws HandlerException {
String suggestName = suggestLookupRequest.getSuggestName();
final Lookup lookup = indexState.suggesters.get(suggestName);
if (lookup == null) {
throw new RuntimeException("suggestName: this suggester (\"" + suggestName + "\") was not yet built; valid suggestNames: " + indexState.suggesters.keySet());
}
final String text = suggestLookupRequest.getText();
final int count = suggestLookupRequest.getCount() == 0 ? 5 : suggestLookupRequest.getCount();
final boolean allTermsRequired = suggestLookupRequest.getAllTermsRequired();
final boolean highlight = suggestLookupRequest.getHighlight();
final Set<BytesRef> contexts;
if (!suggestLookupRequest.getContextsList().isEmpty()) {
contexts = new HashSet<>();
for (String each : suggestLookupRequest.getContextsList()) {
contexts.add(new BytesRef(each));
}
} else {
contexts = null;
}
List<Lookup.LookupResult> results;
try {
// Make sure lookup object is not a subclass of AnalyzingInfixSuggester.
if (lookup instanceof AnalyzingInfixSuggester && lookup.getClass() == AnalyzingInfixSuggester.class) {
results = ((AnalyzingInfixSuggester) lookup).lookup(text, contexts, count, allTermsRequired, highlight);
} else {
results = lookup.lookup(text, contexts, false, count);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
SuggestLookupResponse.Builder suggestLookupResponseBuilder = SuggestLookupResponse.newBuilder();
for (Lookup.LookupResult result : results) {
OneSuggestLookupResponse.Builder oneSuggestLookupResponseBuilder = OneSuggestLookupResponse.newBuilder();
if (result.highlightKey != null) {
oneSuggestLookupResponseBuilder.setSuggestLookupHighlight((SuggestLookupHighlight) result.highlightKey);
} else {
oneSuggestLookupResponseBuilder.setKey(result.key.toString());
}
oneSuggestLookupResponseBuilder.setWeight(result.value);
if (result.payload != null) {
oneSuggestLookupResponseBuilder.setPayload(result.payload.utf8ToString());
}
suggestLookupResponseBuilder.addResults(oneSuggestLookupResponseBuilder);
}
return suggestLookupResponseBuilder.build();
}
Aggregations