Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jul 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}
}