Merge pull request #23161 from johtani/support_keyword_to_analyze_api
[Analyze]Support Keyword type in Analyze API
This commit is contained in:
commit
12bbe6e660
|
@ -53,6 +53,7 @@ import org.elasticsearch.index.analysis.NamedAnalyzer;
|
|||
import org.elasticsearch.index.analysis.TokenFilterFactory;
|
||||
import org.elasticsearch.index.analysis.TokenizerFactory;
|
||||
import org.elasticsearch.index.mapper.AllFieldMapper;
|
||||
import org.elasticsearch.index.mapper.KeywordFieldMapper;
|
||||
import org.elasticsearch.index.mapper.MappedFieldType;
|
||||
import org.elasticsearch.index.shard.ShardId;
|
||||
import org.elasticsearch.indices.IndicesService;
|
||||
|
@ -131,10 +132,17 @@ public class TransportAnalyzeAction extends TransportSingleShardAction<AnalyzeRe
|
|||
}
|
||||
MappedFieldType fieldType = indexService.mapperService().fullName(request.field());
|
||||
if (fieldType != null) {
|
||||
if (fieldType.tokenized() == false) {
|
||||
if (fieldType.tokenized()) {
|
||||
analyzer = fieldType.indexAnalyzer();
|
||||
} else if (fieldType instanceof KeywordFieldMapper.KeywordFieldType) {
|
||||
analyzer = ((KeywordFieldMapper.KeywordFieldType) fieldType).normalizer();
|
||||
if (analyzer == null) {
|
||||
// this will be KeywordAnalyzer
|
||||
analyzer = fieldType.indexAnalyzer();
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("Can't process field [" + request.field() + "], Analysis requests are only supported on tokenized fields");
|
||||
}
|
||||
analyzer = fieldType.indexAnalyzer();
|
||||
field = fieldType.name();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -639,4 +639,36 @@ public class AnalyzeActionIT extends ESIntegTestCase {
|
|||
assertThat(analyzeResponse.detail().tokenizer().getTokens()[2].getPositionLength(), equalTo(1));
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
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));
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue