Created
September 10, 2013 15:46
-
-
Save evanwong/6511366 to your computer and use it in GitHub Desktop.
elasticsearch java client to query a completion suggest
v0.90.3
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
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(); | |
} |
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
{ | |
"testtype": { | |
"properties": { | |
"suggest": { | |
"type": "completion" | |
} | |
} | |
} | |
} |
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
{ | |
"suggest": [ | |
"test123", | |
"test456", | |
"nomatch" | |
] | |
} |
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
{ | |
"suggestion": { | |
"text": "tes", | |
"completion": { | |
"field": "suggest" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment