Java Code Examples for org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse#getTokens()

The following examples show how to use org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse#getTokens() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Test.java    From dht-spider with MIT License 5 votes vote down vote up
public static void anylyze() throws Exception{
    AnalyzeRequest request = new AnalyzeRequest();
    request.text("ReadMe.txt Screenshots,ReadMe.txt Screenshots,1.jpg COVER.jpg Screenshots,4.jpg Screenshots,2.jpg Screenshots,3.jpg FIFA.Street.2012 - RELOADED.rar");
    request.analyzer("ik_max_word");

    AnalyzeResponse response = client.indices().analyze(request, RequestOptions.DEFAULT);
    List<AnalyzeResponse.AnalyzeToken> tokens =
            response.getTokens();
    for(AnalyzeResponse.AnalyzeToken to:tokens){
        System.out.println(to.getTerm());
    }
    System.out.println(response.getTokens().get(0).getAttributes());
}
 
Example 2
Source File: CrudDemo.java    From javabase with Apache License 2.0 5 votes vote down vote up
private static void showAnaylzerText(IndicesAdminClient indicesAdminClient,String analyzerName, String text) {
    AnalyzeResponse analyzeResponse = indicesAdminClient.analyze(new AnalyzeRequest(INDEX_NAME).analyzer(analyzerName).text(text)).actionGet();
    List<AnalyzeResponse.AnalyzeToken> token=analyzeResponse.getTokens();
    for (AnalyzeResponse.AnalyzeToken analyzeToken : token) {
        log.info(analyzerName+": {}",analyzeToken.getTerm());
    }

}
 
Example 3
Source File: AnalyzeHelper.java    From es-service-parent with Apache License 2.0 5 votes vote down vote up
/**
 * 分词-无法分词则返回空集合
 * 
 * @param analyzer
 * @param str
 * @return
 */
public static List<String> analyze(String analyzer, String str) {

    AnalyzeResponse ar = null;
    try {
        AnalyzeRequest request = new AnalyzeRequest(str).analyzer(analyzer).index(
                getCurrentValidIndex());
        ar = ESClient.getClient().admin().indices().analyze(request).actionGet();
    } catch (IndexMissingException e) {
        if (!reLoad) {
            synchronized (AnalyzeHelper.class) {
                if (!reLoad) {
                    reLoad = true;
                }
            }
        }
        return analyze(analyzer, str);
    }

    if (ar == null || ar.getTokens() == null || ar.getTokens().size() < 1) {
        return Lists.newArrayList();
    }
    List<String> analyzeTokens = Lists.newArrayList();
    for (AnalyzeToken at : ar.getTokens()) {
        analyzeTokens.add(at.getTerm());
    }
    return analyzeTokens;
}
 
Example 4
Source File: SetupIndexServiceImpl.java    From searchanalytics-bigdata with MIT License 5 votes vote down vote up
@Override
public List<String> analyzeText(final String indexAliasName,
		final String analyzer, final String[] tokenFilters,
		final String text) {
	final List<String> tokens = new ArrayList<String>();
	final AnalyzeRequestBuilder analyzeRequestBuilder = searchClientService
			.getClient().admin().indices().prepareAnalyze(text);
	if (analyzer != null) {
		analyzeRequestBuilder.setIndex(indexAliasName);
	}
	if (analyzer != null) {
		analyzeRequestBuilder.setAnalyzer(analyzer);
	}
	if (tokenFilters != null) {
		analyzeRequestBuilder.setTokenFilters(tokenFilters);
	}
	logger.debug(
			"Analyze request is text: {}, analyzer: {}, tokenfilters: {}",
			new Object[] { analyzeRequestBuilder.request().text(),
					analyzeRequestBuilder.request().analyzer(),
					analyzeRequestBuilder.request().tokenFilters() });
	final AnalyzeResponse analyzeResponse = analyzeRequestBuilder.get();
	try {
		if (analyzeResponse != null) {
			logger.debug(
					"Analyze response is : {}",
					analyzeResponse
							.toXContent(jsonBuilder().startObject(),
									ToXContent.EMPTY_PARAMS).prettyPrint()
							.string());
		}
	} catch (final IOException e) {
		logger.error("Error printing response.", e);
	}
	for (final AnalyzeToken analyzeToken : analyzeResponse.getTokens()) {
		tokens.add(analyzeToken.getTerm());
	}
	return tokens;
}
 
Example 5
Source File: SetupIndexServiceImpl.java    From elasticsearch-tutorial with MIT License 4 votes vote down vote up
@Override
public List<String> analyzeText(String indexAliasName, String analyzer, String[] tokenFilters, String text)
{
    List<String> tokens = new ArrayList<String>();
    
    AnalyzeRequestBuilder analyzeRequestBuilder = searchClientService.getClient().admin().indices().prepareAnalyze(text);
    
    if(analyzer !=null)
    {
        analyzeRequestBuilder.setIndex(indexAliasName);
    }
    if(analyzer !=null)
    {
        analyzeRequestBuilder.setAnalyzer(analyzer);
    }
    
    if(tokenFilters !=null)
    {
        analyzeRequestBuilder.setTokenFilters(tokenFilters);
    }
    
    logger.debug("Analyze request is text: {}, analyzer: {}, tokenfilters: {}", new Object[]{analyzeRequestBuilder.request().text(), 
                                                                                analyzeRequestBuilder.request().analyzer(),
                                                                                analyzeRequestBuilder.request().tokenFilters()});
                                                                                        
    AnalyzeResponse analyzeResponse = analyzeRequestBuilder.get();
    
    try
    {
        if(analyzeResponse != null)
        {
            logger.debug("Analyze response is : {}", analyzeResponse.toXContent(jsonBuilder().startObject(), ToXContent.EMPTY_PARAMS).prettyPrint().string());
        }
    } catch (IOException e)
    {
        logger.error("Error printing response.", e);
    }
    
    for (AnalyzeToken analyzeToken : analyzeResponse.getTokens())
    {
        tokens.add(analyzeToken.getTerm());
    }
    return tokens;
}