Query a layer with multiple search criterias

4315
28
Jump to solution
04-09-2014 08:05 PM
kiranbodhasrungi
New Contributor
hi,

i have a requirement in my project where i have to query the layer with the help of multiple search fields.How can i achieve this?

till now i have been doing it like this:

findParams.layerIds = [1];
findParams.searchFields = ["STREET_NAME", "STREET_NUMBER"];

findTask.execute(findParams, showResults);

I have  a single text box and i query the map with the help street name OR number.How can i combine both(similar to AND) to filter results?

thanks in advance.

keran.
0 Kudos
1 Solution

Accepted Solutions
RaymondGoins
Occasional Contributor
Yes this code does that.

Since I am relatively new to the arcgis jsapi, I think the difference is the ability to query more than one field and use an operator other than equal.

Here is the other function which creates the list into a table
function addrList(results)
        {
          var template = "";
          var addr = results.features;
          template = "<i>Streets Found: " + addr.length + "</i>";
          template += "<table border='1'>";
          template += "<tr>"
          template += "</tr>";
          for (var i = 0, il = addr.length; i < il; i++)
          {
            template += "<tr>";
            template += "<td>"+ addr.attributes["PRIMARYADD"] +" "+ addr.attributes["ZN"] +"</td>";
            template += '<td><a href="#" onclick="featureZoom(AddrResults.features['+ i +']); return false;">(Zoom To)</a></td>';
            template += "</tr>";
          }

          template += "</table>";

          return template;
        }


Hope that helps

Ray

View solution in original post

0 Kudos
28 Replies
TimCollyer
Occasional Contributor
I think "searchText" is probably what you are looking for.

https://developers.arcgis.com/javascript/jsapi/findparameters-amd.html#searchtext
0 Kudos
kiranbodhasrungi
New Contributor
Thanks for the reply!!

SearchText will do a search through out all the search params,i want to club multiple search results.For example:

there are 3 search params,street name,street number,owner,i want to search by both street name SOUTH and owner name katz.
Normal search would return rows which has SOUTH in any of the 3 params.

thanks,
keran.
0 Kudos
TimCollyer
Occasional Contributor
If you are querying just a single layer, you could use a query task and write a more specific "where" clause like "street_name = 'SOUTH' and owner_name = 'katz'".

https://developers.arcgis.com/javascript/jsapi/query-amd.html#where
0 Kudos
RaymondGoins
Occasional Contributor
I use a query task instead of find task here is a sample.
        function AddressSearchTask(stnum, stname, stcity)
        {
          streetname = stname.toUpperCase();
          queryTask = new QueryTask("http://192.168.20.14:6080/arcgis/rest/services/emap12/MapServer/17");
          query = new Query();
          query.returnGeometry = true;
          query.outFields = ["PRIMARYADD", "ZN"];
          query.where = "HOUSENUMBE = '" + stnum + "' AND PRIMARYNAM LIKE '%" + streetname + "%'";
          if(stcity != 0)
          {
            query.where = "HOUSENUMBE = '" + stnum + "' AND PRIMARYNAM LIKE '%" + streetname + "%' AND ZN = '" + stcity + "'";
          }
          queryTask.execute(query, pickAddress);
        }


The 17 is the layer id from your service. You should be able to modify it to use your fields.

query.where uses Standard Query Language(SQL) so you can use LIKE, IN, ect..

This returns the results to another function called pickAddress
        function pickAddress(results)
        {
          AddrFeatures = results.features;
          console.log(results);
          var scount = AddrFeatures.length;
          switch(scount)
          {
            case 0:
            showMessage("asmessages", "Address not found");
            break;
            case 1:
            zoomToStreet(results);
            break;
            default:
            AddrResults = {displayFieldName: null, features:[]};
            for (var i = 0; i < scount; i++)
            {
              var aResult = AddrFeatures;
              if (!AddrResults.displayFieldName){ AddrResults.displayFieldName = aResult.displayFieldName }
              AddrResults.features.push(aResult);
            }
            var stlist = addrList(AddrResults);
            $("#addrlist").html(stlist);
            $("#addrlist").show("slow");
            break;
          }
        }


