问题
I use Elasticsearch 1.7.4 and its Java API. Currently, I want to count the top 10 high frequency searching words by user. So I have to record the words of query text which user type to search, and before recording the words I have to analyze the query text. I find the restful way to analyze text as the link says, but I can't find the api in TransportClient.
do anyone know how to analyze text in elasticsearch using Java api or some other way rather than requesting the restful api?
回答1:
Using the example from the documentation (where the text "this is a test" is being analyzed using the standard
analyzer), here is an example of how one might accomplish the same analysis using the Java API:
TransportClient client = new TransportClient();
AnalyzeRequest request = (new AnalyzeRequest("this is a test")).analyzer("standard");
List<AnalyzeResponse.AnalyzeToken> tokens = client.admin().indices().analyze(request).actionGet().getTokens();
for (AnalyzeResponse.AnalyzeToken token : tokens)
{
// do something with each token...
}
Hope this helps!
来源:https://stackoverflow.com/questions/35040625/how-to-analyze-text-in-elasticsearch-using-java-api