require(["esri/tasks/QueryTask"], function(QueryTask) { /* code goes here */ });
Class: esri/tasks/QueryTask
Inheritance: QueryTask Task Accessor
Since: ArcGIS API for JavaScript 4.0

Executes different types of query operations on a layer. The most common method used in this class is execute(), which executes the query as defined in the Query object that is passed as a parameter to the function. QueryTask.execute() returns a Promise that resolves to a FeatureSet, which contains the features in the layer that satisfy the Query.

With QueryTask, you can also obtain the number of features that satisfy the query, the extent of features, related features or records associated with a feature, attachments for features or the featureIds of features.

For example, when working with a feature layer of world cities, to obtain a FeatureSet of cities with a population greater than one million people, use the following code:

require(["esri/tasks/QueryTask", "esri/tasks/support/Query"], function(QueryTask, Query){
  var citiesLayerUrl = " ... "; // Represents the REST endpoint for a layer of cities.
  var queryTask = new QueryTask({
    url: citiesLayerUrl
  });
  var query = new Query();
  query.returnGeometry = true;
  query.outFields = ["*"];
  query.where = "POP > 1000000";  // Return all cities with a population greater than 1 million
  // When resolved, returns features and graphics that satisfy the query.
  queryTask.execute(query).then(function(results){
    console.log(results.features);
  });

  // When resolved, returns a count of the features that satisfy the query.
  queryTask.executeForCount(query).then(function(results){
    console.log(results);
  });

});
See also:

Constructors

new QueryTask(properties)
Parameter:
properties Object
optional

See the properties for a list of all the properties that may be passed into the constructor.

Property Overview

Any properties can be set, retrieved or listened to. See the Working with Properties topic.
NameTypeSummaryClass
String

The name of the class.

more details
more detailsAccessor
String

Specify the geodatabase version to query.

more details
more detailsQueryTask
Object

The options to be used for data requests.

more details
more detailsTask
String

The ArcGIS Server REST service URL (usually of a Feature Service Layer or Map Service Layer) for use in a task.

more details
more detailsTask

Property Details

declaredClassStringreadonly inherited
Since: ArcGIS API for JavaScript 4.7

The name of the class. The declared class name is formatted as esri.folder.className.

gdbVersionString

Specify the geodatabase version to query. Requires ArcGIS Server service 10.1 or greater.

requestOptionsObject inherited

The options to be used for data requests. These options can also be controlled through the requestOptions method parameter.

The ArcGIS Server REST service URL (usually of a Feature Service Layer or Map Service Layer) for use in a task.

Method Overview

NameReturn TypeSummaryClass
Promise<FeatureSet>

Executes a Query against the layer specified in the url.

more details
more detailsQueryTask
Promise<AttachmentInfo[]>

Query information about attachments associated with features from a feature layer specified in the url.

more details
more detailsQueryTask
Promise<Number>

Gets a count of the number of features that satisfy the input query.

more details
more detailsQueryTask
Promise<Object>

Gets the extent of the features that satisfy the input query.

more details
more detailsQueryTask
Promise<Number[]>

Executes a Query against the layer specified in the url.

more details
more detailsQueryTask
Promise<FeatureSet>

Executes a RelationshipQuery against the layer or table specified in the url.

more details
more detailsQueryTask

Method Details

execute(query, requestOptions){Promise<FeatureSet>}

Executes a Query against the layer specified in the url. The result is returned as a FeatureSet, which can be accessed using the .then() method. A FeatureSet contains an array of Graphic features, which can be added to the map. This array will not be populated if no results are found.

Parameters:
Autocasts from Object

Specifies the attributes and spatial filter of the query.

requestOptions Object
optional

Additional options to be used for the data request (will override requestOptions defined during construction).

Returns:
TypeDescription
Promise<FeatureSet>When resolved, a FeatureSet containing an array of graphic features is returned.
Example:
require([
  "esri/tasks/support/Query", "esri/tasks/QueryTask", ...
], function(Query, QueryTask, ... ) {
  var query = new Query();
  var queryTask = new QueryTask( ... );
  query.where = "STATE_NAME = 'Washington'";
  query.outSpatialReference = { wkid:102100 };
  query.returnGeometry = true;
  query.outFields = [ "CITY_NAME" ];
  queryTask.execute(query).then(function(results){
    // Results.graphics contains the graphics returned from query
  });
  ...
});
executeAttachmentQuery(attachmentQuery, requestOptions){Promise<AttachmentInfo[]>}
Since: ArcGIS API for JavaScript 4.9

Query information about attachments associated with features from a feature layer specified in the url. It will return an error if the layer does not support attachments.

Parameters:
attachmentQuery AttachmentQuery autocast
Autocasts from Object

Specifies the attachment parameters for query.

requestOptions Object
optional

Additional options to be used for the data request (will override requestOptions defined during construction).

Returns:
TypeDescription
Promise<AttachmentInfo[]>When resolved, returns AttachmentInfos grouped by the source feature objectIds.
See also:
executeForCount(query, requestOptions){Promise<Number>}

Gets a count of the number of features that satisfy the input query. Valid for layers published with 10.0 SP1.

Parameters:
Autocasts from Object

Specifies the attributes and spatial filter of the query.

requestOptions Object
optional

Additional options to be used for the data request (will override requestOptions defined during construction).

Returns:
TypeDescription
Promise<Number>When resolved, the result is the number of features that satisfy the input query.
Example:
require([
  "esri/tasks/QueryTask", ...
], function(QueryTask, ... ) {
  var queryTask = new QueryTask( ... );
  queryTask.executeForCount({  // autocasts as new Query()
     where: "POP90_SQMI &lt; 100"
  }).then(function(count){
    console.log(count, " features matched the input query");
  }, function(error){
       console.log(error); // Will print error in console if unsupported layers are used
     });
});
executeForExtent(params, requestOptions){Promise<Object>}

Gets the extent of the features that satisfy the input query. The count of features that satisfy the input query is returned upon resolution as well. At 10.3, this option is only available for hosted feature services. At 10.3.1, this option is available for all feature services and map service layers.

Parameters:
params Query

Specifies the attributes and spatial filter of the query.

requestOptions Object
optional

Additional options to be used for the data request (will override requestOptions defined during construction).

Returns:
TypeDescription
Promise<Object>When resolved, returns the extent and count of the features that satisfy the input query. See the object specification table below for details.
PropertyTypeDescription
countnumberThe number of features that satisfy the input query.
extentExtentThe extent of the features that satisfy the query.
executeForIds(query, requestOptions){Promise<Number[]>}

Executes a Query against the layer specified in the url. The result is an array of the object IDs of features that satisfy the input query.

Parameters:
Autocasts from Object

Specifies the attributes and spatial filter of the query.

requestOptions Object
optional

Additional options to be used for the data request (will override requestOptions defined during construction).

Returns:
TypeDescription
Promise<Number[]>When resolved, the result is an array of object IDs for features that satisfy the input query.
Example:
require([
  "esri/tasks/QueryTask", ...
], function(QueryTask, ... ) {
  var queryTask = new QueryTask( ... );
  queryTask.executeForIds({  // autocasts as new Query()
     where: "region = 'Southern California'"
  }).then(function(results){
    console.log(results);  // an array of object IDs
  });
  ...
});
executeRelationshipQuery(relationshipQuery, requestOptions){Promise<FeatureSet>}

Executes a RelationshipQuery against the layer or table specified in the url. If the query is successful, the returned results are FeatureSets grouped by source layer or table objectIds.

Parameters:
relationshipQuery RelationshipQuery autocast
Autocasts from Object

Specifies relationship parameters for querying related features or records from a layer or a table.

requestOptions Object
optional

Additional options to be used for the data request (will override requestOptions defined during construction).

Returns:
TypeDescription
Promise<FeatureSet>When resolved, the results are FeatureSets grouped by source layer or table objectIds. Each FeatureSet contains an array of Graphic features including the values of the fields requested by the user.
Example:
// specify relationship query parameter
const query = new RelationshipQuery({
  outFields: ["*"],
  relationshipId: relationshipId,
  objectIds: [385, 416]
});

// query related features that meet the query parameters
queryTask.executeRelationshipQuery(query).then(function (results) {
  console.log("queryTask results", results);
})
.catch(function (error) {
  console.log("query task error", error);
});

Type Definitions

AttachmentInfoObject

The AttachmentInfo returns information about attachments associated with a feature. This resource is available only if the feature layer has attachments.

Properties:
contentType String

The content type of the attachment. For example, image/jpeg. See the ArcGIS REST API documentation for more information on supported attachment types.

The identifier for the attachment.

name String

The name of the attachment.

parentObjectId Number

The parent or the feature object id of the attachment.

size Number

The size of the attachment.

url String

The URL of the attachment.

API Reference search results

NameTypeModule
Loading...