Browse Source

Reject regexp queries on the _index field. (#46945)

We speculatively added support for `regexp` queries on the `_index` field in
#34089 (this functionality was not actually requested by a user). Supporting
regex logic adds complexity to the `_index` field for not much gain, so we
would like to remove it.

From an end-to-end test it turns out this functionality never even worked in
the first place because of an error in how regex flags were interpreted! For
this reason, we can remove support for `regexp` on `_index` without a
deprecation period.

Relates to #46640.
Julie Tibshirani 6 years ago
parent
commit
b6454e978e

+ 0 - 16
server/src/main/java/org/elasticsearch/index/mapper/IndexFieldMapper.java

@@ -28,7 +28,6 @@ import org.apache.lucene.util.BytesRef;
 import org.elasticsearch.common.Nullable;
 import org.elasticsearch.common.lucene.Lucene;
 import org.elasticsearch.common.lucene.search.Queries;
-import org.elasticsearch.common.regex.Regex;
 import org.elasticsearch.common.settings.Settings;
 import org.elasticsearch.common.xcontent.XContentBuilder;
 import org.elasticsearch.index.fielddata.IndexFieldData;
@@ -38,7 +37,6 @@ import org.elasticsearch.index.query.QueryShardContext;
 import java.io.IOException;
 import java.util.List;
 import java.util.Map;
-import java.util.regex.Pattern;
 
 
 public class IndexFieldMapper extends MetadataFieldMapper {
@@ -175,20 +173,6 @@ public class IndexFieldMapper extends MetadataFieldMapper {
             }
         }
 
-        @Override
-        public Query regexpQuery(String value, int flags, int maxDeterminizedStates,
-                                 MultiTermQuery.RewriteMethod method, QueryShardContext context) {
-            String indexName = context.getFullyQualifiedIndex().getName();
-            Pattern pattern = Regex.compile(value, Regex.flagsToString(flags));
-
-            if (pattern.matcher(indexName).matches()) {
-                return Queries.newMatchAllQuery();
-            } else {
-                return Queries.newMatchNoDocsQuery("The index [" + context.getFullyQualifiedIndex().getName()
-                    + "] doesn't match the provided pattern [" + value + "].");
-            }
-        }
-
         @Override
         public Query wildcardQuery(String value,
                                    @Nullable MultiTermQuery.RewriteMethod method,

+ 10 - 6
server/src/test/java/org/elasticsearch/index/mapper/IndexFieldTypeTests.java

@@ -27,9 +27,12 @@ import org.elasticsearch.common.regex.Regex;
 import org.elasticsearch.common.settings.Settings;
 import org.elasticsearch.index.IndexSettings;
 import org.elasticsearch.index.query.QueryShardContext;
+import org.elasticsearch.index.query.QueryShardException;
 
 import java.util.function.Predicate;
 
+import static org.hamcrest.Matchers.containsString;
+
 public class IndexFieldTypeTests extends FieldTypeTestCase {
 
     @Override
@@ -46,22 +49,23 @@ public class IndexFieldTypeTests extends FieldTypeTestCase {
         assertEquals(new MatchNoDocsQuery(), ft.prefixQuery("other_ind", null, createContext()));
     }
 
-    public void testRegexpQuery() {
+    public void testWildcardQuery() {
         MappedFieldType ft = createDefaultFieldType();
         ft.setName("field");
         ft.setIndexOptions(IndexOptions.DOCS);
 
-        assertEquals(new MatchAllDocsQuery(), ft.regexpQuery("ind.x", 0, 10, null, createContext()));
-        assertEquals(new MatchNoDocsQuery(), ft.regexpQuery("ind?x", 0, 10, null, createContext()));
+        assertEquals(new MatchAllDocsQuery(), ft.wildcardQuery("ind*x", null, createContext()));
+        assertEquals(new MatchNoDocsQuery(), ft.wildcardQuery("other_ind*x", null, createContext()));
     }
 
-    public void testWildcardQuery() {
+    public void testRegexpQuery() {
         MappedFieldType ft = createDefaultFieldType();
         ft.setName("field");
         ft.setIndexOptions(IndexOptions.DOCS);
 
-        assertEquals(new MatchAllDocsQuery(), ft.wildcardQuery("ind*x", null, createContext()));
-        assertEquals(new MatchNoDocsQuery(), ft.wildcardQuery("other_ind*x", null, createContext()));
+        QueryShardException e = expectThrows(QueryShardException.class, () ->
+            assertEquals(new MatchAllDocsQuery(), ft.regexpQuery("ind.x", 0, 10, null, createContext())));
+        assertThat(e.getMessage(), containsString("Can only use regexp queries on keyword and text fields"));
     }
 
     private QueryShardContext createContext() {