I'm using Stack Exchange Data Explorer and trying to switch from Web Applications to Stack Overflow.
I enter sta
into "Switch sites" field, but search showing unrelated results first (3D Printing, 3D Printing Meta, Software Engineering ...
):
Meta Stack Exchange is a question and answer site for meta-discussion of the Stack Exchange family of Q&A websites. It only takes a minute to sign up.
Sign up to join this communityI'm using Stack Exchange Data Explorer and trying to switch from Web Applications to Stack Overflow.
I enter sta
into "Switch sites" field, but search showing unrelated results first (3D Printing, 3D Printing Meta, Software Engineering ...
):
In query.siteswitcher.js
the function formatMatch
is defined as follows:
formatMatch: function (item) {
return htmlEncode(item.LongName + " " + item.Url);
},
It will take from the /sites JSON result the attributes LongName and Url as the data to be searched.
The Url
attribute is the one that doesn't play nice here as it contains the url for each site which by definition contains almost always the word stackexchange, therefor matching most items in the list.
Why it selects the few items it did is an implementation detail that can be found in autocomplete,js, specially the populate()
function but I leave the analysis of that to the reader.
I would argue it is not a bug but by design.
That said: you're free to offer better logic for that function to match search terms against the collection of site items.
One option to fix this:
formatMatch: function (item) {
return htmlEncode(item.LongName + " " + item.Url.replace('stackexchange.com',''));
},
but that might need extra care for meta.stackexchange.com? And by doing this you can never switch on the fully qualified dns name of the site. (unless someone posts a feature request for that)
Two things discovered from rene's answer:
LongName
and site's Url
, that's the reason sites with .stackexchange.com
in url are found by sta
search;Possible solution is to use weighted search for two site's fields LongName
and Url
- if search term will be found in site's LongName
, this site will receive more weight "points" and will appear higher in autocomplete list.
But as jQuery Autocomplete plugin has no support of it (and by the way, have been deprecated long time ago, 2010-06-23), it may be effective to use instead search library like Fuse.js:
var sites = [{
'Url': 'https://3dprinting.stackexchange.com',
'LongName': '3D Printing'
},
{
'Url': 'https://sports.stackexchange.com',
'LongName': 'Sports'
},
{
'Url': 'https://stackoverflow.com',
'LongName': 'Stack Overflow'
}];
var options = {
keys: [{
name: 'Url',
weight: 0.5
},
{
name: 'LongName',
weight: 1
}]
};
var fuse = new Fuse(sites, options)
var res = fuse.search('sta');
// Stack Overflow > Sports > 3D Printing
console.log(res);
<script src="https://cdnjs.cloudflare.com/ajax/libs/fuse.js/3.0.4/fuse.min.js"></script>