Select to view content in your preferred language

Definition Expression on a Query

2160
2
09-16-2013 12:55 PM
KennethRichards
Occasional Contributor
Does anyone one know of a way to set a definition expression on a query? I have about 4,000 records in my service but I want to limit all queries to about 1,000 of those records.
0 Kudos
2 Replies
BrianBeck
Regular Contributor
Does anyone one know of a way to set a definition expression on a query? I have about 4,000 records in my service but I want to limit all queries to about 1,000 of those records.


As far as I know, the only way to set a definition expression on the layer itself and not for individual queries.  However, since the definition expression is just SQL, you could just add that into your query where clause.

Otherwise, you could create a second FeatureLayer that may not be visible on the map and set the definition expression on that before querying it.  Of course, the query would not be reflected on the map, but you would be able to access those features.

If you are simply trying to limit the number of features returned by a query for performance reasons.  This is a setting when publishing a service and is usually set to 1000 by default.
0 Kudos
JasonZou
Frequent Contributor
I am not sure I understand your question clearly. If you like to query a subset of a layer data, set query.where to limit the result based on the attribute values. That's about the equivalent of definition expression if using Query. If you do need to use the real definition expression as used in ArcMap, create a FeatureLayer for the layer you like to query, and then set definition expression like the below sample:

require(["esri/layers/FeatureLayer", ...], function(FeatureLayer, ...) {
    var featureLayer = new FeatureLayer("rest/url/of/the/layer",{
        mode: FeatureLayer.MODE_SELECTION,
        outFields: ["*"]
    }); 
    featureLayer.setDefinitionExpression("STATE_NAME = 'South Carolina'");
    ...
});


You can further query against the featureLayer using featureLayer.queryFeatures.

Hope it helps.
0 Kudos