search widget only returning first value

3751
8
Jump to solution
02-02-2016 11:56 PM
FriedaL
New Contributor II

I use the search widget to allow the user to find features in a Feature Layer.

I created several searches - one for each field - but all querying the same layer. Generally the search is working. However I do encounter two problems:

a) I don't get any suggestions while I have set, enableSuggestions: true,
It does return suggestions for the Geolocator but not on my Feature Layer. I tried it working on the following sample code: ArcGIS API for JavaScript Sandbox and just exchanged the feature layer with my feature layer (and changed the field names accordingly) and while with the current code I do get suggestions I don't get them as soon as I use my feature layer. So I'm wondering are the suggestions only working on layers using http but not https? Or is there anything that needs to be authorized when publishing the layer?

b) When I select my layer from the drop-down options the widget only returns one value, probably the first it finds. For example, I let the user query by country name. Say the user enters "USA" I was hoping the widget would return a list of all points which have the field country filled with USA but instead it just zooms to the first point (while I'm sure there are more). When I use "All" in the layer drop down options I get a first option but at the same time the popup allows me to click on the "Show more results". Which is what I also like to see when just querying my layer. Is that possible?

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Frieda,

a) See this note about suggestion support:

NOTE: Working with suggestions is only available if working with a 10.3 geocoding service that has the suggest capability loaded or a 10.3 feature layer that supports pagination, i.e. supportsPagination = true

Search widget | Guide | ArcGIS API for JavaScript .

b) Sounds like you need to adjust this property: Search | API Reference | ArcGIS API for JavaScript | autoSelect

View solution in original post

8 Replies
RobertScheitlin__GISP
MVP Emeritus

Frieda,

a) See this note about suggestion support:

NOTE: Working with suggestions is only available if working with a 10.3 geocoding service that has the suggest capability loaded or a 10.3 feature layer that supports pagination, i.e. supportsPagination = true

Search widget | Guide | ArcGIS API for JavaScript .

b) Sounds like you need to adjust this property: Search | API Reference | ArcGIS API for JavaScript | autoSelect

FriedaL
New Contributor II

Thanks.

I do work with a 10.2.2 service. Will be upgrading soon.

For the second part, however I do have trouble integrating the code.

I added the autoSelect: false at the top and now want to retrieve the results (at the bottom of the code below) but can't seem to figure out what the s in the example code:

search.on(s, 'search-result', function(e) {

        console.log ('search result');

    });

should be referencing.

//SEARCH WIDGET

    var search = new Search({

        enableButtonMode: false, //this enables the search widget to display as a single button

        enableLabel: false,

        enableInfoWindow: true,

        showInfoWindowOnSelect: true,

        autoSelect: false,

        map: map

    }, "search");

    var sources = search.get("sources");

    sources.push({

        featureLayer: new FeatureLayer("..."),

        searchFields: ["type"],

        displayField:  "type",

        exactMatch: false,

        outFields: ["name", "type", "place"],

        name: "Seach by type",

        placeholder: "Enter type",

        maxResults: 10,

        maxSuggestions: 5,

        //Create an InfoTemplate and include three fields

        infoTemplate: new InfoTemplate("${name}", "Place: ${type}</br>Place: ${place}</br>"),

        enableSuggestions: true,

        minCharacters: 0

    });

    sources.push({

        featureLayer: new FeatureLayer("..."),

        searchFields: ["name"],

        displayField:  "name",

        exactMatch: false,

        outFields: ["name", "type", "place"],

        name: "Seach by Name",

        placeholder: "Enter a name",

        maxResults: 10,

        maxSuggestions: 5,

        //Create an InfoTemplate and include three fields

        infoTemplate: new InfoTemplate("${name}", "Place: ${type}</br>Place: ${place}</br>"),

        enableSuggestions: true,

        minCharacters: 0

    });

     search.set("sources", sources);

     search.on(s, 'search-result', function(e) {

        console.log ('search result');

    });

    search.startup();  

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Frieda,

  Looks like you have two different ways of using dojo "on" mixed into the same line:

The correct code is either:

search.on('search-result', function(e) {

or (where s was the var for the search widget object)

on(search, 'search-result', function(e) {

0 Kudos
DarinaTchountcheva
Occasional Contributor II

Frieda and Robert,

I just started playing with the Search widget, and I am also having trouble with the result list.

1). I don't get a results list no matter what settings I have for "autoSelect".

And I don't ever see the results list or the "Show more results" option.

I am not sure what the problem is, as i can see by debugging that I have 27 results back.

2). Also, for some reason I only get 5 suggestion back. I tested this in the services directory, and I only get 5 back there. Again, not sure why (created a ticket for this problem, but haven't been contacted yet).

Frieda,

Even if your Feature Layers comes from a 10.3 service, suggestion will not work if your underlying database does not support pagination. Show stopper.

I did find a workaround, but it gets complicated working with polygon and line layers.

I created address locators ("Single Field") from the layer of interest, enabled suggestion on the locator, and then enabled suggestion on the geocoding service. The suggestions work, but the returned geometry is a point.

If anybody has more experience with the Search widget, and knows how to fix 1) and 2), please, share your knowledge.

Thank you!

0 Kudos
FriedaL
New Contributor II

Hi Darina, thanks for your input on pagination. Will have a look at it.
For your first problem it would be good if you could post some code so we could have a look at it.

0 Kudos
FriedaL
New Contributor II

I had tried both options (and just double-checked again) but they are not working. I never get into the function to print out anything. The search is clearly working since I will get notification if no such result exists but nothing happens if there is a result.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Frieda,

   Sorry I did not notice the event you were listening to before. The proper result name is "search-results" (notice the "s") not "search-result".

0 Kudos
DarinaTchountcheva
Occasional Contributor II

Hi Frieda,

Thank you for your reply.

Yes, that is what I observe, results come back but nothing happens if autoSelect is false.

My code is attached. (I just copied one of the ESRI samples, and plugged in my services, and added event listeners for debugging).

I am also attaching an image of the console showing 5 suggestion coming back and 27 results coming back, but no results list.

0 Kudos