Authz FieldExtractor support for TermInSet query (elastic/x-pack-elasticsearch#2539)

FieldExtractor for TermInSet query.

Closes: elastic/x-pack-elasticsearch#1104

Original commit: elastic/x-pack-elasticsearch@7a46361c86
This commit is contained in:
Albert Zaharovits 2017-09-21 11:35:34 +03:00 committed by GitHub
parent 6b51f5e6ca
commit a8ea4f2d90
2 changed files with 18 additions and 0 deletions

View File

@ -5,6 +5,7 @@
*/
package org.elasticsearch.xpack.security.authz.accesscontrol;
import org.apache.lucene.index.PrefixCodedTerms.TermIterator;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.DisjunctionMaxQuery;
@ -19,6 +20,7 @@ import org.apache.lucene.search.PointInSetQuery;
import org.apache.lucene.search.PointRangeQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.SynonymQuery;
import org.apache.lucene.search.TermInSetQuery;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.Weight;
import org.apache.lucene.search.spans.SpanTermQuery;
@ -88,6 +90,14 @@ class FieldExtractor {
} catch (UnsupportedOperationException e) {
extractFields(((IndexOrDocValuesQuery) query).getIndexQuery(), fields);
}
} else if (query instanceof TermInSetQuery) {
// TermInSetQuery#field is inaccessible
TermInSetQuery termInSetQuery = (TermInSetQuery) query;
TermIterator termIterator = termInSetQuery.getTermData().iterator();
// there should only be one field
if (termIterator.next() != null) {
fields.add(termIterator.field());
}
} else if (query instanceof MatchAllDocsQuery) {
// no field
} else if (query instanceof MatchNoDocsQuery) {

View File

@ -21,8 +21,10 @@ import org.apache.lucene.search.MultiPhraseQuery;
import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.SynonymQuery;
import org.apache.lucene.search.TermInSetQuery;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.spans.SpanTermQuery;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.test.ESTestCase;
import java.util.Arrays;
@ -113,6 +115,12 @@ public class FieldExtractorTests extends ESTestCase {
assertEquals(asSet("foo"), fields);
}
public void testTermInSet() {
Set<String> fields = new HashSet<>();
FieldExtractor.extractFields(new TermInSetQuery("foo", new BytesRef("baz"), new BytesRef("baz2")), fields);
assertEquals(asSet("foo"), fields);
}
public void testMatchAllDocs() {
Set<String> fields = new HashSet<>();
FieldExtractor.extractFields(new MatchAllDocsQuery(), fields);