@@ -312,6 +312,7 @@ public static class KeywordFieldType extends StringFieldType {
312312 private final int ignoreAbove ;
313313 private final String nullValue ;
314314 private final boolean useSimilarity ;
315+ private final boolean splitQueriesOnWhitespace ;
315316
316317 public KeywordFieldType (String name , FieldType fieldType , NamedAnalyzer normalizer , NamedAnalyzer searchAnalyzer , Builder builder ) {
317318 super (
@@ -328,18 +329,31 @@ public KeywordFieldType(String name, FieldType fieldType, NamedAnalyzer normaliz
328329 this .ignoreAbove = builder .ignoreAbove .getValue ();
329330 this .nullValue = builder .nullValue .getValue ();
330331 this .useSimilarity = builder .useSimilarity .getValue ();
332+ this .splitQueriesOnWhitespace = builder .splitQueriesOnWhitespace .getValue ();
331333 }
332334
333335 public KeywordFieldType (String name , boolean isSearchable , boolean hasDocValues , Map <String , String > meta ) {
334336 this (name , isSearchable , hasDocValues , false , meta );
335337 }
336338
337339 public KeywordFieldType (String name , boolean isSearchable , boolean hasDocValues , boolean useSimilarity , Map <String , String > meta ) {
340+ this (name , isSearchable , hasDocValues , useSimilarity , false , meta );
341+ }
342+
343+ public KeywordFieldType (
344+ String name ,
345+ boolean isSearchable ,
346+ boolean hasDocValues ,
347+ boolean useSimilarity ,
348+ boolean splitQueriesOnWhitespace ,
349+ Map <String , String > meta
350+ ) {
338351 super (name , isSearchable , false , hasDocValues , TextSearchInfo .SIMPLE_MATCH_ONLY , meta );
339352 setIndexAnalyzer (Lucene .KEYWORD_ANALYZER );
340353 this .ignoreAbove = Integer .MAX_VALUE ;
341354 this .nullValue = null ;
342355 this .useSimilarity = useSimilarity ;
356+ this .splitQueriesOnWhitespace = splitQueriesOnWhitespace ;
343357 }
344358
345359 public KeywordFieldType (String name ) {
@@ -358,13 +372,15 @@ public KeywordFieldType(String name, FieldType fieldType) {
358372 this .ignoreAbove = Integer .MAX_VALUE ;
359373 this .nullValue = null ;
360374 this .useSimilarity = false ;
375+ this .splitQueriesOnWhitespace = false ;
361376 }
362377
363378 public KeywordFieldType (String name , NamedAnalyzer analyzer ) {
364379 super (name , true , false , true , new TextSearchInfo (Defaults .FIELD_TYPE , null , analyzer , analyzer ), Collections .emptyMap ());
365380 this .ignoreAbove = Integer .MAX_VALUE ;
366381 this .nullValue = null ;
367382 this .useSimilarity = false ;
383+ this .splitQueriesOnWhitespace = false ;
368384 }
369385
370386 @ Override
@@ -447,6 +463,7 @@ protected Object rewriteForDocValue(Object value) {
447463 @ Override
448464 public Query termQueryCaseInsensitive (Object value , QueryShardContext context ) {
449465 failIfNotIndexedAndNoDocValues ();
466+ checkToDisableCaching (context );
450467 if (isSearchable ()) {
451468 return super .termQueryCaseInsensitive (value , context );
452469 } else {
@@ -467,6 +484,7 @@ public Query termQueryCaseInsensitive(Object value, QueryShardContext context) {
467484 @ Override
468485 public Query termQuery (Object value , QueryShardContext context ) {
469486 failIfNotIndexedAndNoDocValues ();
487+ checkToDisableCaching (context );
470488 if (isSearchable ()) {
471489 Query query = super .termQuery (value , context );
472490 if (!this .useSimilarity ) {
@@ -494,6 +512,7 @@ public Query termQuery(Object value, QueryShardContext context) {
494512 @ Override
495513 public Query termsQuery (List <?> values , QueryShardContext context ) {
496514 failIfNotIndexedAndNoDocValues ();
515+ checkToDisableCaching (context );
497516 // has index and doc_values enabled
498517 if (isSearchable () && hasDocValues ()) {
499518 if (!context .keywordFieldIndexOrDocValuesEnabled ()) {
@@ -540,6 +559,7 @@ public Query prefixQuery(
540559 );
541560 }
542561 failIfNotIndexedAndNoDocValues ();
562+ checkToDisableCaching (context );
543563 if (isSearchable () && hasDocValues ()) {
544564 if (!context .keywordFieldIndexOrDocValuesEnabled ()) {
545565 return super .prefixQuery (value , method , caseInsensitive , context );
@@ -583,6 +603,7 @@ public Query regexpQuery(
583603 );
584604 }
585605 failIfNotIndexedAndNoDocValues ();
606+ checkToDisableCaching (context );
586607 if (isSearchable () && hasDocValues ()) {
587608 if (!context .keywordFieldIndexOrDocValuesEnabled ()) {
588609 return super .regexpQuery (value , syntaxFlags , matchFlags , maxDeterminizedStates , method , context );
@@ -621,6 +642,7 @@ public Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower
621642 );
622643 }
623644 failIfNotIndexedAndNoDocValues ();
645+ checkToDisableCaching (context );
624646 if (isSearchable () && hasDocValues ()) {
625647 Query indexQuery = new TermRangeQuery (
626648 name (),
@@ -669,6 +691,7 @@ public Query fuzzyQuery(
669691 QueryShardContext context
670692 ) {
671693 failIfNotIndexedAndNoDocValues ();
694+ checkToDisableCaching (context );
672695 if (context .allowExpensiveQueries () == false ) {
673696 throw new OpenSearchException (
674697 "[fuzzy] queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES .getKey () + "' is set to " + "false."
@@ -716,6 +739,7 @@ public Query wildcardQuery(
716739 );
717740 }
718741 failIfNotIndexedAndNoDocValues ();
742+ checkToDisableCaching (context );
719743 // keyword field types are always normalized, so ignore case sensitivity and force normalize the
720744 // wildcard
721745 // query text
@@ -745,6 +769,13 @@ public Query wildcardQuery(
745769 return super .wildcardQuery (value , method , caseInsensitive , true , context );
746770 }
747771
772+ private void checkToDisableCaching (QueryShardContext context ) {
773+ // Mark the query as non-cacheable if the defaults for useSimilarity or splitQueriesOnWhitespace are not used.
774+ if (useSimilarity || splitQueriesOnWhitespace ) {
775+ context .setIsCacheable (false );
776+ }
777+ }
778+
748779 }
749780
750781 private final boolean indexed ;
0 commit comments