Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why use Neo4j in Ruby APP

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Why to use Neo4j in Ruby APP, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Every day, I need to store a lot of data, and I can use a lot of tools, such as PostgreSQL,MySQL,SQLite,Redis and MongoDB, when I have accumulated a lot of experience and mastered these tools, I don't think they have any fun. I love Ruby from the bottom of my heart because it is fun and allows me to do some great things in my own way. But I didn't realize that such a data processing tool would affect me for a long time, although it made me find a new interest, so please let me introduce Neo4j to you.

What is Neo4j?

Neo4j is a graphical database! This means that it is optimized to manage and query relationships (nodes) between entities (nodes), rather than something like a relational database that uses tables.

Why is this great? Imagine a world without foreign keys. The relationships of each entity in the database are so close that other entities can be referenced directly. If the relationship you want to reference does not have a relation table or search scan, all you need is a few links. This shows a typical object model. This is very exciting because Neo4j provides a lot of the functionality that we expect databases to have and provides us with tools to query complex data graphs.

Introduction to ActiceNote

To link to Neo4j, we will use neo4j gem. The way to connect to Neo4j in your Rails application can be found in the gem documentation. At the same time, the application that displays the code can also run the Rails application in the GitHub library (using a branch of sitepoint) you can run in your database using the pre-rake load_sample_data command to populate the database.

Here is a basic case study of the asset model from the Rails APP of asset management

Class Asset include Neo4j::ActiveNode property: title has_many: out,: categories, type:: HAS_CATEGORY end

A brief explanation:

1.Neo4j 's GEM gave us the Neo4j:: ActiveNode module, including the model we made.

two。 This asset means that this model will be responsible for all nodes that tag assets in Neo4j (except for tagging a node, there can be many tags that play a similar role)

3. We have a title attribute to describe each node

4. We have a taxonomic association of outgoing has_many. This association helps us find the classified objects through the database HAS_CATEGORY relationship below.

With this model, we can do a basic query to find an asset and get its classification.

2.2.0: 001 > asset = Asset.first = > # 2.2.0: 002 > asset.categories.to_a = > [#]

Anyone familiar with ActiveRecord or Mongoid will see it a hundred times. To make it more interesting, let's define a Category model:

Class Category include Neo4j::ActiveNode property: name has_many: in,: assets, origin:: categories end

Here, our association has an option for origin that references the categories association of the asset model. If we want, we can specify type:: HAS_CATEGORY again

Create Recommendations

What if we want to acquire all the assets (asset) that share a category with our assets?

2.2.0: 003 > asset.categories.assets.to_a = > [#,...]

So what's gonna happen? ActiveNode generates a database query that specifies a path from our assets (asset) to all other assets (asset) that share a category. Then the database will send those asset back to us. Here is the query it uses:

MATCH asset436, asset436- [rel1: `has _ CATEGORY`]-> (node3: `Category`), node3 (: Category) (: Group) (: Group)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report