Comments (9)
@dandv I can think of a couple ways to implement a preferential matching rule without even modifying the current API; why don't you start this in a branch and we can discuss?
from meteor-autocomplete.
Hey @mizzao, revisiting this. It's been a while and I don't have any brighter ideas at the moment other than sorting again the results client-side. I'd be happy to start a branch. What were the couple ways you had in mind?
from meteor-autocomplete.
I think what I was thinking with in the above comment was to push a custom sort field down with the specified publication function on the server, since it's already required to be a custom publication anyway. That way, the server could just pre-sort the results in whatever order and the client could use that order to display the results.
In the simplest case, the server can add a field that is 0
for things that start with the string and 1
otherwise, and the client-side can sort objects by that field, then the specified search field.
My hunch is that if the publication does sub.added
with the results in the right order, they won't even need to be re-sorted on the client. That seems to occur in the vast majority of cases. Can we just depend on that behavior and obviate the need to do client-side re-sorting?
from meteor-autocomplete.
Is there a way to achieve this with client side data?
from meteor-autocomplete.
@jfly I don't know what you mean, can you elaborate?
from meteor-autocomplete.
My understanding of how the preferential sorting works in the example is that the server builds a subscription by doing 2 Mongo queries: 1 requiring that the regex match the start of the field, and another which can match anywhere. The server then builds a query from those two that will return records in preferential order. See https://github.com/mizzao/meteor-autocomplete/blob/a437c7b464ad9e779da2ca15566a5b91cf603902/autocomplete-server.coffee
How would I do this with a simple local collection client side? I was able to achieve it by building a wrapper for my collection that acts like a collection (and does all the stuff the server does in the example above), but that feels kind of gross. I'm curious what the best way of achieving this is.
from meteor-autocomplete.
That description is out of date. That logic has been taken out, although you can write that in your own publication. @dandv has some code that implements that.
I don't see a "simple" way to do it other than to run the two queries sequentially, but if it's in-memory it should be pretty quick to do in the client.
from meteor-autocomplete.
Running two queries sequentially seems fine to me for what I'm doing. I just don't know what the best way of doing it is with the api meteor-autocomplete gives me.
Here's what I hacked together (I'm looking at the profile.name attribute of Meteor.users):
Template.foo.settings = function() {
var fakeCollection = {
find: function(selector, options) {
var allResults = Meteor.users.find(selector, options).fetch();
selector['profile.name'].$regex = "\\b" + selector['profile.name'].$regex;
var preferredResults = Meteor.users.find(selector, options).fetch();
var seenIds = {};
var arr = [];
var addResult = function(result) {
if(seenIds[result._id]) {
return;
}
seenIds[result._id] = true;
arr.push(result);
};
var i;
for(i = 0; i < preferredResults.length; i++) {
addResult(preferredResults[i]);
}
for(i = 0; i < allResults.length; i++) {
addResult(allResults[i]);
}
arr.count = function() { return arr.length; };
return arr;
}
};
return {
position: "top",
limit: 5,
rules: [
{
collection: fakeCollection,
field: "profile.name",
template: Template.userPill,
matchAll: true
}
]
};
};
Faking a collection feels pretty gross to me. Is there a better way of achieving this?
from meteor-autocomplete.
Bumping this issue because I'm still curious if this is the recommended solution. Thanks for the great library!
from meteor-autocomplete.
Related Issues (20)
- Support multiple collections for multiple input fields, with each collection maps to an input field? HOT 1
- Show nothing when no records found.
- Multiple field search & display 2 fields in textbox of same collection. HOT 4
- Search anywhere in the string tokenless using server collection
- inserting without \n
- 'Bottom' position rendering over field HOT 2
- autoSelect :false isn't working HOT 1
- Starting Autocomplete after the n-th letter HOT 1
- autocompete not displaying HOT 1
- retrive docuemnts on spcefic condition HOT 1
- Autocomplete (server-side configuration) not working HOT 3
- React instead of Blaze? HOT 4
- Display nothing when no matches HOT 2
- How can I search multiple fields from the same collection?
- search in nested array field HOT 1
- Problem when entering a very lengthy text.
- I need to return two fields and put in two diferent inputs
- Exception in template helper: AutoComplete.prototype.filteredList@http://localhost:3000/packages/mizzao_autocomplete.js?
- Preselect or set a default value
- Broken server side subscription on Meteor 2.0 HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from meteor-autocomplete.