Search in sources :

Example 1 with AnalyzeRequest

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

the class TransportAnalyzeActionTests method testWithIndexAnalyzers.

public void testWithIndexAnalyzers() throws IOException {
    AnalyzeRequest request = new AnalyzeRequest();
    request.analyzer("standard");
    request.text("the quick brown fox");
    request.analyzer("custom_analyzer");
    request.text("the qu1ck brown fox");
    AnalyzeResponse analyze = TransportAnalyzeAction.analyze(request, AllFieldMapper.NAME, null, indexAnalyzers, registry, environment);
    List<AnalyzeResponse.AnalyzeToken> tokens = analyze.getTokens();
    assertEquals(4, tokens.size());
    request.analyzer("whitespace");
    request.text("the qu1ck brown fox-dog");
    analyze = TransportAnalyzeAction.analyze(request, AllFieldMapper.NAME, null, indexAnalyzers, registry, environment);
    tokens = analyze.getTokens();
    assertEquals(4, tokens.size());
    request.analyzer("custom_analyzer");
    request.text("the qu1ck brown fox-dog");
    analyze = TransportAnalyzeAction.analyze(request, AllFieldMapper.NAME, null, indexAnalyzers, registry, environment);
    tokens = analyze.getTokens();
    assertEquals(5, tokens.size());
    request.analyzer(null);
    request.tokenizer("whitespace");
    request.addTokenFilter("lowercase");
    request.addTokenFilter("wordDelimiter");
    request.text("the qu1ck brown fox-dog");
    analyze = TransportAnalyzeAction.analyze(request, AllFieldMapper.NAME, null, indexAnalyzers, registry, environment);
    tokens = analyze.getTokens();
    assertEquals(5, tokens.size());
    assertEquals("the", tokens.get(0).getTerm());
    assertEquals("qu1ck", tokens.get(1).getTerm());
    assertEquals("brown", tokens.get(2).getTerm());
    assertEquals("fox", tokens.get(3).getTerm());
    assertEquals("dog", tokens.get(4).getTerm());
    request.analyzer(null);
    request.tokenizer("trigram");
    request.addTokenFilter("synonym");
    request.text("kimchy");
    analyze = TransportAnalyzeAction.analyze(request, AllFieldMapper.NAME, null, indexAnalyzers, registry, environment);
    tokens = analyze.getTokens();
    assertEquals(2, tokens.size());
    assertEquals("sha", tokens.get(0).getTerm());
    assertEquals("hay", tokens.get(1).getTerm());
}
Also used : AnalyzeRequest(org.elasticsearch.action.admin.indices.analyze.AnalyzeRequest) AnalyzeResponse(org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse)

Example 2 with AnalyzeRequest

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

the class TransportAnalyzeActionTests method testNoIndexAnalyzers.

public void testNoIndexAnalyzers() throws IOException {
    AnalyzeRequest request = new AnalyzeRequest();
    request.analyzer("standard");
    request.text("the quick brown fox");
    AnalyzeResponse analyze = TransportAnalyzeAction.analyze(request, AllFieldMapper.NAME, null, null, registry, environment);
    List<AnalyzeResponse.AnalyzeToken> tokens = analyze.getTokens();
    assertEquals(4, tokens.size());
    request.analyzer(null);
    request.tokenizer("whitespace");
    request.addTokenFilter("lowercase");
    request.addTokenFilter("word_delimiter");
    request.text("the qu1ck brown fox");
    analyze = TransportAnalyzeAction.analyze(request, AllFieldMapper.NAME, null, randomBoolean() ? indexAnalyzers : null, registry, environment);
    tokens = analyze.getTokens();
    assertEquals(6, tokens.size());
    assertEquals("qu", tokens.get(1).getTerm());
    assertEquals("1", tokens.get(2).getTerm());
    assertEquals("ck", tokens.get(3).getTerm());
    request.analyzer(null);
    request.tokenizer("whitespace");
    request.addCharFilter("html_strip");
    request.addTokenFilter("lowercase");
    request.addTokenFilter("word_delimiter");
    request.text("<p>the qu1ck brown fox</p>");
    analyze = TransportAnalyzeAction.analyze(request, AllFieldMapper.NAME, null, randomBoolean() ? indexAnalyzers : null, registry, environment);
    tokens = analyze.getTokens();
    assertEquals(6, tokens.size());
    assertEquals("the", tokens.get(0).getTerm());
    assertEquals("qu", tokens.get(1).getTerm());
    assertEquals("1", tokens.get(2).getTerm());
    assertEquals("ck", tokens.get(3).getTerm());
    assertEquals("brown", tokens.get(4).getTerm());
    assertEquals("fox", tokens.get(5).getTerm());
}
Also used : AnalyzeRequest(org.elasticsearch.action.admin.indices.analyze.AnalyzeRequest) AnalyzeResponse(org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse)

