Java Code Examples for org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse#AnalyzeToken

The following examples show how to use org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse#AnalyzeToken . 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: AnalyzeDemo.java    From elasticsearch-full with Apache License 2.0 6 votes vote down vote up
@Test
public void testTokenizer() throws Exception {
    AnalyzeRequest analyzeRequest = new AnalyzeRequest();
    analyzeRequest.text("My œsophagus caused a débâcle");
    /**
     * whitespace (空白字符)分词器按空白字符 —— 空格、tabs、换行符等等进行简单拆分
     * letter 分词器 ,采用另外一种策略,按照任何非字符进行拆分
     * standard 分词器使用 Unicode 文本分割算法
     */
    analyzeRequest.addTokenFilter("standard");
    analyzeRequest.addCharFilter("asciifolding");
    ActionFuture<AnalyzeResponse> analyzeResponseActionFuture =  client.admin().indices().analyze(analyzeRequest);
    List<AnalyzeResponse.AnalyzeToken> analyzeTokens =  analyzeResponseActionFuture.actionGet().getTokens();
    for (AnalyzeResponse.AnalyzeToken analyzeToken : analyzeTokens){
        System.out.println(analyzeToken.getTerm());
    }
}
 
Example 2
Source File: ESIndex.java    From pyramid with Apache License 2.0 6 votes vote down vote up
/**
 * analyze the given text using the provided analyzer, return an ngram
 * @param text
 * @param analyzer
 * @return
 */
public Ngram analyze(String text, String analyzer){
    List<AnalyzeResponse.AnalyzeToken> tokens = client.admin().indices().prepareAnalyze(indexName,text).setAnalyzer(analyzer).get().getTokens();

    Ngram ngram = new Ngram();
    StringBuilder sb = new StringBuilder();
    for (int i=0;i<tokens.size();i++)
    {
        AnalyzeResponse.AnalyzeToken token = tokens.get(i);
        sb.append(token.getTerm());
        if (i!=tokens.size()-1){
            sb.append(" ");
        }
    }
    ngram.setNgram(sb.toString());
    return ngram;

}
 
Example 3
Source File: ElasticsearchUtil.java    From SpringBootLearn with Apache License 2.0 5 votes vote down vote up
/**
 * ik分词测试
 * @Author lihaodong
 * @Description
 * @Date 20:09 2018/12/21
 * @Param []
 * @return java.lang.String
 **/
public static String ik() {
    StringBuilder stringBuilder = new StringBuilder();
    AnalyzeRequest analyzeRequest = new AnalyzeRequest("entity")
            .text("书名")
            .analyzer("standard");  //ik_smart  ik_max_word  standard
    List<AnalyzeResponse.AnalyzeToken> tokens = client.admin().indices()
            .analyze(analyzeRequest)
            .actionGet()
            .getTokens();
    for (AnalyzeResponse.AnalyzeToken token : tokens) {
        stringBuilder.append(token.getTerm() + "\\r\\n");
    }
    return stringBuilder.toString();
}
 
Example 4
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 5
Source File: XPackBaseDemo.java    From elasticsearch-full with Apache License 2.0 5 votes vote down vote up
@Test
public void testClientConnection() throws Exception {
    AnalyzeRequest analyzeRequest = new AnalyzeRequest();
    analyzeRequest.text("美女");
    ActionFuture<AnalyzeResponse> analyzeResponseActionFuture = client.admin().indices().analyze(analyzeRequest);
    List<AnalyzeResponse.AnalyzeToken> analyzeTokens =  analyzeResponseActionFuture.actionGet().getTokens();
    for (AnalyzeResponse.AnalyzeToken analyzeToken  : analyzeTokens){
        System.out.println(analyzeToken.getTerm());
    }
}
 
Example 6
Source File: BaseDemo.java    From elasticsearch-full with Apache License 2.0 5 votes vote down vote up
@Test
public void testClientConnection() throws Exception {
    AnalyzeRequest analyzeRequest = new AnalyzeRequest();
    analyzeRequest.text("中华人民共和国");
    ActionFuture<AnalyzeResponse> analyzeResponseActionFuture = client.admin().indices().analyze(analyzeRequest);
    List<AnalyzeResponse.AnalyzeToken> analyzeTokens =  analyzeResponseActionFuture.actionGet().getTokens();
    for (AnalyzeResponse.AnalyzeToken analyzeToken  : analyzeTokens){
        System.out.println(analyzeToken.getTerm());
    }
}
 
