Elasticsearch sorting by relationship - ruby

I've defined relationship like this
PUT /my_index/user/1
{
"name": "John Smith",
"email": "john#smith.com",
"dob": "1970/10/24"
"number_of_posts": 4
}
PUT /my_index/blogpost/2
{
"title": "Relationships",
"body": "It's complicated...",
"user": 1
}
How can I sort blogpost by user information like name, email or number_of_posts without using nested field on blogpost, because I don't want to update all blogpost with user = 1 when user change their information. Thanks!

Related

Craft CMS category count by slug

I created categories in craft and I'd like to get total amount of entries related to individual category. Right now I have a query that returns my categories:
query categoriesQuery {
categories(group: "projectCategories") {
id
slug
title
}
}
but I would also like for each category to return the amount of related entries.
So I would like to add another field that counts related entries - I wanted to do somethings like this, but it won't work:
query categoriesQuery {
categories(group: "projectCategories") {
id
slug
title
_count(relatedToEntries(section: "projects"))
}
}
Is there any way to get amount of entries related to single category? The result that I'm trying to achieve would look somethings like this:
{
"data": {
"categories": [
{
"id": "1",
"slug": "Category-1",
"title": "Category 1",
"amountOfRelatedEntries": 1
},
{
"id": "2",
"slug": "Category-2",
"title": "Category 2",
"amountOfRelatedEntries": 2
},
{
"id": "3",
"slug": "Category-3",
"title": "Category 3",
"amountOfRelatedEntries": 3
},
]
}
}

Idiomatic way of eager loading - how to populate deep nested association with database/sql and pq

I was just wondering how to deal with eager loading queries to retrieve data with several associations, let me explain my models:
type User struct {
Id uint
Name string
Username string
Image string
}
type Post struct {
Id unit
Content string
Author *User // I use user_id in author column for the posts table
Comments []Comments
}
type Comments struct {
Id unit
PostId uint
Message string
CommentBy *User // I use user_id in comment_by column for the comments table
}
This is an example of retrieved data:
{
"Posts": [
{
"Id": 1,
"Content": "test post",
"Author": {
"Id": 1,
"Name": "author",
"Username": "h3ll0",
"Image": "author.png"
},
"Comments": [
{
"Id": 1,
"PostId": 1,
"Message": "good article",
"CommentBy": {
"Id": 2,
"Name": "second user",
"Username": "r3ader",
"Image": "reader.png"
}
},
{
"Id": 2,
"PostId": 1,
"Message": "bad article",
"CommentBy": {
"Id": 3,
"Name": "third user",
"Username": "thirD",
"Image": "third.png"
}
}
]
}
]
}
I want to retrieve the post data with all nested associations and map them to the Post struct. How would you do it with database/sql and pq (Postgres driver)?
I don't want to use any ORM at all, I only use database/SQL and SQLx.
Keep in mind that the performance is important because The posts and comments tables are big enough.

Laravel hasOne get only one field instead of object

There two models user and also address, which contains country, city and etc. I need to get a list of users with the city, not with the whole address. Relation is oneToOne. The only thing I can get, using select['user_id', 'city']:
{
"id": 1,
"name": "John",
"city": {
"user_id": 3,
"city": "Paris"
},
but I need:
{
"id": 1,
"name": "John",
"city": "Paris"
}
Of course, I can use a loop and do something like $user->city = $user->address->city but maybe there is a better way to solve this problem. Laravel 5.4
You can use Accessor,
And append the attribute to json appending-values-to-json:
In your User model:
protected $appends = ['city'];
public function getCityAttribute()
{
return $this->city->city;
}
Test it like this:
User::with('city')->get()->toJson();
// It will return:
// [{"id": 1, "name": "John", "city": "Paris", "city" : {"user_id": 3, "city": "Paris"}}, {...}]

How to structure index in ElasticSearch

Let's say I have users index and blogs index. Now I want to enable users to create posts on by blog.
My first thought is that this is how my index should look like:
{
"title": "Blog post title",
"content": "Post text",
"user": {
"username": "lala",
"name": "Johnny",
"birthday": "1991-29-12"
}
}
Which looks good. But what if user changes his name and birthday. Then I need to go through all blog posts and update "user" object?
What if that needs to happen with multiple indexes?
How should I store data like this?

RethinkDB: How to do recursive joins on three tables?

I am developing a platform with JSON API using Python Flask. In some cases I need to join three tables. How to join tables with a array of IDs gave me some guidance but I need a solution beyond it.
Let's assume we have three tables for a messaging app.
Accounts
Conversations
Messages
Message Readers
Accounts table snippet
{
"id": "account111",
"name": "John Doe",
},
Conversations table snippet
{
"id": "conversation111",
"to": ["account111", "account222", "account333"], // accounts who are participating the conversation
"subject": "RethinkDB",
}
Messages table snippet
{
"id": "message111",
"text": "I love how RethinkDB does joins.",
"from": "account111", // accounts who is the author of the message
"conversation": "conversation111"
}
Message Readers table snippet
{
"id": "messagereader111",
"message": "message111",
"reader": "account111",
}
My question is "What's the magic query to get the document below when I receive a get request on an account document with id="account111"?"
{
"id": "account111",
"name": John Doe,
"conversations": [ // 2) Join account table with conversations
{
"id": "conversation111",
"name": "RethinkDB",
"to": [ // 3) Join conversations table with accounts
{
"id": "account111",
"name": "John Doe",
},
{
"id": "account222",
"name": "Bobby Zoya",
},
{
"id": "account333",
"name": "Maya Bee",
},
]
"messages": [ // 4) Join conversations with messages
{
"id": "message111",
"text": "I love how RethinkDB does joins.",
"from": { // 5) Join messages with accounts
"id": "account111",
"name": "John Doe",
},
"message_readers": [
{
"name": "John Doe",
"id": "account111",
}
],
},
],
},
],
}
Any guidance or advice would be fantastic. JavaScript or Python code would be awesome.
I had a hard time understanding what you want (you have multiple documents with the id 111), but I think this is the query you are looking for
Python query:
r.table("accounts").map(lambda account:
account.merge({
"conversations": r.table("conversations").filter(lambda conversation:
conversation["to"].contains(account["id"])).coerce_to("array").map(lambda conversation:
conversation.merge({
"to": conversation["to"].map(lambda account:
r.table("accounts").get(account)).pluck(["id", "name",]).coerce_to("array"),
"messages": r.table("messages").filter(lambda message:
message["conversation"] == conversation["id"]).coerce_to("array").map(lambda message:
message.merge({
"from": r.table("accounts").get(message["from"]).pluck(["id", "name",]),
"readers": r.table("message_readers").filter(lambda message_reader:
message["id"] == message_reader["message"]).coerce_to("array").order_by(r.desc("created_on")),
})).order_by(r.desc("created_on"))
})).order_by(r.desc("modified_on"))
})).order_by("id").run(db_connection)

Resources