Example 3 with AnalyzeRequest

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

the class RestAnalyzeActionTests method testParseXContentForAnalyzeRequest.

public void testParseXContentForAnalyzeRequest() throws Exception {
    XContentParser content = createParser(XContentFactory.jsonBuilder().startObject().field("text", "THIS IS A TEST").field("tokenizer", "keyword").array("filter", "lowercase").endObject());
    AnalyzeRequest analyzeRequest = new AnalyzeRequest("for test");
    RestAnalyzeAction.buildFromContent(content, analyzeRequest);
    assertThat(analyzeRequest.text().length, equalTo(1));
    assertThat(analyzeRequest.text(), equalTo(new String[] { "THIS IS A TEST" }));
    assertThat(analyzeRequest.tokenizer().name, equalTo("keyword"));
    assertThat(analyzeRequest.tokenFilters().size(), equalTo(1));
    for (AnalyzeRequest.NameOrDefinition filter : analyzeRequest.tokenFilters()) {
        assertThat(filter.name, equalTo("lowercase"));
    }
}
Also used : AnalyzeRequest(org.elasticsearch.action.admin.indices.analyze.AnalyzeRequest) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 4 with AnalyzeRequest

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

the class TransportAnalyzeActionTests method testUnknown.

public void testUnknown() throws IOException {
    boolean notGlobal = randomBoolean();
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> TransportAnalyzeAction.analyze(new AnalyzeRequest().analyzer("foobar").text("the qu1ck brown fox"), AllFieldMapper.NAME, null, notGlobal ? indexAnalyzers : null, registry, environment));
    if (notGlobal) {
        assertEquals(e.getMessage(), "failed to find analyzer [foobar]");
    } else {
        assertEquals(e.getMessage(), "failed to find global analyzer [foobar]");
    }
    e = expectThrows(IllegalArgumentException.class, () -> TransportAnalyzeAction.analyze(new AnalyzeRequest().tokenizer("foobar").text("the qu1ck brown fox"), AllFieldMapper.NAME, null, notGlobal ? indexAnalyzers : null, registry, environment));
    if (notGlobal) {
        assertEquals(e.getMessage(), "failed to find tokenizer under [foobar]");
    } else {
        assertEquals(e.getMessage(), "failed to find global tokenizer under [foobar]");
    }
    e = expectThrows(IllegalArgumentException.class, () -> TransportAnalyzeAction.analyze(new AnalyzeRequest().tokenizer("whitespace").addTokenFilter("foobar").text("the qu1ck brown fox"), AllFieldMapper.NAME, null, notGlobal ? indexAnalyzers : null, registry, environment));
    if (notGlobal) {
        assertEquals(e.getMessage(), "failed to find token filter under [foobar]");
    } else {
        assertEquals(e.getMessage(), "failed to find global token filter under [foobar]");
    }
    e = expectThrows(IllegalArgumentException.class, () -> TransportAnalyzeAction.analyze(new AnalyzeRequest().tokenizer("whitespace").addTokenFilter("lowercase").addCharFilter("foobar").text("the qu1ck brown fox"), AllFieldMapper.NAME, null, notGlobal ? indexAnalyzers : null, registry, environment));
    if (notGlobal) {
        assertEquals(e.getMessage(), "failed to find char filter under [foobar]");
    } else {
        assertEquals(e.getMessage(), "failed to find global char filter under [foobar]");
    }
}
Also used : AnalyzeRequest(org.elasticsearch.action.admin.indices.analyze.AnalyzeRequest)

Example 5 with AnalyzeRequest

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

the class TransportAnalyzeActionTests method testNonPreBuildTokenFilter.

public void testNonPreBuildTokenFilter() throws IOException {
    AnalyzeRequest request = new AnalyzeRequest();
    request.tokenizer("whitespace");
    request.addTokenFilter("min_hash");
    request.text("the quick brown fox");
    AnalyzeResponse analyze = TransportAnalyzeAction.analyze(request, AllFieldMapper.NAME, null, indexAnalyzers, registry, environment);
    List<AnalyzeResponse.AnalyzeToken> tokens = analyze.getTokens();
    int default_hash_count = 1;
    int default_bucket_size = 512;
    int default_hash_set_size = 1;
    assertEquals(default_hash_count * default_bucket_size * default_hash_set_size, tokens.size());
}
Also used : AnalyzeRequest(org.elasticsearch.action.admin.indices.analyze.AnalyzeRequest) AnalyzeResponse(org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse)

Aggregations

AnalyzeRequest (org.elasticsearch.action.admin.indices.analyze.AnalyzeRequest)10 AnalyzeResponse (org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse)4 XContentParser (org.elasticsearch.common.xcontent.XContentParser)4