Relax TermVectors API to work with textual fields other than TextFieldType (#31915)

This changes the field-eligibility test to check one level up in the class hierarchy to allow any subclasses of StringFieldType.
Closes #31902
This commit is contained in:
markharwood 2018-07-17 13:11:10 +01:00 committed by GitHub
parent b43fe560a4
commit a7e477126f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 5 deletions

View File

@ -48,7 +48,7 @@ import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.ParseContext;
import org.elasticsearch.index.mapper.ParsedDocument;
import org.elasticsearch.index.mapper.SourceFieldMapper;
import org.elasticsearch.index.mapper.TextFieldMapper;
import org.elasticsearch.index.mapper.StringFieldType;
import org.elasticsearch.index.shard.IndexShard;
import org.elasticsearch.search.dfs.AggregatedDfs;
@ -162,8 +162,7 @@ public class TermVectorsService {
private static boolean isValidField(MappedFieldType fieldType) {
// must be a string
if (fieldType instanceof KeywordFieldMapper.KeywordFieldType == false
&& fieldType instanceof TextFieldMapper.TextFieldType == false) {
if (fieldType instanceof StringFieldType == false) {
return false;
}
// and must be indexed

View File

@ -109,12 +109,62 @@ public class TermVectorsServiceTests extends ESSingleNodeTestCase {
IndexService test = indicesService.indexService(resolveIndex("test"));
IndexShard shard = test.getShardOrNull(0);
assertThat(shard, notNullValue());
TermVectorsResponse response = TermVectorsService.getTermVectors(shard, request);
TermVectorsResponse response = TermVectorsService.getTermVectors(shard, request);
assertEquals(1, response.getFields().size());
Terms terms = response.getFields().terms("text");
TermsEnum iterator = terms.iterator();
while (iterator.next() != null) {
assertEquals(max, iterator.docFreq());
}
}
}
public void testWithIndexedPhrases() throws IOException {
XContentBuilder mapping = jsonBuilder()
.startObject()
.startObject("_doc")
.startObject("properties")
.startObject("text")
.field("type", "text")
.field("index_phrases", true)
.field("term_vector", "with_positions_offsets_payloads")
.endObject()
.endObject()
.endObject()
.endObject();
Settings settings = Settings.builder()
.put("number_of_shards", 1)
.build();
createIndex("test", settings, "_doc", mapping);
ensureGreen();
int max = between(3, 10);
BulkRequestBuilder bulk = client().prepareBulk();
for (int i = 0; i < max; i++) {
bulk.add(client().prepareIndex("test", "_doc", Integer.toString(i))
.setSource("text", "the quick brown fox jumped over the lazy dog"));
}
bulk.get();
TermVectorsRequest request = new TermVectorsRequest("test", "_doc", "0").termStatistics(true);
IndicesService indicesService = getInstanceFromNode(IndicesService.class);
IndexService test = indicesService.indexService(resolveIndex("test"));
IndexShard shard = test.getShardOrNull(0);
assertThat(shard, notNullValue());
TermVectorsResponse response = TermVectorsService.getTermVectors(shard, request);
assertEquals(2, response.getFields().size());
Terms terms = response.getFields().terms("text");
TermsEnum iterator = terms.iterator();
while (iterator.next() != null) {
assertEquals(max, iterator.docFreq());
}
Terms phrases = response.getFields().terms("text._index_phrase");
TermsEnum phraseIterator = phrases.iterator();
while (phraseIterator.next() != null) {
assertEquals(max, phraseIterator.docFreq());
}
}
}