1111import org .apache .lucene .index .LeafReaderContext ;
1212import org .apache .lucene .index .OrdinalMap ;
1313import org .apache .lucene .index .Term ;
14- import org .apache .lucene .search .AutomatonQuery ;
1514import org .apache .lucene .search .MultiTermQuery ;
1615import org .apache .lucene .search .PrefixQuery ;
1716import org .apache .lucene .search .Query ;
1817import org .apache .lucene .search .SortField ;
19- import org .apache .lucene .search .TermInSetQuery ;
20- import org .apache .lucene .search .TermQuery ;
21- import org .apache .lucene .search .TermRangeQuery ;
2218import org .apache .lucene .util .BytesRef ;
23- import org .elasticsearch .ElasticsearchException ;
2419import org .elasticsearch .common .lucene .Lucene ;
2520import org .elasticsearch .common .lucene .search .AutomatonQueries ;
2621import org .elasticsearch .common .unit .Fuzziness ;
5853
5954import java .io .IOException ;
6055import java .util .Arrays ;
61- import java .util .Collection ;
6256import java .util .List ;
6357import java .util .Map ;
6458import java .util .function .Supplier ;
6559
66- import static org .elasticsearch .search .SearchService .ALLOW_EXPENSIVE_QUERIES ;
67-
6860/**
6961 * A field mapper that accepts a JSON object and flattens it into a single field. This data type
7062 * can be a useful alternative to an 'object' mapping when the object has a large, unknown set
@@ -170,19 +162,17 @@ public FlattenedFieldMapper build(ContentPath contentPath) {
170162 */
171163 public static final class KeyedFlattenedFieldType extends StringFieldType {
172164 private final String key ;
173- private final String root ;
174165
175- public KeyedFlattenedFieldType (String root , boolean indexed , boolean hasDocValues , String key ,
166+ public KeyedFlattenedFieldType (String name , boolean indexed , boolean hasDocValues , String key ,
176167 boolean splitQueriesOnWhitespace , Map <String , String > meta ) {
177- super (root + "." + key , indexed , false , hasDocValues ,
168+ super (name , indexed , false , hasDocValues ,
178169 splitQueriesOnWhitespace ? TextSearchInfo .WHITESPACE_MATCH_ONLY : TextSearchInfo .SIMPLE_MATCH_ONLY ,
179170 meta );
180171 this .key = key ;
181- this .root = root ;
182172 }
183173
184- private KeyedFlattenedFieldType (String root , String key , RootFlattenedFieldType ref ) {
185- this (root , ref .isSearchable (), ref .hasDocValues (), key , ref .splitQueriesOnWhitespace , ref .meta ());
174+ private KeyedFlattenedFieldType (String name , String key , RootFlattenedFieldType ref ) {
175+ this (name , ref .isSearchable (), ref .hasDocValues (), key , ref .splitQueriesOnWhitespace , ref .meta ());
186176 }
187177
188178 @ Override
@@ -196,7 +186,7 @@ public String key() {
196186
197187 @ Override
198188 public Query existsQuery (SearchExecutionContext context ) {
199- Term term = new Term (root , FlattenedFieldParser .createKeyedValue (key , "" ));
189+ Term term = new Term (name () , FlattenedFieldParser .createKeyedValue (key , "" ));
200190 return new PrefixQuery (term );
201191 }
202192
@@ -215,15 +205,8 @@ public Query rangeQuery(Object lowerTerm,
215205 "] fields must include both an upper and a lower bound." );
216206 }
217207
218- if (context .allowExpensiveQueries () == false ) {
219- throw new ElasticsearchException ("[range] queries on [text] or [keyword] fields cannot be executed when '" +
220- ALLOW_EXPENSIVE_QUERIES .getKey () + "' is set to false." );
221- }
222- failIfNotIndexed ();
223- return new TermRangeQuery (root ,
224- indexedValueForSearch (lowerTerm ),
225- indexedValueForSearch (upperTerm ),
226- includeLower , includeUpper );
208+ return super .rangeQuery (lowerTerm , upperTerm ,
209+ includeLower , includeUpper , context );
227210 }
228211
229212 @ Override
@@ -251,51 +234,7 @@ public Query wildcardQuery(String value,
251234
252235 @ Override
253236 public Query termQueryCaseInsensitive (Object value , SearchExecutionContext context ) {
254- return AutomatonQueries .caseInsensitiveTermQuery (new Term (root , indexedValueForSearch (value )));
255- }
256-
257- @ Override
258- public Query termQuery (Object value , SearchExecutionContext context ) {
259- failIfNotIndexed ();
260- return new TermQuery (new Term (root , indexedValueForSearch (value )));
261- }
262-
263- @ Override
264- public Query termsQuery (Collection <?> values , SearchExecutionContext context ) {
265- failIfNotIndexed ();
266- BytesRef [] bytesRefs = values .stream ().map (this ::indexedValueForSearch ).toArray (BytesRef []::new );
267- return new TermInSetQuery (root , bytesRefs );
268- }
269-
270- @ Override
271- public Query prefixQuery (String value , MultiTermQuery .RewriteMethod method , boolean caseInsensitive , SearchExecutionContext context ) {
272- if (context .allowExpensiveQueries () == false ) {
273- throw new ElasticsearchException ("[prefix] queries cannot be executed when '" +
274- ALLOW_EXPENSIVE_QUERIES .getKey () + "' is set to false. For optimised prefix queries on text " +
275- "fields please enable [index_prefixes]." );
276- }
277- failIfNotIndexed ();
278- if (caseInsensitive ) {
279- AutomatonQuery query = AutomatonQueries .caseInsensitivePrefixQuery ((new Term (root , indexedValueForSearch (value ))));
280- if (method != null ) {
281- query .setRewriteMethod (method );
282- }
283- return query ;
284-
285- }
286- PrefixQuery query = new PrefixQuery (new Term (root , indexedValueForSearch (value )));
287- if (method != null ) {
288- query .setRewriteMethod (method );
289- }
290- return query ;
291- }
292-
293- @ Override
294- protected void failIfNotIndexed () {
295- if (isIndexed == false ) {
296- // we throw an IAE rather than an ISE so that it translates to a 4xx code rather than 5xx code on the http layer
297- throw new IllegalArgumentException ("Cannot search on field [" + root + "] since it is not indexed." );
298- }
237+ return AutomatonQueries .caseInsensitiveTermQuery (new Term (name (), indexedValueForSearch (value )));
299238 }
300239
301240 @ Override
@@ -314,7 +253,7 @@ public BytesRef indexedValueForSearch(Object value) {
314253 @ Override
315254 public IndexFieldData .Builder fielddataBuilder (String fullyQualifiedIndexName , Supplier <SearchLookup > searchLookup ) {
316255 failIfNoDocValues ();
317- return new KeyedFlattenedFieldData .Builder (root + "._keyed" , key , CoreValuesSourceType .KEYWORD );
256+ return new KeyedFlattenedFieldData .Builder (name () , key , CoreValuesSourceType .KEYWORD );
318257 }
319258
320259 @ Override
@@ -501,7 +440,7 @@ public RootFlattenedFieldType fieldType() {
501440
502441 @ Override
503442 public KeyedFlattenedFieldType keyedFieldType (String key ) {
504- return new KeyedFlattenedFieldType (name (), key , fieldType ());
443+ return new KeyedFlattenedFieldType (keyedFieldName (), key , fieldType ());
505444 }
506445
507446 public String keyedFieldName () {
0 commit comments