Skip to content

Commit

Permalink
Merge pull request #1923 from Danielle9897/RDoc-2547-fixIndexingHiera…
Browse files Browse the repository at this point in the history
…rchicalData

RDoc-2547 Fix Indexing Hierarchical Data
  • Loading branch information
ppekrol authored Oct 15, 2024
2 parents dddcea6 + 68a904c commit b26806b
Show file tree
Hide file tree
Showing 12 changed files with 323 additions and 194 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,48 +1,53 @@
# Indexes: Indexing Hierarchical Data

# Indexing Hierarchical Data
---

{NOTE: }

Use the indexing `Recurse` method to recurse through the layers of a hierarchical document
and index its elements.
* Use the `Recurse` method to traverse the layers of a hierarchical document and index its fields.

* In this Page:
* [Hierarchical Data](../indexes/indexing-hierarchical-data#hierarchical-data)
* [Indexing Hierarchical Data](../indexes/indexing-hierarchical-data#indexing-hierarchical-data)
* [Hierarchical data](../indexes/indexing-hierarchical-data#hierarchical-data)
* [Index hierarchical data](../indexes/indexing-hierarchical-data#index-hierarchical-data)
* [Query the index](../indexes/indexing-hierarchical-data#query-the-index)

{NOTE/}

---

{PANEL: Hierarchical Data}
{PANEL: Hierarchical data}

One significant advantage of document databases is their tendency not to impose limits on data structuring.
**Hierarchical data structures** exemplify this quality well; for example, consider the commonly used comment thread, implemented using objects such as:

{CODE classes_1@Indexes\IndexingHierarchicalData.cs /}

One of the significant advantages offered by document databases is their tendency not to force
limits upon data structuring. **Hierarchical data structures** demonstrate this quality beautifully:
take, for example, the commonly-used **Comment thread**, implemented using objects such as:
{CODE indexes_1@Indexes\IndexingHierarchicalData.cs /}
Readers of a post created using the above `BlogPost` structure can add `BlogPostComment` entries to the post's _Comments_ field,
and readers of these comments can reply with comments of their own, creating a recursive hierarchical structure.

Readers of a post created using the above `BlogPost` structure, can add `BlogPostComment` comments
to its Comments field. And readers of these comments can reply with comments of their own, creating
a recursive hierarchical structure.
For example, the following document, `BlogPosts/1-A`, represents a blog post by John that contains multiple layers of comments from various authors.

`BlogPosts/1-A`, for example, is a blog entry posted by John, that contains several layers of
comments left by various authors.
`BlogPosts/1-A`:

`BlogPosts/1-A`:
{CODE-BLOCK:JSON}
{
"Author ": "John",
"Author": "John",
"Title": "Post title..",
"Text": "Post text..",
"Comments": [
{
"Author": "Moon",
"Text": "Comment text..",
"Comments": [
{
"Author": "Bob"
"Author": "Bob",
"Text": "Comment text.."
},
{
"Author": "Adel",
"Text": "Comment text..",
"Comments": {
"Author": "Moon"
"Author": "Moon",
"Text": "Comment text.."
}
}
]
Expand All @@ -56,44 +61,52 @@ comments left by various authors.

{PANEL/}

{PANEL: Indexing Hierarchical Data}
{PANEL: Index hierarchical data}

To index the elements of a hierarchical structure like the one demonstrated above,
use RavenDB's `Recurse` method.
To index the elements of a hierarchical structure like the one above, use RavenDB's `Recurse` method.

The sample index below shows how to use `Recurse` to traverse the comments in the post thread and index them by their authors.
We can then [query the index](../indexes/indexing-hierarchical-data#query-the-index) for all blog posts that contain comments by specific authors.

In the sample below, we use `Recurse` to go through comments in the post thread
and index them by their authors.
{CODE-TABS}
{CODE-TAB:csharp:AbstractIndexCreationTask indexes_2@Indexes\IndexingHierarchicalData.cs /}
{CODE-TAB:csharp:Operation indexes_3@Indexes\IndexingHierarchicalData.cs /}
{CODE-TAB:csharp:JavaScript indexes_3@Indexes\JavaScript.cs /}
{CODE-TAB:csharp:Index index_1@Indexes\IndexingHierarchicalData.cs /}
{CODE-TAB:csharp:JavaScript_index index_2@Indexes\IndexingHierarchicalData.cs /}
{CODE-TAB:csharp:Put_indexes_operation index_3@Indexes\IndexingHierarchicalData.cs /}
{CODE-TABS/}

---
{PANEL/}

{PANEL: Query the index}

The index can be queried for all blog posts that contain comments made by specific authors.

**Query the index using code**:

{CODE-TABS}
{CODE-TAB:csharp:Query query_1@Indexes\IndexingHierarchicalData.cs /}
{CODE-TAB:csharp:Query_async query_2@Indexes\IndexingHierarchicalData.cs /}
{CODE-TAB:csharp:DocumentQuery query_3@Indexes\IndexingHierarchicalData.cs /}
{CODE-TAB-BLOCK:sql:RQL}
from index "BlogPosts/ByCommentAuthor"
where Authors == "Moon"
{CODE-TAB-BLOCK/}
{CODE-TABS/}

**Query the index using the Studio**:

### Querying the created index
* Query the index from the Studio's [List of Indexes](../studio/database/indexes/indexes-list-view#indexes-list-view) view:

* The index we created can be queried using code.
{CODE-TABS}
{CODE-TAB:csharp:Query indexes_4@Indexes\IndexingHierarchicalData.cs /}
{CODE-TAB:csharp:DocumentQuery indexes_5@Indexes\IndexingHierarchicalData.cs /}
{CODE-TABS/}
!["List of Indexes view"](images/list-of-indexes-view.png "List of Indexes view")

* The index can also be queried using Studio.
* View the query results in the [Query](../studio/database/queries/query-view) view:

* Use Studio's [List of Indexes](../studio/database/indexes/indexes-list-view#indexes-list-view)
view to define and query the index.

!["List of Indexes view"](images/list-of-indexes-view.png "List of Indexes view")
!["Query View"](images/query-view.png "Query view")

* Use the **Query** view to see the results and the list of [terms](../studio/database/indexes/indexes-list-view#indexes-list-view---actions)
indexed by the `Recurse` method.

!["Query View"](images/query-view.png "Query View")
* View the list of terms indexed by the `Recurse` method:

!["Click to View Index Terms"](images/click-to-view-terms.png "Click to View Index Terms")
!["Click to View Index Terms"](images/click-to-view-terms.png "Click to view index terms")

!["Index Terms"](images/index-terms.png "Index Terms")
!["Index Terms"](images/index-terms.png "Index terms")

{PANEL/}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# Indexes: Indexing Hierarchical Data
# Indexing Hierarchical Data

One of the greatest advantages of a document database is that we have very few limits on how we structure our data. One very common scenario is the usage of hierarchical data structures. The most trivial of them is the comment thread:
One of the greatest advantages of a document database is that we have very few limits on how we structure our data.
One very common scenario is the usage of hierarchical data structures.
The most trivial of them is the comment thread:

{CODE:java indexes_1@Indexes\IndexingHierarchicalData.java /}

While it is very easy to work with such a structure in all respects, it does bring up an interesting question, namely how can we search for all blog posts that were commented by specified author?
While it is very easy to work with such a structure in all respects, it does bring up an interesting question,
namely how can we search for all blog posts that were commented by specified author?

The answer to that is that RavenDB contains built-in support for indexing hierarchies, and you can take advantage of the `Recurse` method to define an index using the following syntax:
The answer to that is that RavenDB contains built-in support for indexing hierarchies,
and you can take advantage of the `Recurse` method to define an index using the following syntax:

{CODE-TABS}
{CODE-TAB:java:AbstractIndexCreationTask indexes_2@Indexes\IndexingHierarchicalData.java /}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,111 @@
# Indexes: Indexing Hierarchical Data
# Indexing Hierarchical Data
---

One of the greatest advantages of a document database is that we have very few limits on how we structure our data. One very common scenario is the usage of hierarchical data structures. The most trivial of them is the comment thread:
{NOTE: }

{CODE:nodejs indexes_1@indexes\indexingHierarchicalData.js /}
* Use the `Recurse` method to traverse the layers of a hierarchical document and index its fields.

While it is very easy to work with such a structure in all respects, it does bring up an interesting question, namely how can we search for all blog posts that were commented by specified author?
* In this Page:
* [Hierarchical data](../indexes/indexing-hierarchical-data#hierarchical-data)
* [Index hierarchical data](../indexes/indexing-hierarchical-data#index-hierarchical-data)
* [Query the index](../indexes/indexing-hierarchical-data#query-the-index)

The answer to that is that RavenDB contains built-in support for indexing hierarchies, and you can take advantage of the `Recurse` method to define an index using the following syntax:
{NOTE/}

---

{PANEL: Hierarchical data}

One significant advantage of document databases is their tendency not to impose limits on data structuring.
**Hierarchical data structures** exemplify this quality well; for example, consider the commonly used comment thread, implemented using objects such as:

{CODE:nodejs class_1@indexes\indexingHierarchicalData.js /}

Readers of a post created using the above `BlogPost` structure can add `BlogPostComment` entries to the post's _comments_ field,
and readers of these comments can reply with comments of their own, creating a recursive hierarchical structure.

For example, the following document, `BlogPosts/1-A`, represents a blog post by John that contains multiple layers of comments from various authors.

`BlogPosts/1-A`:

{CODE-BLOCK:JSON}
{
"author": "John",
"title": "Post title..",
"text": "Post text..",
"comments": [
{
"author": "Moon",
"text": "Comment text..",
"comments": [
{
"author": "Bob",
"text": "Comment text.."
},
{
"author": "Adel",
"text": "Comment text..",
"comments": {
"author": "Moon",
"text": "Comment text.."
}
}
]
}
],
"@metadata": {
"@collection": "BlogPosts"
}
}
{CODE-BLOCK/}

{PANEL/}

{PANEL: Index hierarchical data}

To index the elements of a hierarchical structure like the one above, use RavenDB's `Recurse` method.

The sample index below shows how to use `Recurse` to traverse the comments in the post thread and index them by their authors.
We can then [query the index](../indexes/indexing-hierarchical-data#query-the-index) for all blog posts that contain comments by specific authors.

{CODE-TABS}
{CODE-TAB:nodejs:Index index_1@indexes\indexingHierarchicalData.js /}
{CODE-TAB:nodejs:Put_indexes_operation index_2@indexes\indexingHierarchicalData.js /}
{CODE-TABS/}

{PANEL/}

{PANEL: Query the index}

The index can be queried for all blog posts that contain comments made by specific authors.

**Query the index using code**:

{CODE-TABS}
{CODE-TAB:nodejs:AbstractIndexCreationTask indexes_2@indexes\indexingHierarchicalData.js /}
{CODE-TAB:nodejs:Operation indexes_3@indexes\indexingHierarchicalData.js /}
{CODE-TAB:nodejs:Query query_1@indexes\indexingHierarchicalData.js /}
{CODE-TAB-BLOCK:sql:RQL}
from index "BlogPosts/ByCommentAuthor"
where authors == "Moon"
{CODE-TAB-BLOCK/}
{CODE-TABS/}

This will index all the comments in the thread, regardless of their location in the hierarchy.
**Query the index using the Studio**:

* Query the index from the Studio's [List of Indexes](../studio/database/indexes/indexes-list-view#indexes-list-view) view:

!["List of Indexes view"](images/list-of-indexes-view.png "List of Indexes view")

* View the query results in the [Query](../studio/database/queries/query-view) view:

!["Query View"](images/query-view.png "Query view")

* View the list of terms indexed by the `Recurse` method:

!["Click to View Index Terms"](images/click-to-view-terms.png "Click to view index terms")

!["Index Terms"](images/index-terms.png "Index terms")

{CODE:nodejs indexes_4@indexes\indexingHierarchicalData.js /}
{PANEL/}

## Related articles

Expand All @@ -26,6 +116,6 @@ This will index all the comments in the thread, regardless of their location in
- [Indexing Spatial Data](../indexes/indexing-spatial-data)
- [Indexing Polymorphic Data](../indexes/indexing-polymorphic-data)

### Querying
### Querying

- [Query Overview](../client-api/session/querying/how-to-query)
Loading

0 comments on commit b26806b

Please sign in to comment.