Code Monkey home page Code Monkey logo

Comments (9)

mizzao avatar mizzao commented on May 21, 2024

@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.

dandv avatar dandv commented on May 21, 2024

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.

mizzao avatar mizzao commented on May 21, 2024

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.

jfly avatar jfly commented on May 21, 2024

Is there a way to achieve this with client side data?

from meteor-autocomplete.

mizzao avatar mizzao commented on May 21, 2024

@jfly I don't know what you mean, can you elaborate?

from meteor-autocomplete.

jfly avatar jfly commented on May 21, 2024

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.

mizzao avatar mizzao commented on May 21, 2024

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.

jfly avatar jfly commented on May 21, 2024

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.

jfly avatar jfly commented on May 21, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.