Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug 1449038 - Add a simple weighted search to KeywordService #317

Merged
merged 1 commit into from
Mar 19, 2018

Conversation

spadgett
Copy link
Member

@openshift-ci-robot openshift-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Mar 12, 2018
@spadgett spadgett force-pushed the weighted-search branch 2 times, most recently from 294b56b to 5ae26ab Compare March 16, 2018 13:49
@spadgett spadgett changed the title [WIP] Bug 1449038 - Add a simple weighted search to KeywordService Bug 1449038 - Add a simple weighted search to KeywordService Mar 16, 2018
@openshift-ci-robot openshift-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Mar 16, 2018
@spadgett
Copy link
Member Author

/assign @benjaminapetersen

PTAL

@benjaminapetersen
Copy link
Contributor

reviewing

Copy link
Contributor

@benjaminapetersen benjaminapetersen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor things, mostly wondering if we want it to handle both w & w/o weights.

if (_.isEmpty(keywords)) {
return [];
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: can we use _.reduce( ) or _.flatten(_.map()) vs manually creating [ ] and _.each() to .push()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you give some example code?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Outermost:

var weightedSearch = function(objects, filterFields, keywords) {
  if(_.isEmpty(keywords)) {
    return [];
  }
  
  var results = _.reduce(objects, function(result, object) {

    // do scoring work .....
    
    if (score > 0) {
      results.push({
        object: object,
        score: score
      });
    }
    return result;
  }, []);

  // Sort first by score, then by display name for items that have the same score.
  var orderedResult = _.orderBy(results, ['score', displayName], ['desc', 'asc']);
  return _.map(orderedResult, 'object');
  
}

Could possibly do the score and matchesKeyword similarly, but def isn't a huge deal.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I'm not clear on the benefit.

Since _.reduce can change result to anything, you have to read the code carefully to see that it's just appending values to an array. I feel like _.each / push is easier to understand.

Copy link
Contributor

@benjaminapetersen benjaminapetersen Mar 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I prob should not have suggested reduce over like _.compact(_.map(objects)), its too general purpose.

Its prob just the imperative vs declarative discussion... I'd rather not see the "noise" of manually making the new array(s) when we have a number of good functions that do that for us.

That said, is def a (subjective) nit, and not a huge issue 😄

@@ -107,4 +107,94 @@ describe('KeywordService', function() {
expect(result).toEqual(mockData);
});
});

Copy link
Contributor

@benjaminapetersen benjaminapetersen Mar 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

big pts for tests! much easier to review & figure out what the new stuff is doing.

}
};

it('should honor field weights', function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So is this the kind of configuration we might want to do once up front, via $provider in app.js .cofig(), or would we want to custom config weights & paths for every use?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ie, config some functions & cache them, so we don't have copy / paste repeats of the setup in multiple places. I don't know how many times we will use this search feature.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will vary by the kind of resource and the context, so I think this has to be up to the caller.

});

it('should match all keywords', function() {
var keywords = KeywordService.generateKeywords('maria db');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe unrelated, but wonder if .generateKeywords should take either 'string of multiple' or ['array','of','multiple'].

var orderedResult = _.orderBy(results, ['score', displayName], ['desc', 'asc']);
return _.map(orderedResult, 'object');
};

return {
filterForKeywords: filterForKeywords,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have both a search and a weightedSearch? or could weightedSearch be used w/o weights, just with keyword and objects (perhaps if paths was last & optional)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filterForKeywords is basically weightedSearch without the weights

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose naming symmetry. Prob not big deal.

Copy link
Contributor

@benjaminapetersen benjaminapetersen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

I think all my questions answered, def looks good!

@openshift-ci-robot openshift-ci-robot added the lgtm Indicates that a PR is ready to be merged. label Mar 19, 2018
@spadgett spadgett merged commit 9abeb9c into openshift:master Mar 19, 2018
@spadgett spadgett deleted the weighted-search branch March 19, 2018 16:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm Indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants