Search in sources :

Example 11 with AnalyzeResponse

use of org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse in project elasticsearch by elastic.

the class AnalyzeActionIT method testCustomCharFilterInRequest.

public void testCustomCharFilterInRequest() throws Exception {
    Map<String, Object> charFilterSettings = new HashMap<>();
    charFilterSettings.put("type", "mapping");
    charFilterSettings.put("mappings", new String[] { "ph => f", "qu => q" });
    AnalyzeResponse analyzeResponse = client().admin().indices().prepareAnalyze().setText("jeff quit phish").setTokenizer("keyword").addCharFilter(charFilterSettings).setExplain(true).get();
    assertThat(analyzeResponse.detail().analyzer(), IsNull.nullValue());
    //charfilters
    assertThat(analyzeResponse.detail().charfilters().length, equalTo(1));
    assertThat(analyzeResponse.detail().charfilters()[0].getName(), equalTo("_anonymous_charfilter_[0]"));
    assertThat(analyzeResponse.detail().charfilters()[0].getTexts().length, equalTo(1));
    assertThat(analyzeResponse.detail().charfilters()[0].getTexts()[0], equalTo("jeff qit fish"));
    //tokenizer
    assertThat(analyzeResponse.detail().tokenizer().getName(), equalTo("keyword"));
    assertThat(analyzeResponse.detail().tokenizer().getTokens().length, equalTo(1));
    assertThat(analyzeResponse.detail().tokenizer().getTokens()[0].getTerm(), equalTo("jeff qit fish"));
    assertThat(analyzeResponse.detail().tokenizer().getTokens()[0].getStartOffset(), equalTo(0));
    assertThat(analyzeResponse.detail().tokenizer().getTokens()[0].getEndOffset(), equalTo(15));
    assertThat(analyzeResponse.detail().tokenizer().getTokens()[0].getPositionLength(), equalTo(1));
}
Also used : HashMap(java.util.HashMap) AnalyzeResponse(org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse)

Example 12 with AnalyzeResponse

use of org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse in project elasticsearch by elastic.

the class AnalyzeActionIT method testAnalyzeWithNoIndex.

public void testAnalyzeWithNoIndex() throws Exception {
    AnalyzeResponse analyzeResponse = client().admin().indices().prepareAnalyze("THIS IS A TEST").setAnalyzer("simple").get();
    assertThat(analyzeResponse.getTokens().size(), equalTo(4));
    analyzeResponse = client().admin().indices().prepareAnalyze("THIS IS A TEST").setTokenizer("keyword").addTokenFilter("lowercase").get();
    assertThat(analyzeResponse.getTokens().size(), equalTo(1));
    assertThat(analyzeResponse.getTokens().get(0).getTerm(), equalTo("this is a test"));
    analyzeResponse = client().admin().indices().prepareAnalyze("THIS IS A TEST").setTokenizer("standard").addTokenFilter("lowercase").addTokenFilter("reverse").get();
    assertThat(analyzeResponse.getTokens().size(), equalTo(4));
    AnalyzeResponse.AnalyzeToken token = analyzeResponse.getTokens().get(0);
    assertThat(token.getTerm(), equalTo("siht"));
    token = analyzeResponse.getTokens().get(1);
    assertThat(token.getTerm(), equalTo("si"));
    token = analyzeResponse.getTokens().get(2);
    assertThat(token.getTerm(), equalTo("a"));
    token = analyzeResponse.getTokens().get(3);
    assertThat(token.getTerm(), equalTo("tset"));
    analyzeResponse = client().admin().indices().prepareAnalyze("of course").setTokenizer("standard").addTokenFilter("stop").get();
    assertThat(analyzeResponse.getTokens().size(), equalTo(1));
    assertThat(analyzeResponse.getTokens().get(0).getTerm(), equalTo("course"));
    assertThat(analyzeResponse.getTokens().get(0).getPosition(), equalTo(1));
    assertThat(analyzeResponse.getTokens().get(0).getStartOffset(), equalTo(3));
    assertThat(analyzeResponse.getTokens().get(0).getEndOffset(), equalTo(9));
    assertThat(analyzeResponse.getTokens().get(0).getPositionLength(), equalTo(1));
}
Also used : AnalyzeResponse(org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse)

Example 13 with AnalyzeResponse

use of org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse in project elasticsearch by elastic.

the class AnalyzeActionIT method testDetailAnalyze.

public void testDetailAnalyze() throws Exception {
    assertAcked(prepareCreate("test").addAlias(new Alias("alias")).setSettings(Settings.builder().put("index.analysis.char_filter.my_mapping.type", "mapping").putArray("index.analysis.char_filter.my_mapping.mappings", "PH=>F").put("index.analysis.analyzer.test_analyzer.type", "custom").put("index.analysis.analyzer.test_analyzer.position_increment_gap", "100").put("index.analysis.analyzer.test_analyzer.tokenizer", "standard").putArray("index.analysis.analyzer.test_analyzer.char_filter", "my_mapping").putArray("index.analysis.analyzer.test_analyzer.filter", "snowball")));
    ensureGreen();
    for (int i = 0; i < 10; i++) {
        AnalyzeResponse analyzeResponse = admin().indices().prepareAnalyze().setIndex(indexOrAlias()).setText("THIS IS A PHISH").setExplain(true).addCharFilter("my_mapping").setTokenizer("keyword").addTokenFilter("lowercase").get();
        assertThat(analyzeResponse.detail().analyzer(), IsNull.nullValue());
        //charfilters
        assertThat(analyzeResponse.detail().charfilters().length, equalTo(1));
        assertThat(analyzeResponse.detail().charfilters()[0].getName(), equalTo("my_mapping"));
        assertThat(analyzeResponse.detail().charfilters()[0].getTexts().length, equalTo(1));
        assertThat(analyzeResponse.detail().charfilters()[0].getTexts()[0], equalTo("THIS IS A FISH"));
        //tokenizer
        assertThat(analyzeResponse.detail().tokenizer().getName(), equalTo("keyword"));
        assertThat(analyzeResponse.detail().tokenizer().getTokens().length, equalTo(1));
        assertThat(analyzeResponse.detail().tokenizer().getTokens()[0].getTerm(), equalTo("THIS IS A FISH"));
        assertThat(analyzeResponse.detail().tokenizer().getTokens()[0].getStartOffset(), equalTo(0));
        assertThat(analyzeResponse.detail().tokenizer().getTokens()[0].getEndOffset(), equalTo(15));
        //tokenfilters
        assertThat(analyzeResponse.detail().tokenfilters().length, equalTo(1));
        assertThat(analyzeResponse.detail().tokenfilters()[0].getName(), equalTo("lowercase"));
        assertThat(analyzeResponse.detail().tokenfilters()[0].getTokens().length, equalTo(1));
        assertThat(analyzeResponse.detail().tokenfilters()[0].getTokens()[0].getTerm(), equalTo("this is a fish"));
        assertThat(analyzeResponse.detail().tokenfilters()[0].getTokens()[0].getPosition(), equalTo(0));
        assertThat(analyzeResponse.detail().tokenfilters()[0].getTokens()[0].getStartOffset(), equalTo(0));
        assertThat(analyzeResponse.detail().tokenfilters()[0].getTokens()[0].getEndOffset(), equalTo(15));
    }
}
Also used : Alias(org.elasticsearch.action.admin.indices.alias.Alias) AnalyzeResponse(org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse)

Example 14 with AnalyzeResponse

use of org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse in project elasticsearch by elastic.

the class AnalyzeActionIT method testCustomTokenizerInRequest.

