ElasticSearch sort by id's in array - sorting

Is there a way to sort some elasticsearch response in the same direction, which I am posting an array with ids?
Example: array[23,45,67] and the results should be sort in the same way like the id's are: first come all rows with ID 23, after that all rows with ID 45 and at the end all rows with ID 67 ?
Thanks
Nik

You can use scripting in sort or the other option is to use bool - should query where you boost documents with these values.

Related

How to sort in laravel eloquent?

When i sort it gives me data like this:
10
19
100
20
abc
fff
How can i properly sort it like this:
10
19
20
100
abc
fff
Model::orderBy('text','asc')->get();
The sortBy method sorts the collection by the given key. The sorted collection keeps the original array keys :
$q = Model::orderBy('text','asc')->get();
$sorted = $q->sortBy('text');
Alternatively, you can use sortByDesc() method. sortDesc()
method will sort the collection in the opposite order as the sort() method.
you need to declare the field in your table as an Integer and not as a String.
If a field is of String type its values are evaluated like this:
"100" < "20"
But if the field is of Integer type, eloquent evaluate like this:
20 < 100
Hi.
You can also use sortBy to sort your data with passing closer to your query, have added an example you can use it.
class::all()->sortBy(function ($query){
return $query->Your_Field_Name;
});
It will also depend on what data type you have chosen in your migration.
you can sort based on the column if it can be casted to UNSIGNED number, otherwise sort by the column it self:
Model::orderByRaw('CAST(text AS UNSIGNED), text')->get();

How do you write a query that returns an ordered list starting from a specific item?

If you have table with {1,3,4,5,9,15,43} how do you write a query that returns the three items following "5" in the natural sort order?
db.table
.Where(x=>x.field>5)
.OrderBy(x=>x.field)
.Take(3);

OFFSET/LIMIT only count DISTINCT values in Activerecord query

I am running this query
Playlistship.order("created_at desc").select("distinct playlist_id").limit(12).offset(2)
This query does not necessarily return 12 records. It returns the number of distinct records in the set of 12 defined by the LIMIT, OFFSET and ORDER parameters.
For example if the Playlistships between id=13 and id=24 had playlist_ids of [2,3,3,5,6,3,5,6,8,11,12,12], then this query will only give return 7 records, corresponding to the first ones having the playlist_ids [2,3,5,6,8,11,12].
What I would like to find is a query that yields 12, records with distinct playlist_ids, with the correct offset so that running this query again with an OFFSET of 3 would yield the next 12 records with distinct playlist_ids.
Hopefully I didn't "over explain" this one, as I think it's a relatively straightforward question. Please ask for more details if you need them.
Thanks!
Have you tried with subqueries? Give this a try:
Playlistship.select("distinct playlist_id").limit(12).where(playlist_id: Playlistship.order("created_at desc").select('playlist_id').offset(2))

How to retrieve the last 100 documents with a MongoDB/Moped query?

I am using the Ruby Mongoid gem and trying to create a query to retrieve the last 100 documents from a collection. Rather than using Mongoid, I would like to create the query using the underlying driver (Moped). The Moped documentation only mentions how to retrieve the first 100 records:
session[:my_collection].find.limit(100)
How can I retrieve the last 100?
I have found a solution, but you will need to sort collection in descending order. If you have a field id or date you would do:
Method .sort({fieldName: 1 or -1})
The 1 will sort ascending (oldest to newest), -1 will sort descending (newest to oldest). This will reverse entries of your collection.
session[:my_collection].find().sort({id:-1}) or
session[:my_collection].find().sort({date:-1})
If your collection contain field id (_id) that identifier have a date embedded, so you can use
session[:my_collection].find().sort({_id:-1})
In accordance with your example using .limit() the complete query will be:
session[:my_collection].find().sort({id:-1}).limit(100);
Technically that query isn't finding the first 100, that's essentially finding 100 random documents because you haven't specified an order. If you want the first then you'd have to say explicitly sort them:
session[:my_collection].find.sort(:some_field => 1).limit(100)
and to reverse the order to find the last 100 with respect to :some_field:
session[:my_collection].find.sort(:some_field => -1).limit(100)
# -----------------------------------------------^^
Of course you have decide what :some_field is going to be so the "first" and "last" make sense for you.
If you want them sorted by :some_field but want to peel off the last 100 then you could reverse them in Ruby:
session[:my_collection].find
.sort(:some_field => -1)
.limit(100)
.reverse
or you could use use count to find out how many there are then skip to offset into the results:
total = session[:my_collection].find.count
session[:my_collection].find
.sort(:some_field => 1)
.skip(total - 100)
You'd have to check that total >= 100 and adjust the skip argument if it wasn't of course. I suspect that the first solution would be faster but you should benchmark it with your data to see what reality says.

Sorting on a count field in web2py

I have the following code in web2py. I am trying to retrieve how many types of items I have in the table and a count of how many of each of them there are.
count = db.table.field1.count()
rows=db((some criteria).select(db.table.field2, count, groupby=db.table.field2)
print rows
The print of this is:
table.field2, COUNT(table.field1)
4,3
6,4
9,2
Now I would like to sort from high to low by the count field, so the outcome would be:
6,4
4,3
9,2
What's the best way to do it? rows=rows.sort(lambda row: row.COUNT(table.field1)) did not work for me.
Instead of row.COUNT(table.field1), use row['COUNT(table.field1)'] or just row[count] (see here).
Note, you can also have the database do the sorting using the orderby argument:
rows = db(query).select(db.table.field2, count,
groupby=db.table.field2, orderby=count)
And for descending order: orderby=~count

Resources