Skip to content

Instantly share code, notes, and snippets.

@evanwong
Created September 10, 2013 15:46
Show Gist options
  • Save evanwong/6511366 to your computer and use it in GitHub Desktop.
Save evanwong/6511366 to your computer and use it in GitHub Desktop.
elasticsearch java client to query a completion suggest v0.90.3
String clusterName = "testcluster";
String indexName = "textindex";
String suggestionName = "suggestion";
NodeBuilder nodeBuilder = NodeBuilder.nodeBuilder().clusterName(clusterName).client(true);
Node node = nodeBuilder.node();
try {
Client client = node.client();
CompletionSuggestionBuilder completionSuggestionBuilder = new CompletionSuggestionBuilder(suggestionName);
SuggestResponse suggestionRes = client.prepareSuggest(indexName).setSuggestText("tes").addSuggestion(completionSuggestionBuilder.field("suggest")).execute().actionGet();
Suggest suggest = suggestionRes.getSuggest();
Suggest.Suggestion suggestion = suggest.getSuggestion(suggestionName);
Iterator<Entry> suggestionIterator = suggestion.iterator();
while (suggestionIterator.hasNext()) {
Entry entry = suggestionIterator.next();
List<Entry.Option> options = entry.getOptions();
for (Entry.Option option : options) {
System.out.println(option.getText());
}
}
} finally {
node.close();
}
{
"testtype": {
"properties": {
"suggest": {
"type": "completion"
}
}
}
}
{
"suggest": [
"test123",
"test456",
"nomatch"
]
}
{
"suggestion": {
"text": "tes",
"completion": {
"field": "suggest"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment