Skip to content

Commit

Permalink
Relax TermVectors API to work with textual fields other than TextFiel…
Browse files Browse the repository at this point in the history
…dType

Closes #31902
  • Loading branch information
markharwood committed Jul 17, 2018
1 parent b655c11 commit 8e5b068
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
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;

Expand Down Expand Up @@ -162,8 +162,7 @@ private static void handleFieldWildcards(IndexShard indexShard, TermVectorsReque

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,62 @@ public void testDocFreqs() throws IOException {
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());
}
}
}

0 comments on commit 8e5b068

Please sign in to comment.