If more than one result is found it will create a pick list if only one result is found it will zoom to the point. Some of this code requires other functions and variable in order to function correctly which I have not included.

BTW I have enclosed these function in jQuery in order to populate divs and tables. You can use the stardard ArcGIS JSAPI functions to do this if you like.

Ray
0 Kudos
kiranbodhasrungi
New Contributor
If you are querying just a single layer, you could use a query task and write a more specific "where" clause like "street_name = 'SOUTH' and owner_name = 'katz'".

https://developers.arcgis.com/javascript/jsapi/query-amd.html#where


Yes,I am querying a single layer.This is the one am looking for.Thanks!!
0 Kudos
kiranbodhasrungi
New Contributor
I use a query task instead of find task here is a sample.
        function AddressSearchTask(stnum, stname, stcity)
        {
          streetname = stname.toUpperCase();
          queryTask = new QueryTask("http://192.168.20.14:6080/arcgis/rest/services/emap12/MapServer/17");
          query = new Query();
          query.returnGeometry = true;
          query.outFields = ["PRIMARYADD", "ZN"];
          query.where = "HOUSENUMBE = '" + stnum + "' AND PRIMARYNAM LIKE '%" + streetname + "%'";
          if(stcity != 0)
          {
            query.where = "HOUSENUMBE = '" + stnum + "' AND PRIMARYNAM LIKE '%" + streetname + "%' AND ZN = '" + stcity + "'";
          }
          queryTask.execute(query, pickAddress);
        }


The 17 is the layer id from your service. You should be able to modify it to use your fields.

query.where uses Standard Query Language(SQL) so you can use LIKE, IN, ect..

This returns the results to another function called pickAddress
        function pickAddress(results)
        {
          AddrFeatures = results.features;
          console.log(results);
          var scount = AddrFeatures.length;
          switch(scount)
          {
            case 0:
            showMessage("asmessages", "Address not found");
            break;
            case 1:
            zoomToStreet(results);
            break;
            default:
            AddrResults = {displayFieldName: null, features:[]};
            for (var i = 0; i < scount; i++)
            {
              var aResult = AddrFeatures;
              if (!AddrResults.displayFieldName){ AddrResults.displayFieldName = aResult.displayFieldName }
              AddrResults.features.push(aResult);
            }
            var stlist = addrList(AddrResults);
            $("#addrlist").html(stlist);
            $("#addrlist").show("slow");
            break;
          }
        }


If more than one result is found it will create a pick list if only one result is found it will zoom to the point. Some of this code requires other functions and variable in order to function correctly which I have not included.

BTW I have enclosed these function in jQuery in order to populate divs and tables. You can use the stardard ArcGIS JSAPI functions to do this if you like.

Ray


Thanks for the reply!! Between what is the difference between find and query?both are like SQL rite!! I intend to perform a search on a single layer which should return me some values which i show in a datagrid and the map,when i click on a row on the grid it should zoom to the point.is this the way your code works?
0 Kudos
RaymondGoins
Occasional Contributor
Yes this code does that.

Since I am relatively new to the arcgis jsapi, I think the difference is the ability to query more than one field and use an operator other than equal.

Here is the other function which creates the list into a table
function addrList(results)
        {
          var template = "";
          var addr = results.features;
          template = "<i>Streets Found: " + addr.length + "</i>";
          template += "<table border='1'>";
          template += "<tr>"
          template += "</tr>";
          for (var i = 0, il = addr.length; i < il; i++)
          {
            template += "<tr>";
            template += "<td>"+ addr.attributes["PRIMARYADD"] +" "+ addr.attributes["ZN"] +"</td>";
            template += '<td><a href="#" onclick="featureZoom(AddrResults.features['+ i +']); return false;">(Zoom To)</a></td>';
            template += "</tr>";
          }

          template += "</table>";

          return template;
        }


Hope that helps

Ray
0 Kudos
kiranbodhasrungi
New Contributor
Ok.That clears my doubt!! you saved lot of my time!! thnks!!
0 Kudos
kiranbodhasrungi
New Contributor
Ray,

am having problems with the featureZoom code,could you please help?

thanks,
keran.
0 Kudos