Example 7
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 8
Source File: URLTokenFilterIntegrationTest.java    From elasticsearch-analysis-url with Apache License 2.0 5 votes vote down vote up
@Test
public void testPassthrough() {
    List<AnalyzeResponse.AnalyzeToken> tokens = analyzeURL("http://foo.com:9200/foo.bar baz bat.blah", "url_host_passthrough");
    assertThat(tokens, hasSize(4));
    assertThat(tokens.get(0).getTerm(), equalTo("foo.com"));
    assertThat(tokens.get(1).getTerm(), equalTo("com"));
    assertThat(tokens.get(2).getTerm(), equalTo("baz"));
    assertThat(tokens.get(3).getTerm(), equalTo("bat.blah"));
}
 
Example 9
Source File: URLTokenizerIntegrationTest.java    From elasticsearch-analysis-url with Apache License 2.0 5 votes vote down vote up
@Test
public void testAnalyze() {
    assertTokensContain(URLTokenizerTest.TEST_HTTP_URL, "tokenizer_url_protocol", "http");
    assertTokensContain(URLTokenizerTest.TEST_HTTPS_URL, "tokenizer_url_protocol", "https");

    assertTokensContain(URLTokenizerTest.TEST_HTTP_URL, "tokenizer_url_host", "www.foo.bar.com", "foo.bar.com", "bar.com", "com");
    List<AnalyzeResponse.AnalyzeToken> hostTokens = assertTokensContain(URLTokenizerTest.TEST_HTTP_URL, "tokenizer_url_host_single", "www.foo.bar.com");
    assertThat(hostTokens, hasSize(1));

    assertTokensContain(URLTokenizerTest.TEST_HTTP_URL, "tokenizer_url_all", "www.foo.bar.com:9200", "http://www.foo.bar.com");

    assertTokensContain(URLTokenizerTest.TEST_HTTP_URL, "tokenizer_url_protocol_and_host", "http", "www.foo.bar.com", "foo.bar.com", "bar.com", "com");

    assertTokensContain("foo.bar.com/baz.html/query?a=1", "tokenizer_url_all_malformed", "foo.bar.com", "/baz.html/query");
}
 
Example 10
Source File: URLTokenizerIntegrationTest.java    From elasticsearch-analysis-url with Apache License 2.0 5 votes vote down vote up
private List<AnalyzeResponse.AnalyzeToken> assertTokensContain(String url, String analyzer, String... expected) {
    List<AnalyzeResponse.AnalyzeToken> tokens = analyzeURL(url, analyzer);
    for (String e : expected) {
        assertThat(tokens, hasItem(Matchers.<AnalyzeResponse.AnalyzeToken>hasProperty("term", equalTo(e))));
    }
    return tokens;
}
 
Example 11
Source File: ESIndex.java    From pyramid with Apache License 2.0 5 votes vote down vote up
/**
 * analyze the given text using the provided analyzer, return an ngram
 * @param text
 * @param analyzer
 * @return
 */
public List<String> analyzeString(String text, String analyzer){
    List<AnalyzeResponse.AnalyzeToken> tokens = client.admin().indices().prepareAnalyze(indexName,text).setAnalyzer(analyzer).get().getTokens();
    List<String> list = new ArrayList<>();
    for (int i=0;i<tokens.size();i++)
    {
        AnalyzeResponse.AnalyzeToken token = tokens.get(i);
        list.add(token.getTerm());

    }
    return list;

}
 
Example 12
Source File: URLTokenFilterIntegrationTest.java    From elasticsearch-analysis-url with Apache License 2.0 4 votes vote down vote up
@Test
public void testEmptyString() {
    List<AnalyzeResponse.AnalyzeToken> tokens = analyzeURL("", "url_protocol");
    assertThat("no tokens", tokens, hasSize(0));
}
 
Example 13
Source File: URLTokenFilterIntegrationTest.java    From elasticsearch-analysis-url with Apache License 2.0 4 votes vote down vote up
private void assertURLAnalyzesTo(String url, String analyzer, String expected) {
    List<AnalyzeResponse.AnalyzeToken> tokens = analyzeURL(url, analyzer);
    assertThat("a URL part was parsed", tokens, hasSize(1));
    assertEquals("term value", expected, tokens.get(0).getTerm());
}
 
Example 14
Source File: URLTokenizerIntegrationTest.java    From elasticsearch-analysis-url with Apache License 2.0 4 votes vote down vote up
@Test
public void testAnalyzeWhole() throws Exception {
    List<AnalyzeResponse.AnalyzeToken> tokens = analyzeURL("http://foo.bar.com", "tokenizer_url_all_malformed");
    assertThat(tokens, notNullValue());
    assertThat(tokens, hasSize(7));
}
 
Example 15
Source File: URLAnalysisTestCase.java    From elasticsearch-analysis-url with Apache License 2.0 4 votes vote down vote up
protected List<AnalyzeResponse.AnalyzeToken> analyzeURL(String url, String analyzer) {
    return client().admin().indices().prepareAnalyze(INDEX, url).setAnalyzer(analyzer).get().getTokens();
}