public void testCustomTokenizerInRequest() throws Exception {
    Map<String, Object> tokenizerSettings = new HashMap<>();
    tokenizerSettings.put("type", "nGram");
    tokenizerSettings.put("min_gram", 2);
    tokenizerSettings.put("max_gram", 2);
    AnalyzeResponse analyzeResponse = client().admin().indices().prepareAnalyze().setText("good").setTokenizer(tokenizerSettings).setExplain(true).get();
    //tokenizer
    assertThat(analyzeResponse.detail().tokenizer().getName(), equalTo("_anonymous_tokenizer"));
    assertThat(analyzeResponse.detail().tokenizer().getTokens().length, equalTo(3));
    assertThat(analyzeResponse.detail().tokenizer().getTokens()[0].getTerm(), equalTo("go"));
    assertThat(analyzeResponse.detail().tokenizer().getTokens()[0].getStartOffset(), equalTo(0));
    assertThat(analyzeResponse.detail().tokenizer().getTokens()[0].getEndOffset(), equalTo(2));
    assertThat(analyzeResponse.detail().tokenizer().getTokens()[0].getPosition(), equalTo(0));
    assertThat(analyzeResponse.detail().tokenizer().getTokens()[0].getPositionLength(), equalTo(1));
    assertThat(analyzeResponse.detail().tokenizer().getTokens()[1].getTerm(), equalTo("oo"));
    assertThat(analyzeResponse.detail().tokenizer().getTokens()[1].getStartOffset(), equalTo(1));
    assertThat(analyzeResponse.detail().tokenizer().getTokens()[1].getEndOffset(), equalTo(3));
    assertThat(analyzeResponse.detail().tokenizer().getTokens()[1].getPosition(), equalTo(1));
    assertThat(analyzeResponse.detail().tokenizer().getTokens()[1].getPositionLength(), equalTo(1));
    assertThat(analyzeResponse.detail().tokenizer().getTokens()[2].getTerm(), equalTo("od"));
    assertThat(analyzeResponse.detail().tokenizer().getTokens()[2].getStartOffset(), equalTo(2));
    assertThat(analyzeResponse.detail().tokenizer().getTokens()[2].getEndOffset(), equalTo(4));
    assertThat(analyzeResponse.detail().tokenizer().getTokens()[2].getPosition(), equalTo(2));
    assertThat(analyzeResponse.detail().tokenizer().getTokens()[2].getPositionLength(), equalTo(1));
}
Also used : HashMap(java.util.HashMap) AnalyzeResponse(org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse)

Example 15 with AnalyzeResponse

use of org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse in project incubator-sdap-mudrod by apache.

the class ESDriver method customAnalyzing.

public String customAnalyzing(String indexName, String analyzer, String str) throws InterruptedException, ExecutionException {
    String[] strList = str.toLowerCase().split(",");
    for (int i = 0; i < strList.length; i++) {
        String tmp = "";
        AnalyzeResponse r = client.admin().indices().prepareAnalyze(strList[i]).setIndex(indexName).setAnalyzer(analyzer).execute().get();
        for (AnalyzeToken token : r.getTokens()) {
            tmp += token.getTerm() + " ";
        }
        strList[i] = tmp.trim();
    }
    return String.join(",", strList);
}
Also used : AnalyzeToken(org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse.AnalyzeToken) AnalyzeResponse(org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse)

Aggregations

AnalyzeResponse (org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse)25 Alias (org.elasticsearch.action.admin.indices.alias.Alias)10 AnalyzeRequest (org.elasticsearch.action.admin.indices.analyze.AnalyzeRequest)4 HashMap (java.util.HashMap)3 AnalyzeRequestBuilder (org.elasticsearch.action.admin.indices.analyze.AnalyzeRequestBuilder)2 AnalyzeToken (org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse.AnalyzeToken)2 SearchException (core.framework.search.SearchException)1 StopWatch (core.framework.util.StopWatch)1 ArrayList (java.util.ArrayList)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1