3434import org .elasticsearch .common .xcontent .XContentBuilder ;
3535import org .elasticsearch .common .xcontent .XContentParser ;
3636import org .elasticsearch .index .analysis .NamedAnalyzer ;
37- import org .elasticsearch .index .mapper .DateFieldMapper ;
38- import org .elasticsearch .index .mapper .IpFieldMapper ;
39- import org .elasticsearch .index .mapper .KeywordFieldMapper ;
40- import org .elasticsearch .index .mapper .MappedFieldType ;
41- import org .elasticsearch .index .mapper .MapperService ;
42- import org .elasticsearch .index .mapper .NumberFieldMapper ;
43- import org .elasticsearch .index .mapper .ScaledFloatFieldMapper ;
44- import org .elasticsearch .index .mapper .TextFieldMapper ;
4537import org .elasticsearch .index .query .support .QueryParsers ;
4638import org .elasticsearch .index .search .QueryStringQueryParser ;
4739import org .joda .time .DateTimeZone ;
4840
4941import java .io .IOException ;
5042import java .util .ArrayList ;
51- import java .util .Collection ;
5243import java .util .HashMap ;
53- import java .util .HashSet ;
5444import java .util .List ;
5545import java .util .Locale ;
5646import java .util .Map ;
5747import java .util .Objects ;
58- import java .util .Set ;
5948import java .util .TreeMap ;
6049
6150/**
@@ -110,24 +99,10 @@ public class QueryStringQueryBuilder extends AbstractQueryBuilder<QueryStringQue
11099 private static final ParseField TIME_ZONE_FIELD = new ParseField ("time_zone" );
111100 private static final ParseField SPLIT_ON_WHITESPACE = new ParseField ("split_on_whitespace" )
112101 .withAllDeprecated ("This setting is ignored, the parser always splits on logical operator" );
113- private static final ParseField ALL_FIELDS_FIELD = new ParseField ("all_fields" );
102+ private static final ParseField ALL_FIELDS_FIELD = new ParseField ("all_fields" )
103+ .withAllDeprecated ("Set [default_field] to `*` instead" );
114104 private static final ParseField TYPE_FIELD = new ParseField ("type" );
115105
116- // Mapping types the "all-ish" query can be executed against
117- public static final Set <String > ALLOWED_QUERY_MAPPER_TYPES ;
118-
119- static {
120- ALLOWED_QUERY_MAPPER_TYPES = new HashSet <>();
121- ALLOWED_QUERY_MAPPER_TYPES .add (DateFieldMapper .CONTENT_TYPE );
122- ALLOWED_QUERY_MAPPER_TYPES .add (IpFieldMapper .CONTENT_TYPE );
123- ALLOWED_QUERY_MAPPER_TYPES .add (KeywordFieldMapper .CONTENT_TYPE );
124- for (NumberFieldMapper .NumberType nt : NumberFieldMapper .NumberType .values ()) {
125- ALLOWED_QUERY_MAPPER_TYPES .add (nt .typeName ());
126- }
127- ALLOWED_QUERY_MAPPER_TYPES .add (ScaledFloatFieldMapper .CONTENT_TYPE );
128- ALLOWED_QUERY_MAPPER_TYPES .add (TextFieldMapper .CONTENT_TYPE );
129- }
130-
131106 private final String queryString ;
132107
133108 private String defaultField ;
@@ -179,8 +154,6 @@ public class QueryStringQueryBuilder extends AbstractQueryBuilder<QueryStringQue
179154
180155 private DateTimeZone timeZone ;
181156
182- private Boolean useAllFields ;
183-
184157 /** To limit effort spent determinizing regexp queries. */
185158 private int maxDeterminizedStates = DEFAULT_MAX_DETERMINED_STATES ;
186159
@@ -240,8 +213,11 @@ public QueryStringQueryBuilder(StreamInput in) throws IOException {
240213 if (in .getVersion ().onOrAfter (Version .V_5_1_1 )) {
241214 if (in .getVersion ().before (Version .V_6_0_0_beta1 )) {
242215 in .readBoolean (); // split_on_whitespace
216+ Boolean useAllField = in .readOptionalBoolean ();
217+ if (useAllField != null && useAllField ) {
218+ defaultField = "*" ;
219+ }
243220 }
244- useAllFields = in .readOptionalBoolean ();
245221 }
246222 }
247223
@@ -291,8 +267,9 @@ protected void doWriteTo(StreamOutput out) throws IOException {
291267 if (out .getVersion ().onOrAfter (Version .V_5_1_1 )) {
292268 if (out .getVersion ().before (Version .V_6_0_0_beta1 )) {
293269 out .writeBoolean (false ); // split_on_whitespace
270+ Boolean useAllFields = defaultField == null ? null : Regex .isMatchAllPattern (defaultField );
271+ out .writeOptionalBoolean (useAllFields );
294272 }
295- out .writeOptionalBoolean (this .useAllFields );
296273 }
297274 }
298275
@@ -314,17 +291,19 @@ public String defaultField() {
314291 }
315292
316293 /**
317- * Tell the query_string query to use all fields explicitly, even if _all is
318- * enabled. If the "default_field" parameter or "fields" are specified, they
319- * will be ignored.
294+ * This setting is deprecated, set {@link #defaultField(String)} to "*" instead.
320295 */
296+ @ Deprecated
321297 public QueryStringQueryBuilder useAllFields (Boolean useAllFields ) {
322- this .useAllFields = useAllFields ;
298+ if (useAllFields ) {
299+ this .defaultField = "*" ;
300+ }
323301 return this ;
324302 }
325303
304+ @ Deprecated
326305 public Boolean useAllFields () {
327- return this . useAllFields ;
306+ return defaultField == null ? null : Regex . isMatchAllPattern ( defaultField ) ;
328307 }
329308
330309 /**
@@ -703,9 +682,6 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
703682 builder .field (TIME_ZONE_FIELD .getPreferredName (), this .timeZone .getID ());
704683 }
705684 builder .field (ESCAPE_FIELD .getPreferredName (), this .escape );
706- if (this .useAllFields != null ) {
707- builder .field (ALL_FIELDS_FIELD .getPreferredName (), this .useAllFields );
708- }
709685 printBoostAndQueryName (builder );
710686 builder .endObject ();
711687 }
@@ -737,7 +713,6 @@ public static QueryStringQueryBuilder fromXContent(XContentParser parser) throws
737713 Fuzziness fuzziness = QueryStringQueryBuilder .DEFAULT_FUZZINESS ;
738714 String fuzzyRewrite = null ;
739715 String rewrite = null ;
740- Boolean useAllFields = null ;
741716 Map <String , Float > fieldsAndWeights = new HashMap <>();
742717 while ((token = parser .nextToken ()) != XContentParser .Token .END_OBJECT ) {
743718 if (token == XContentParser .Token .FIELD_NAME ) {
@@ -812,7 +787,7 @@ public static QueryStringQueryBuilder fromXContent(XContentParser parser) throws
812787 } else if (LENIENT_FIELD .match (currentFieldName )) {
813788 lenient = parser .booleanValue ();
814789 } else if (ALL_FIELDS_FIELD .match (currentFieldName )) {
815- useAllFields = parser . booleanValue () ;
790+ defaultField = "*" ;
816791 } else if (MAX_DETERMINIZED_STATES_FIELD .match (currentFieldName )) {
817792 maxDeterminizedStates = parser .intValue ();
818793 } else if (TIME_ZONE_FIELD .match (currentFieldName )) {
@@ -847,12 +822,6 @@ public static QueryStringQueryBuilder fromXContent(XContentParser parser) throws
847822 throw new ParsingException (parser .getTokenLocation (), "[" + QueryStringQueryBuilder .NAME + "] must be provided with a [query]" );
848823 }
849824
850- if ((useAllFields != null && useAllFields ) &&
851- (defaultField != null || fieldsAndWeights .size () != 0 )) {
852- throw new ParsingException (parser .getTokenLocation (),
853- "cannot use [all_fields] parameter in conjunction with [default_field] or [fields]" );
854- }
855-
856825 QueryStringQueryBuilder queryStringQuery = new QueryStringQueryBuilder (queryString );
857826 queryStringQuery .fields (fieldsAndWeights );
858827 queryStringQuery .defaultField (defaultField );
@@ -880,7 +849,6 @@ public static QueryStringQueryBuilder fromXContent(XContentParser parser) throws
880849 queryStringQuery .timeZone (timeZone );
881850 queryStringQuery .boost (boost );
882851 queryStringQuery .queryName (queryName );
883- queryStringQuery .useAllFields (useAllFields );
884852 return queryStringQuery ;
885853 }
886854
@@ -914,8 +882,7 @@ protected boolean doEquals(QueryStringQueryBuilder other) {
914882 timeZone == null ? other .timeZone == null : other .timeZone != null &&
915883 Objects .equals (timeZone .getID (), other .timeZone .getID ()) &&
916884 Objects .equals (escape , other .escape ) &&
917- Objects .equals (maxDeterminizedStates , other .maxDeterminizedStates ) &&
918- Objects .equals (useAllFields , other .useAllFields );
885+ Objects .equals (maxDeterminizedStates , other .maxDeterminizedStates );
919886 }
920887
921888 @ Override
@@ -924,72 +891,37 @@ protected int doHashCode() {
924891 quoteFieldSuffix , allowLeadingWildcard , analyzeWildcard ,
925892 enablePositionIncrements , fuzziness , fuzzyPrefixLength ,
926893 fuzzyMaxExpansions , fuzzyRewrite , phraseSlop , type , tieBreaker , rewrite , minimumShouldMatch , lenient ,
927- timeZone == null ? 0 : timeZone .getID (), escape , maxDeterminizedStates , useAllFields );
928- }
929-
930- /**
931- * Given a shard context, return a map of all fields in the mappings that
932- * can be queried. The map will be field name to a float of 1.0f.
933- */
934- public static Map <String , Float > allQueryableDefaultFields (QueryShardContext context ) {
935- Collection <String > allFields = context .simpleMatchToIndexNames ("*" );
936- Map <String , Float > fields = new HashMap <>();
937- for (String fieldName : allFields ) {
938- if (MapperService .isMetadataField (fieldName )) {
939- // Ignore our metadata fields
940- continue ;
941- }
942- MappedFieldType mft = context .fieldMapper (fieldName );
943- assert mft != null : "should never have a null mapper for an existing field" ;
944-
945- // Ignore fields that are not in the allowed mapper types. Some
946- // types do not support term queries, and thus we cannot generate
947- // a special query for them.
948- String mappingType = mft .typeName ();
949- if (ALLOWED_QUERY_MAPPER_TYPES .contains (mappingType )) {
950- fields .put (fieldName , 1.0f );
951- }
952- }
953- return fields ;
894+ timeZone == null ? 0 : timeZone .getID (), escape , maxDeterminizedStates );
954895 }
955896
956897 @ Override
957898 protected Query doToQuery (QueryShardContext context ) throws IOException {
958899 String rewrittenQueryString = escape ? org .apache .lucene .queryparser .classic .QueryParser .escape (this .queryString ) : queryString ;
959- if (( useAllFields != null && useAllFields ) && ( fieldsAndWeights .size () != 0 || this .defaultField != null ) ) {
960- throw addValidationError ("cannot use [all_fields ] parameter in conjunction with [default_field] or [fields ]" , null );
900+ if (fieldsAndWeights .size () > 0 && this .defaultField != null ) {
901+ throw addValidationError ("cannot use [fields ] parameter in conjunction with [default_field]" , null );
961902 }
962903
963904 QueryStringQueryParser queryParser ;
964905 boolean isLenient = lenient == null ? context .queryStringLenient () : lenient ;
965906 if (defaultField != null ) {
966- queryParser = new QueryStringQueryParser (context , defaultField , isLenient );
967- } else if (fieldsAndWeights .size () > 0 ) {
968- final Map <String , Float > resolvedFields = new TreeMap <>();
969- for (Map .Entry <String , Float > fieldsEntry : fieldsAndWeights .entrySet ()) {
970- String fieldName = fieldsEntry .getKey ();
971- Float weight = fieldsEntry .getValue ();
972- if (Regex .isSimpleMatchPattern (fieldName )) {
973- for (String resolvedFieldName : context .getMapperService ().simpleMatchToIndexNames (fieldName )) {
974- resolvedFields .put (resolvedFieldName , weight );
975- }
976- } else {
977- resolvedFields .put (fieldName , weight );
978- }
907+ if (Regex .isMatchAllPattern (defaultField )) {
908+ queryParser = new QueryStringQueryParser (context , lenient == null ? true : lenient );
909+ } else {
910+ queryParser = new QueryStringQueryParser (context , defaultField , isLenient );
979911 }
912+ } else if (fieldsAndWeights .size () > 0 ) {
913+ final Map <String , Float > resolvedFields = QueryStringQueryParser .resolveMappingFields (context , fieldsAndWeights );
980914 queryParser = new QueryStringQueryParser (context , resolvedFields , isLenient );
981915 } else {
982- // If explicitly required to use all fields, use all fields, OR:
983- // Automatically determine the fields (to replace the _all field) if all of the following are true:
984- // - The _all field is disabled,
985- // - and the default_field has not been changed in the settings
986- // - and default_field is not specified in the request
987- // - and no fields are specified in the request
988- if ((useAllFields != null && useAllFields ) ||
916+ // Expand to all fields if:
917+ // - The index default search field is "*"
918+ // - The index default search field is "_all" and _all is disabled
919+ // TODO the index default search field should be "*" for new indices.
920+ if (Regex .isMatchAllPattern (context .defaultField ()) ||
989921 (context .getMapperService ().allEnabled () == false && "_all" .equals (context .defaultField ()))) {
990922 // Automatically determine the fields from the index mapping.
991923 // Automatically set leniency to "true" if unset so mismatched fields don't cause exceptions;
992- queryParser = new QueryStringQueryParser (context , allQueryableDefaultFields ( context ), lenient == null ? true : lenient );
924+ queryParser = new QueryStringQueryParser (context , lenient == null ? true : lenient );
993925 } else {
994926 queryParser = new QueryStringQueryParser (context , context .defaultField (), isLenient );
995927 }
0 commit comments