Code Monkey home page Code Monkey logo

Comments (14)

kfirba avatar kfirba commented on September 21, 2024

I actually thinks that this is a desired behaviour. You want to sort the results based on Relevance first. Imagine that there were 100 records and the relevance would be anything from 0.75-10 and you want to sort them alphabetically, you may end up getting the 0.75 relevance results in higher position than the 10 relevance results.

from eloquence.

andynoelker avatar andynoelker commented on September 21, 2024

I think it should default to this behavior if no orderBy() is in the query, but if one is explicitly declared then it should abide by that one. If you still desire this behavior but also need a different orderBy() in your query, then all you would have to do is add both of them in there:

Item::search('test')->orderBy('relevance')->orderBy('name')->get()

Yes, this would mean that items with a lower score would be listed above items with a higher score, but if you are adding an orderBy() into your query, then it means that you want a different way of ordering things to begin with.

Think about it - this means that the orderBy method is effectively pointless as you will never actually be able to truly order your results without sorting by relevance first.

For instance, imagine you have some search results in your app. You might have a drop-down menu that allows the user to either sort those results by relevance or alphabetical order. Sure, if it's only 10 results you can probably figure out some client-side solution, but imagine there are 10,000 results and you're using pagination. It is literally impossible to order results alphabetically (or by any other means that is not relevance). I think this would actually be a fairly common scenario. I certainly need that option for my own app.

from eloquence.

jarektkaczyk avatar jarektkaczyk commented on September 21, 2024

The idea of search here is to provide the results by relevance. Think about google - is there any way to order the results by anything else but relevance? No. You can filter or narrow the results, but not reorder them.

The same applies here. The only benefit from what you suggest would be getting the results with relevance higher than X - I don't see application for this. On the other hand, orderBy is not pointless at all - imagine you have 50 entries with the same score - this is when you want additional ordering applied.

This is where it happens : https://github.com/jarektkaczyk/eloquence/blob/5.1/src/Builder.php#L158-L160

If you really need to override this behaviour, fork and change these lines for use in your app.

from eloquence.

joey-sexton avatar joey-sexton commented on September 21, 2024

I agree with @andynoelker for multiple reasons. One being that this "Easy and flexible extensions for the Eloquent ORM" should be just that, flexible. Limiting the order by to relevance is not flexible. If an application needs to sort by "relevance" and then "name", do as @andynoelker stated

Item::search('test')->orderBy('relevance')->orderBy('name')->get()

Second, Google may not allow sorting by anything but relevance but that is one example. Look at ThinkGeek; they allow you to search for "Zombie" products and then sort by "Price: Low to High". This is not possible with the extension in its current state.

I am merely asking you to reconsider as the community, and I, enjoy this extension and would like to keep using it and see it grow. Thanks!

from eloquence.

andynoelker avatar andynoelker commented on September 21, 2024

@jarektkaczyk, thank you for your fast response and also providing the relevant code section!

As @joey-sexton mentions above, it's not too difficult to find many online stores that offer this type of functionality. I also need it for an online store. It might be sorting by price. It might be sorting by date the items were added to the catalog. Sometimes people search for something but they only have very vague details about what they are looking for and type in something really generic. Every bit of filtering/sorting they can get will help them find the product they are looking for.

Sure, in an ideal world every search term would return the correct items with an identical score, but the world is not ideal :) Maybe you're adding this to a database of items that is several years old. During the years, you've changed your convention for how you describe products, which lead to some products getting scored higher than others simply because they were added with this new convention. If you're looking for an older product and want to order the results by date, you might have to go through 5,000 results that were scored higher simply because of the newer description, just to make it to the next tier of relevance scoring that actually contains the older items.

I can take a stab at trying to add this functionality myself (well really just make orderBy() do what it's always done). I had assumed this would be a pretty simple thing, but maybe that was naive of me. @jarektkaczyk, would you want me to make this a PR when I have it figured out, or would you rather this extra flexibility stay out of the main repo?

from eloquence.

jarektkaczyk avatar jarektkaczyk commented on September 21, 2024

I never intended to use searchable functionality this way, but I can see where you're coming from.

It doesn't require any hard work to make it happen, so create a PR with a test.

from eloquence.

zawilliams avatar zawilliams commented on September 21, 2024

@andynoelker - have you done any work on this by any chance?

from eloquence.

andynoelker avatar andynoelker commented on September 21, 2024

@zawilliams Hey sorry, the needs of our project changed and we're no longer using Eloquence so I never got around to making this change. There is the possibility I would someday return to figure out this change as I think it is a very helpful one, but I can make no promises when that would be. However, Jarek links to the relevant file further up in the conversation so definitely feel free to make your own PR if you're feeling up to it!

from eloquence.

zawilliams avatar zawilliams commented on September 21, 2024

@andynoelker thanks for the info Andy. I actually ended up not using Eloquence either a day or so after you responded as the search wasn't working as intended. We ended up doing some more simplified SQL queries. If I come back to this I'll submit a PR!

from eloquence.

migueloliveira avatar migueloliveira commented on September 21, 2024

@zawilliams @andynoelker @joey-sexton @kfirba I use searchable in a couple projects and there's one of them I needed to do something like you describe. My solution isn't the best one but in order to avoid forking @jarektkaczyk repository I made a simple hack. Something like:

$results = MyModel::whereActive(1)->search($string, null, false)->take(10);
$query = $results->getQuery();
$query->orders = array_prepend($query->orders, ['column' => 'name', 'direction' => 'ASC']);
$results = $results->setQuery($query)->get();

I agree with @jarektkaczyk that the default behaviour should be ordering results by relevance but hope this helps for an exceptional usage

from eloquence.

clin407 avatar clin407 commented on September 21, 2024

Anyone have a simple solution to this? My situation is similar to those above where search returns a series of results which I then need to sort. Having it sort by relevance first when i want to sort the names by alphabetical order makes the results look really off.

from eloquence.

zawilliams avatar zawilliams commented on September 21, 2024

@clin407 - I ended up not using this in the end because of the relevance issue. Rolled my own scope using the query builder and it worked much better for what I needed.

from eloquence.

clin407 avatar clin407 commented on September 21, 2024

@zawilliams possible to share your solution?

from eloquence.

zawilliams avatar zawilliams commented on September 21, 2024

@clin407 - I can't share the exact solution but I created a local scope on a model like this: https://laravel.com/docs/5.2/eloquent#local-scopes

My solution looked something like:

public function scopeSearch($query, $search) {
    return $query->where(function($query) use ($search) {
        // do advanced stuff here with the search value
    });
}

from eloquence.

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.