diff --git a/plugin/src/main/java/org/elasticsearch/xpack/security/authz/accesscontrol/FieldSubsetReader.java b/plugin/src/main/java/org/elasticsearch/xpack/security/authz/accesscontrol/FieldSubsetReader.java index c78e898be6f..46a78bcb346 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/security/authz/accesscontrol/FieldSubsetReader.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/security/authz/accesscontrol/FieldSubsetReader.java @@ -267,8 +267,8 @@ public final class FieldSubsetReader extends FilterLeafReader { } @Override - public Fields fields() throws IOException { - return new FieldFilterFields(super.fields()); + public Terms terms(String field) throws IOException { + return wrapTerms(super.terms(field), field); } @Override @@ -344,24 +344,27 @@ public final class FieldSubsetReader extends FilterLeafReader { @Override public Terms terms(String field) throws IOException { - if (!hasField(field)) { - return null; - } else if (FieldNamesFieldMapper.NAME.equals(field)) { - // for the _field_names field, fields for the document - // are encoded as postings, where term is the field. - // so we hide terms for fields we filter out. - Terms terms = super.terms(field); - if (terms != null) { - // check for null, in case term dictionary is not a ghostbuster - // So just because its in fieldinfos and "indexed=true" doesn't mean you can go grab a Terms for it. - // It just means at one point there was a document with that field indexed... - // The fields infos isn't updates/removed even if no docs refer to it - terms = new FieldNamesTerms(terms); - } - return terms; - } else { - return super.terms(field); + return wrapTerms(super.terms(field), field); + } + } + + private Terms wrapTerms(Terms terms, String field) { + if (!hasField(field)) { + return null; + } else if (FieldNamesFieldMapper.NAME.equals(field)) { + // for the _field_names field, fields for the document + // are encoded as postings, where term is the field. + // so we hide terms for fields we filter out. + if (terms != null) { + // check for null, in case term dictionary is not a ghostbuster + // So just because its in fieldinfos and "indexed=true" doesn't mean you can go grab a Terms for it. + // It just means at one point there was a document with that field indexed... + // The fields infos isn't updates/removed even if no docs refer to it + terms = new FieldNamesTerms(terms); } + return terms; + } else { + return terms; } } diff --git a/plugin/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/DocumentSubsetReaderTests.java b/plugin/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/DocumentSubsetReaderTests.java index e666618e415..80cdbf8bcf8 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/DocumentSubsetReaderTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/DocumentSubsetReaderTests.java @@ -100,14 +100,14 @@ public class DocumentSubsetReaderTests extends ESTestCase { new TermQuery(new Term("field", "value1")))); assertThat(indexSearcher.getIndexReader().numDocs(), equalTo(1)); TopDocs result = indexSearcher.search(new MatchAllDocsQuery(), 1); - assertThat(result.totalHits, equalTo(1)); + assertThat(result.totalHits, equalTo(1L)); assertThat(result.scoreDocs[0].doc, equalTo(0)); indexSearcher = new IndexSearcher(DocumentSubsetReader.wrap(directoryReader, bitsetFilterCache, new TermQuery(new Term("field", "value2")))); assertThat(indexSearcher.getIndexReader().numDocs(), equalTo(1)); result = indexSearcher.search(new MatchAllDocsQuery(), 1); - assertThat(result.totalHits, equalTo(1)); + assertThat(result.totalHits, equalTo(1L)); assertThat(result.scoreDocs[0].doc, equalTo(1)); // this doc has been marked as deleted: @@ -115,13 +115,13 @@ public class DocumentSubsetReaderTests extends ESTestCase { new TermQuery(new Term("field", "value3")))); assertThat(indexSearcher.getIndexReader().numDocs(), equalTo(0)); result = indexSearcher.search(new MatchAllDocsQuery(), 1); - assertThat(result.totalHits, equalTo(0)); + assertThat(result.totalHits, equalTo(0L)); indexSearcher = new IndexSearcher(DocumentSubsetReader.wrap(directoryReader, bitsetFilterCache, new TermQuery(new Term("field", "value4")))); assertThat(indexSearcher.getIndexReader().numDocs(), equalTo(1)); result = indexSearcher.search(new MatchAllDocsQuery(), 1); - assertThat(result.totalHits, equalTo(1)); + assertThat(result.totalHits, equalTo(1L)); assertThat(result.scoreDocs[0].doc, equalTo(3)); } diff --git a/plugin/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/FieldSubsetReaderTests.java b/plugin/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/FieldSubsetReaderTests.java index 03e104f0e32..a007a4dc73b 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/FieldSubsetReaderTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/FieldSubsetReaderTests.java @@ -20,6 +20,7 @@ import org.apache.lucene.document.StringField; import org.apache.lucene.document.TextField; import org.apache.lucene.index.BinaryDocValues; import org.apache.lucene.index.DirectoryReader; +import org.apache.lucene.index.FieldInfo; import org.apache.lucene.index.FieldInfos; import org.apache.lucene.index.Fields; import org.apache.lucene.index.IndexWriter; @@ -87,8 +88,8 @@ public class FieldSubsetReaderTests extends ESTestCase { // see only one field LeafReader segmentReader = ir.leaves().get(0).reader(); Set seenFields = new HashSet<>(); - for (String field : segmentReader.fields()) { - seenFields.add(field); + for (FieldInfo info : segmentReader.getFieldInfos()) { + seenFields.add(info.name); } assertEquals(Collections.singleton("fieldA"), seenFields); assertNotNull(segmentReader.terms("fieldA")); @@ -937,13 +938,12 @@ public class FieldSubsetReaderTests extends ESTestCase { // see no fields LeafReader segmentReader = ir.leaves().get(0).reader(); - Fields f = segmentReader.fields(); - assertNotNull(f); // 5.x contract Set seenFields = new HashSet<>(); - for (String field : segmentReader.fields()) { - seenFields.add(field); + for (FieldInfo info : segmentReader.getFieldInfos()) { + seenFields.add(info.name); } assertEquals(0, seenFields.size()); + assertNull(segmentReader.terms("foo")); // see no vectors assertNull(segmentReader.getTermVectors(0));