I'm trying to deep sort an API request via endpoint but it doesn't seems to work.
This is the structure:
article
- articles [a list of article entity]
- article_category
It just not sorting
/api/articles/5?populate[0]=articles.article_category&sort[1]=articles.title:desc
populate: articles.article_category
sort: articles.title:desc
It just not sorting
/api/articles/5?populate[0]=articles.article_category&sort[0]=articles.title:desc&sort[1]=articles.article_category.title:desc
populate: articles.article_category
sort: articles.title:desc
sort: articles.article_category.title:desc
How can I deep sort
Related
I have a application where the same data is present in many places in the graph and need to optimize the data queries to avoid processing and sending the same data too often.
As an example consider the following pseudo schema:
type Group {
name: String
members: [Person]
}
type Person {
name: String
email: String
avatar: Avatar
follows: [Person]
followedBy: [Person]
contacts: [Person]
groups: [Group]
bookmarks: [Bookmark]
sentMessages: [Message]
receivedMessages: [Message]
}
type Message {
text: String
author: Person
recipients: [Person]
}
type Bookmark {
message: Message
}
Querying a users data can easily contain hundreds, if not thousands, of Person-objects even though it the small circle of friends/contacts/follows only contains tens of distict users.
In my real implementation about 80% of each GraphQL query (in bytes) is redundant and considering that the client does many different queries in the same space above 90% of all data transferred and processed is redundant.
How could I improve the model so that I don't have to load the same data again and again without complicating the client too much?
I'm using Apollo for both GraphQL client and server.
Use/implement pagination (instead of just arrays) for relations - this way you can query for count/total (render it without array processing) and array of ids only - usually there is no need to query/join person table (DB) at all.
Render list of Person components (react?) using passed id prop only ... only rendered Person fetches for more details (if not cached, use batching to merge requests) consumed/rendered inside.
The docs for MultiEntry indexes say any Indexable Type can be used.
Arrays are indexable types. Therefore it should be possible to index arrays of arrays and so on, right?
However, I couldn't find any examples or figure out how to query these.
Basically, what I want is something like:
var db = new Dexie('dbname');
db.version(1).stores({
books: 'id, author, name, *properties'
});
db.books.put({
id: 1,
name: 'Human Action',
author: 'Ludwig von Mises',
properties: [['language', 'english'], ['subject', 'philosophy'], ['subject', 'economics']]
});
Then be able to find a book that has an economics subject.
That's right. Each entry in the properties array is an array of two strings - and that inner array itself is indexable and may act as an indexable entry.
So to find all books that has subject economics, do
db.books.where({properties: ['subject', 'economics']}).toArray()
or the equivalent form:
db.books
.where('properties')
.equals(['subject', 'economics'])
.toArray();
I'm using Amplify from AWS to build a small ecommerce project using React as frontend.
I'd like to know how I should write the "Product" and "Order" types in the schema in order to be able to write productId's to a product array in the Order table when users complete a purchase.
My schema.graphql file:
type Product #model {
id: ID!
name: String!
price: Int!
category: String!
images: [String]!
}
type Order #model {
id: ID!
products: [Product] #connection
}
My question is about the last line, do I need to define that [Product] connection there or I can use [String] to store product id's in a simple string array?
Point 1: In dynamoDB, you only need to define the data type of your partition key and sort key, and these can be string, number etc. For all the other attributes, you don't need to define anything.
Point 2: The dynamoDB designers prefer using a single table per application, unless it's impossible to manage data without multiple tables. Keeping this in mind, your table can be something like this.
Please observe: Only Id aka partition key and Sk aka sort key column is fixed here, all other columns can be anything per item. This is the beauty of DynamoDB. Refer to this document for dynamoDB supported data types.
When simply displaying large amounts of data (over 100k records) my code works well, and I paginate on the server.
However, when I need to sort this data I'm stuck. I'm only sorting on the page, and NOT sorting on ALL the records related to this one customer.
How can I paginate but also sort across all the records of my customer and NOT simply sort the records returned from the server side pagination?
I'm also using BootStrap Table to display all my data.
Here is my code that gets all the customers:
def get_customers
#data_to_return = []
#currency = current_shop.country_currency
customers = current_shop.customers.limit(records_limit).offset(records_offset)#.order("#{sort_by}" " " "#{sort_order}")
customers.each do |customer|
#data_to_return.push(
state: false,
id: customer.id,
email: customer.email,
accepts_marketing: customer.accepts_marketing,
customer_status: customer.customer_status,
tags: customer.tags)
end
sort_customers
end
And then this is the sort_customers method:
def sort_customers
fixed_data = data_to_return.sort_by {|hsh| hsh[sort_by]}
customer_size = current_shop.customers.length
if sort_order == "ASC"
fixed_data
else
fixed_data.reverse!
end
render json: {"total": customer_size, "rows": fixed_data}
end
In the above code you can see that data_to_return is coming from get_customers and its limited. But I don't want to return ALL the customers for many reasons.
How can I sort across all the records, but only return the paginated subset?
You should actually sort at the model/query level, not at the ruby level.
The difference is basically:
# sort in ruby
relation.sort_by { |item| foo(item) }
# sort in database - composes with pagination
relation.order('column_name ASC/DESC')
In the first case, the relation is implicitly executed, enumerated and converted to array before calling sort_by. If you did pagination (manually or with kaminari), you will get just that page of data.
In the second case, you are actually composing the limit, offset and where (limit and offset are anyways used under the hood by kaminari, where is implicit when you use associations) with a order so your database would execute
SELECT `customers`.`*` FROM `customers`
WHERE ...
OFFSET ...
LIMIT ...
ORDER BY ...
which will return the correct data.
A good option is to define scopes in the model, like
class Customer < ApplicationRecord
scope :sorted_by_email, ->(ascending = true) { order("email #{ascending ? 'ASC' : 'DESC'}") }
end
# in controller
customers = current_shop.customers.
limit(records_limit).
offset(records_offset).
sorted_by_email(false)
You can resolve sorting and paginate issue using Data Tables library, which is client side. It's a Jquery library. Using this you need to load all data into page, then it would work very well.
Below are the references please check.
Data tables jquery libray
Data tables gem for rails
You can try these, they will work very well. You can customise it as well
If the answer is helpful, you can accept it.
I have question about Solr 7.0.0 and doing sort operations in combination with [subquery]:
I have two collections:
Collection A: for static data
Collection B: for data that is frequently updated, it has the same ID's as collection B.
The results of both collections i combine via fl=*,foo:[subquery]. The resulting response looks something like this:
{
"twda_names":["TWA"],
"id":"TWA-2017-000",
"file_loc":["Other"],
"file_name":["Other"],
"file_type":["Other"],
"doc_type":["Other"],
"_version_":1583236633578176512,
"twda_names_str":["TWA"],
"foo":[
{
"views":21,
"id":"TWA-2017-000",
"_version_":1583236633716588544}]},
{
"id":"TWA-2017-001",
"twda_names":["TWRWRW"],
"file_loc":["Other"],
"file_name":["Other"],
"file_type":["Other"],
"doc_type":["Other"],
"_version_":1583237084210003968,
"twda_names_str":["TWRWRW"],
"foo":[
{
"id":"TWA-2017-001",
"views":99,
"_version_":1583237084347367424}]}]
Now i would like to sort based on the field views, but cant seem to get it working..
What i have tried so far:
&sort=faa:[subquery]asc and specifying &faa.q etc .... results in a error
&sort=childfield(views) ... error
&sort={!func}query($qq) and specifing &qq={!dismax qf=id} etc ... error
Any suggestions are much appreciated!
Thanks, Tim
no way. you can probably use scoring in query time {!join ..}.. see https://lucene.apache.org/solr/guide/7_1/other-parsers.html#join-parser-scoring