Search in sources :

Example 6 with AnalyzeResponse

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

the class AnalyzeActionIT method testThatStandardAndDefaultAnalyzersAreSame.

// issue #5974
public void testThatStandardAndDefaultAnalyzersAreSame() throws Exception {
    AnalyzeResponse response = client().admin().indices().prepareAnalyze("this is a test").setAnalyzer("standard").get();
    assertTokens(response, "this", "is", "a", "test");
    response = client().admin().indices().prepareAnalyze("this is a test").setAnalyzer("default").get();
    assertTokens(response, "this", "is", "a", "test");
    response = client().admin().indices().prepareAnalyze("this is a test").get();
    assertTokens(response, "this", "is", "a", "test");
}
Also used : AnalyzeResponse(org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse)

Example 7 with AnalyzeResponse

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

the class AnalyzeActionIT method testAnalyzeNormalizedKeywordField.

public void testAnalyzeNormalizedKeywordField() throws IOException {
    assertAcked(prepareCreate("test").addAlias(new Alias("alias")).setSettings(Settings.builder().put(indexSettings()).put("index.analysis.normalizer.my_normalizer.type", "custom").putArray("index.analysis.normalizer.my_normalizer.filter", "lowercase")).addMapping("test", "keyword", "type=keyword,normalizer=my_normalizer"));
    ensureGreen("test");
    AnalyzeResponse analyzeResponse = client().admin().indices().prepareAnalyze(indexOrAlias(), "ABC").setField("keyword").get();
    assertThat(analyzeResponse.getTokens().size(), equalTo(1));
    AnalyzeResponse.AnalyzeToken token = analyzeResponse.getTokens().get(0);
    assertThat(token.getTerm(), equalTo("abc"));
    assertThat(token.getStartOffset(), equalTo(0));
    assertThat(token.getEndOffset(), equalTo(3));
    assertThat(token.getPosition(), equalTo(0));
    assertThat(token.getPositionLength(), equalTo(1));
}
Also used : Alias(org.elasticsearch.action.admin.indices.alias.Alias) AnalyzeResponse(org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse)

Example 8 with AnalyzeResponse

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

the class AnalyzeActionIT method testAnalyzeWithCharFilters.

public void testAnalyzeWithCharFilters() throws Exception {
    assertAcked(prepareCreate("test").addAlias(new Alias("alias")).setSettings(Settings.builder().put(indexSettings()).put("index.analysis.char_filter.custom_mapping.type", "mapping").putArray("index.analysis.char_filter.custom_mapping.mappings", "ph=>f", "qu=>q").put("index.analysis.analyzer.custom_with_char_filter.tokenizer", "standard").putArray("index.analysis.analyzer.custom_with_char_filter.char_filter", "custom_mapping")));
    ensureGreen();
    AnalyzeResponse analyzeResponse = client().admin().indices().prepareAnalyze("<h2><b>THIS</b> IS A</h2> <a href=\"#\">TEST</a>").setTokenizer("standard").addCharFilter("html_strip").get();
    assertThat(analyzeResponse.getTokens().size(), equalTo(4));
    analyzeResponse = client().admin().indices().prepareAnalyze("THIS IS A <b>TEST</b>").setTokenizer("keyword").addTokenFilter("lowercase").addCharFilter("html_strip").get();
    assertThat(analyzeResponse.getTokens().size(), equalTo(1));
    assertThat(analyzeResponse.getTokens().get(0).getTerm(), equalTo("this is a test"));
    analyzeResponse = client().admin().indices().prepareAnalyze(indexOrAlias(), "jeff quit phish").setTokenizer("keyword").addTokenFilter("lowercase").addCharFilter("custom_mapping").get();
    assertThat(analyzeResponse.getTokens().size(), equalTo(1));
    assertThat(analyzeResponse.getTokens().get(0).getTerm(), equalTo("jeff qit fish"));
    analyzeResponse = client().admin().indices().prepareAnalyze(indexOrAlias(), "<a href=\"#\">jeff quit fish</a>").setTokenizer("standard").addCharFilter("html_strip").addCharFilter("custom_mapping").get();
    assertThat(analyzeResponse.getTokens().size(), equalTo(3));
    AnalyzeResponse.AnalyzeToken token = analyzeResponse.getTokens().get(0);
    assertThat(token.getTerm(), equalTo("jeff"));
    token = analyzeResponse.getTokens().get(1);
    assertThat(token.getTerm(), equalTo("qit"));
    token = analyzeResponse.getTokens().get(2);
    assertThat(token.getTerm(), equalTo("fish"));
}
Also used : Alias(org.elasticsearch.action.admin.indices.alias.Alias) AnalyzeResponse(org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse)

Example 9 with AnalyzeResponse

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

the class AnalyzeActionIT method testDetailAnalyzeWithMultiValuesWithCustomAnalyzer.

