The input to your query is stored in a Query object which contains various parameters that can be set for the query. The QueryTask object executes the task using the input provided in the Query object, and the result set is stored in a FeatureSet object which contains an array of Graphic features.
The ‘queryGeometry’ property is used to input a geometry that will be used in a spatial query and will be a point, line, or polygon geometry. The complementary ‘spatialRelationship’ property can be set to one of the constants you see in the figure below.
The ‘where’ and ‘text’ properties are used to define an attribute query. The only difference being that the ‘text’ property is shorthand for a where clause using ‘like’.
In the code example below you can see how a Query object is created. Here, we are defining a spatial query which is evident from the use of the ‘geometry’ and ‘spatialRelationship’ properties. In addition, we have also specified that the features returned as a result of the query should include the geometry and also the fields that will be returned. This is a spatial query only; no attribute query has been defined with either the ‘where’ or ‘text’ properties.
However, we could easily include an attribute query as seen below through the use of the ‘where’ property. Here we are simply performing an attribute query to find records where the value of the ‘POP2007’ field is greater than the variable ‘population’.
A QueryTask object is used to execute a query against a layer in a map service. This QueryTask object needs the input parameters which are defined within a Query object as we saw in the section above. Once these input parameters have been defined they are passed into the QueryTask.execute() method which submits the query task to ArcGIS Server. The creation of an instance of QueryTask needs to point to a specific data layer within a map service as you can see in the code example below.
In this case we are defining that our query will execute against layer ‘1’ which is the Census Block Group layer from the ESRI_Census_USA map service as you can see from examining the ArcGIS Services Directory.
Now that an instance of QueryTask has been created we can call the QueryTask.execute() method, passing in the Query object we have already created.
The query is then executed on the server. A FeatureSet object containing the results of the query is then passed back into a callback function. In this case ‘dojo.connect’ has been used to define an event handler for the ‘onComplete’ event for our QueryTask. The FeatureSet object is passed into this function as the variable ‘fset’.
This FeatureSet can then be processed for display on a map or as a tabular display on your web page. The FeatureSet is nothing more than an array of Graphic which you can plot on your map.
ESRI provides a great example of a spatial query here.