Select to view content in your preferred language

How could I get the suggestions from SearchViewModel?

307
2
11-28-2024 10:21 PM
TedZhou
Occasional Contributor

Dear Sir/Madam:

I intend to get the suggestions from a SearchViewModel with multiple search resources with 4 FeatherLayers (ActiveSourceIndex = -1).

 

I tried to do in the following way:

                    const suggestResults: __esri.SearchViewModelSuggestResponse =
            await searchVM.suggest(searchTerm);

        let isEmpty = true; // Are suggestions empty? 
        for (let i = 0; i < suggestResults.numResults; ++i) {
            if (!suggestResults.results) continue;
            if (!suggestResults.results[i]) continue;
            const suggestions = suggestResults.results[i].results;
            console.error('Number of Suggestions = ' + suggestions.length);
            if (suggestions.length > 0) {
                isEmpty = false;
                // Yes, got it, This is what I intend to get from SearchViewModel
            }
        }

 

It works for short search term (less than 11 or 12 characters probably).

But if the search term is more than 12 characters long, I got no suggestions from SearchViewModel.suggest() call. 

 

However, If I use the ArcGIS's SearchWidget to hook to the same search sources, SearchWidget always could get suggestions even the search term is more than 12 characters or longer.

 

I used SearchWidget's SearchViewModel to do SearchWidget.viewModel.suggest(searchTerm), i got the same results as I did from my SearchViewModel, it will got no suggestions when the search term is longer than 12 characters or longer.

 

But ArcGIS's SearchWidget works perfectly 

TedZhou_0-1732860811790.png

While I used SearchWidget.viewModel, I could only get suggestion for "LANE COVE B" or shorter.

 

Probably SearchWidget get the suggestions not from SearchViewModel.suggest(searchTerm)?

 

I suspect ArcGIS's SearchWidget just listen to the 'suggest-complete' event, and parse the event[] and get the suggestions as below:

        searchWidget.on('suggest-complete', function (event) {
            // The results are stored in the event Object[]
            console.error('Results of suggest: ', event.numResults);
            console.error('event source index = ' + event.activeSourceIndex);
            console.error('event search term  = ' + event.searchTerm);
            console.error('event results number = ' + event.results.length);
            if (event.results.length > 0) {
                for (let i = 0; i < event.results.length; ++i) {
                    console.error(
                        'evernt results[' +
                            i +
                            '].results.length = ' +
                            event.results[i].results.length
                    );
                    if (event.results[i].results.length > 0) {
                        console.error(
                            'TEXT = ' + event.results[i].results[0].text
                        );
                        console.error(
                            'KEY = ' + event.results[i].results[0].key
                        );
                        console.error(
                            'SOURCE Index = ' +
                                event.results[i].results[0].sourceIndex
                        );
                    }
                }
            }
        });

 

I am not why SearchViewModel does not work for long search term?

 

Is relevant server configuration? Or my SearchViewModel not properly configured? or suggest(searchTerm) has different arguments to call?

 

Your kind assistance would be greatly appreciated.

Ted

 

 

 

 

0 Kudos
2 Replies
TedZhou
Occasional Contributor

1. Not sure why we need to hook on listener to get suggestions? Is it a good design or legacy C style design?

2. And the suggestions from listeners are not always the same as from 

__esri.SearchViewModelSuggestResponse =
            await searchVM.suggest(searchTerm);
 
How could we elegantly use ArgGIS's get suggestions from SearchViewModel.suggest(...)?
 
I am a little confused now.
 
Ted
 
 
0 Kudos
TedZhou
Occasional Contributor

How the searchWidget implemented the search with multiple search sources?

1. Using listener? Probably not since I did implement as listener, I still failed to get a few suggestions in some case.

2. Parsing the response when set active source index = -1? When term is more than 12 characters, some case failed to get suggestions?

3. Parsing the responses when set active source index one by one? Not try this way yet/

4. How could I control those workers when call search view model's suggest methods? Using AbortController?

 

Thanks for your tips,

 

Ted

 

0 Kudos