public void testDetailAnalyzeWithMultiValuesWithCustomAnalyzer() 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", "lowercase")));
    ensureGreen();
    client().admin().indices().preparePutMapping("test").setType("document").setSource("simple", "type=text,analyzer=simple,position_increment_gap=100").get();
    //only analyzer =
    String[] texts = new String[] { "this is a PHISH", "the troubled text" };
    AnalyzeResponse analyzeResponse = client().admin().indices().prepareAnalyze().setIndex(indexOrAlias()).setText(texts).setExplain(true).setAnalyzer("test_analyzer").setText(texts).execute().get();
    // charfilter
    assertThat(analyzeResponse.detail().charfilters().length, equalTo(1));
    assertThat(analyzeResponse.detail().charfilters()[0].getName(), equalTo("my_mapping"));
    assertThat(analyzeResponse.detail().charfilters()[0].getTexts().length, equalTo(2));
    assertThat(analyzeResponse.detail().charfilters()[0].getTexts()[0], equalTo("this is a FISH"));
    assertThat(analyzeResponse.detail().charfilters()[0].getTexts()[1], equalTo("the troubled text"));
    // tokenizer
    assertThat(analyzeResponse.detail().tokenizer().getName(), equalTo("standard"));
    assertThat(analyzeResponse.detail().tokenizer().getTokens().length, equalTo(7));
    AnalyzeResponse.AnalyzeToken token = analyzeResponse.detail().tokenizer().getTokens()[3];
    assertThat(token.getTerm(), equalTo("FISH"));
    assertThat(token.getPosition(), equalTo(3));
    assertThat(token.getStartOffset(), equalTo(10));
    assertThat(token.getEndOffset(), equalTo(15));
    assertThat(token.getPositionLength(), equalTo(1));
    token = analyzeResponse.detail().tokenizer().getTokens()[5];
    assertThat(token.getTerm(), equalTo("troubled"));
    assertThat(token.getPosition(), equalTo(105));
    assertThat(token.getStartOffset(), equalTo(20));
    assertThat(token.getEndOffset(), equalTo(28));
    assertThat(token.getPositionLength(), equalTo(1));
    // tokenfilter(snowball)
    assertThat(analyzeResponse.detail().tokenfilters().length, equalTo(2));
    assertThat(analyzeResponse.detail().tokenfilters()[0].getName(), equalTo("snowball"));
    assertThat(analyzeResponse.detail().tokenfilters()[0].getTokens().length, equalTo(7));
    token = analyzeResponse.detail().tokenfilters()[0].getTokens()[3];
    assertThat(token.getTerm(), equalTo("FISH"));
    assertThat(token.getPosition(), equalTo(3));
    assertThat(token.getStartOffset(), equalTo(10));
    assertThat(token.getEndOffset(), equalTo(15));
    assertThat(token.getPositionLength(), equalTo(1));
    token = analyzeResponse.detail().tokenfilters()[0].getTokens()[5];
    assertThat(token.getTerm(), equalTo("troubl"));
    assertThat(token.getPosition(), equalTo(105));
    assertThat(token.getStartOffset(), equalTo(20));
    assertThat(token.getEndOffset(), equalTo(28));
    assertThat(token.getPositionLength(), equalTo(1));
    // tokenfilter(lowercase)
    assertThat(analyzeResponse.detail().tokenfilters()[1].getName(), equalTo("lowercase"));
    assertThat(analyzeResponse.detail().tokenfilters()[1].getTokens().length, equalTo(7));
    token = analyzeResponse.detail().tokenfilters()[1].getTokens()[3];
    assertThat(token.getTerm(), equalTo("fish"));
    assertThat(token.getPosition(), equalTo(3));
    assertThat(token.getStartOffset(), equalTo(10));
    assertThat(token.getEndOffset(), equalTo(15));
    assertThat(token.getPositionLength(), equalTo(1));
    token = analyzeResponse.detail().tokenfilters()[0].getTokens()[5];
    assertThat(token.getTerm(), equalTo("troubl"));
    assertThat(token.getPosition(), equalTo(105));
    assertThat(token.getStartOffset(), equalTo(20));
    assertThat(token.getEndOffset(), equalTo(28));
    assertThat(token.getPositionLength(), equalTo(1));
}
Also used : Alias(org.elasticsearch.action.admin.indices.alias.Alias) AnalyzeResponse(org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse)

Example 10 with AnalyzeResponse

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

the class AnalyzeActionIT method testAnalyzeKeywordField.

public void testAnalyzeKeywordField() throws IOException {
    assertAcked(prepareCreate("test").addAlias(new Alias("alias")).addMapping("test", "keyword", "type=keyword"));
    ensureGreen("test");
    AnalyzeResponse analyzeResponse = client().admin().indices().prepareAnalyze(indexOrAlias(), "ABC").setField("keyword").get();
    assertThat(analyzeResponse.getTokens().size(), equalTo(1));
    AnalyzeResponse.AnalyzeToken token = analyzeResponse.getTokens().get(0);
    assertThat(token.getTerm(), equalTo("ABC"));
    assertThat(token.getStartOffset(), equalTo(0));
    assertThat(token.getEndOffset(), equalTo(3));
    assertThat(token.getPosition(), equalTo(0));
    assertThat(token.getPositionLength(), equalTo(1));
}
Also used : Alias(org.elasticsearch.action.admin.indices.alias.Alias) 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