How to shuffle result from query in Laravel? - laravel-5

I get result of query and try shuffle this.
I tried to use shuffle in php, but it works only for arrays.
Also I can use random() in query, but it is slow

see this pls
Laravel Collection method shuffle

Related

Limit number of results by Max Score's 80% and above(Please Check Body) in SOLR

Lets say my MaxScore is 500.
Then I want results with 500's 80% and above Score results only .
How can I achieve that?
I would suggest to use _score as sorting by descending order and apply the limits by using start and rows.
?q=test&&start=0&rows=10&fl=_score
The other option is to use the function range query parser by reading the documentation provided at solr function range query
for example :
1. {!frange l=400 u=500}nameOfField
2. ​fq={!frange l=400 u=500} sum(field1,field2)
Function range query parser is the better way. If you use facets you get the right result count of facets.
This example is meant to be supplementary if edismax is used as defType
fq={!frange l=400}query({!edismax v=$q})

How can I get the total amount of assets use Query function?

As the tutorial of Query, I can use statement to query assets, participant like sql. But my question is can I query the assets use Count() to get the length of a specified assets?If it is not effective, how can I get the total amount or latest element of the assets?
Composer query language offers a very limited set of queries. An overview can be found at Hyperledger Composer Query Language.
There you may see that this query language does not support any aggregation functions like count, sum, max, min, ... yet.
The consequence is that you need to execute a query which filters your data properly to avoid huge arrays and then apply the respective aggregation in JavaScript as shown in the link david_k has given.
For the latest element it might be e.g. possible to sort by id and then read the "last" entry of the result.

How To Handle Pagination And Ordering In Cypher Query?

I am writing a cypher query in neo4j that merge few seperate queries with'UNION' operator. Now, the final result need to be sorted according to a specific column and I also need to be able to use 'LIMIT' to the final result so that I can fetch based on pagination request.
How to achieve that? adding 'ORDER BY' or 'LIMIT' at the end doesn't seems to work. Can't find a way to wrap the result as temporary set(as in sql queries) either. Any suggestions?
Currently it is not possible to add a ORDER BY or SKIP/LIMIT clause to the global set of UNION.
This is a pending feature request, see https://github.com/neo4j/neo4j/issues/2725
Maybe you can share your query and we can find a way to do it without UNION ?

Mongomapper sorting

I am getting confused with mongomapper + sorting.
I have a test blog with data with posts like this:
http://www.mongodb.org/display/DOCS/Schema+Design
Now, I would like to show just comments sorted by time asc for each blog entry.
If I use where, I get Plucky, when I use all() I get Array...how does this work?
The answer you linked to is sorting your result set in memory, which is expensive and likely going to cause errors if you have more comments than you can display in one batch.
The right way to sort this is using plucky's sort() method on the query, by created_at, descending (newest first):
post.comments.sort(:created_at).each {|comment| do_awesome_stuff(comment) }
(assuming you have a Post model that has_many comments and you're using the built-in created_at field on the comments for time.
Mongomapper uses a chaining query/filter model like ActiveRecord's scopes, which is why where() returns a chain-able Plucky query, as does sort(). You can then convert that into an array of results or chain more sorts/filters. You can also use enumerators like each() directly on the plucky query.
The code in the example above is doing the following chained calls:
Return a plucky query filtering all comments for this post object
Update that query to sort the comments returned by created_at, descending
Fetch a group of results from the db, and yield them to your code via each()

Better to use DQL for getting Column Count or Get Collection Then Count?

I am quite sure that DQL will be the way to go, but I am wondering if Doctrine, i am using Doctrine 2, has someway to return the row count. I won't be using the rows itself, I just want the count.
I'm new to Doctrine2 but it looks like you can simply do this:
$query = $em->createQuery('SELECT COUNT(u.id) FROM Entities\User u');
$count = $query->getSingleScalarResult();
Source (Using Agregate Functions): http://www.doctrine-project.org/docs/orm/2.0/en/reference/dql-doctrine-query-language.html#dql-select-examples
Allowed aggregate functions: http://www.doctrine-project.org/docs/orm/2.0/en/reference/dql-doctrine-query-language.html#aggregate-functions
Here is another interesting point of view about using aggregated functions in DQL
http://doctrine-orm.readthedocs.org/en/latest/cookbook/aggregate-fields.html
Maybe you would avoid the creation of an specific query to obtain an aggregate value. In this case, aggregate fields are a good alternative.

Resources