QueryTask
require(["esri/tasks/QueryTask"], function(QueryTask) { /* code goes here */ });
esri/tasks/QueryTask
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);
});
});
Constructors
- new QueryTask(properties)
- Parameter:properties Objectoptional
See the properties for a list of all the properties that may be passed into the constructor.
Property Overview
Name | Type | Summary | Class | |
---|---|---|---|---|
String | The name of the class. more details | more details | Accessor | |
String | Specify the geodatabase version to query. more details | more details | QueryTask | |
Object | The options to be used for data requests. more details | more details | Task | |
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 details | Task |
Property Details
- 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.
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
Name | Return Type | Summary | Class | |
---|---|---|---|---|
Promise<FeatureSet> | Executes a Query against the layer specified in the url. more details | more details | QueryTask | |
Promise<AttachmentInfo[]> | Query information about attachments associated with features from a feature layer specified in the url. more details | more details | QueryTask | |
Promise<Number> | Gets a count of the number of features that satisfy the input query. more details | more details | QueryTask | |
Promise<Object> | Gets the extent of the features that satisfy the input query. more details | more details | QueryTask | |
Promise<Number[]> | Executes a Query against the layer specified in the url. more details | more details | QueryTask | |
Promise<FeatureSet> | Executes a RelationshipQuery against the layer or table specified in the url. more details | more details | QueryTask |
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 ObjectSpecifies the attributes and spatial filter of the query.
requestOptions ObjectoptionalAdditional options to be used for the data request (will override requestOptions defined during construction).
Returns:Type Description 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:Autocasts from ObjectSpecifies the attachment parameters for query.
requestOptions ObjectoptionalAdditional options to be used for the data request (will override requestOptions defined during construction).
Returns:Type Description Promise<AttachmentInfo[]> When resolved, returns AttachmentInfos grouped by the source feature objectIds.
Gets a count of the number of features that satisfy the input query. Valid for layers published with 10.0 SP1.
Parameters:Autocasts from ObjectSpecifies the attributes and spatial filter of the query.
requestOptions ObjectoptionalAdditional options to be used for the data request (will override requestOptions defined during construction).
Returns:Type Description 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 < 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 }); });
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 QuerySpecifies the attributes and spatial filter of the query.
requestOptions ObjectoptionalAdditional options to be used for the data request (will override requestOptions defined during construction).
Returns:Type Description 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. Property Type Description count number The number of features that satisfy the input query. extent Extent The extent of the features that satisfy the query.
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 ObjectSpecifies the attributes and spatial filter of the query.
requestOptions ObjectoptionalAdditional options to be used for the data request (will override requestOptions defined during construction).
Returns:Type Description 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:Autocasts from ObjectSpecifies relationship parameters for querying related features or records from a layer or a table.
requestOptions ObjectoptionalAdditional options to be used for the data request (will override requestOptions defined during construction).
Returns:Type Description 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.id NumberThe identifier for the attachment.
name StringThe name of the attachment.
parentObjectId NumberThe parent or the feature object id of the attachment.
size NumberThe size of the attachment.
url StringThe URL of the attachment.