{NOTE: }
-
In RavenDB different documents can have different shapes.
Documents are schemaless - new fields can be added or removed as needed. -
For such dynamic data, you can define indexes with dynamic-index-fields.
-
This allows querying the index on fields that aren't yet known at index creation time,
which is very useful when working on highly dynamic systems. -
Any value type can be indexed, string, number, date, etc.
-
An index definition can contain both dynamic-index-fields and regular-index-fields.
-
In this page:
{NOTE/}
{PANEL: Indexing documents fields KEYS}
{NOTE: }
The following allows you to:
- Index any field that is under the some object from the document.
- After index is deployed, any new field added to the this object will be indexed as well.
The document: {CODE:java dynamic_fields_1@Indexes\DynamicFields.java /}
{CODE-BLOCK:json} // Sample document content { "attributes": { "color": "Red", "size": 42 } } {CODE-BLOCK/}
The index:
-
The following index will index any field under the
attributes
object from the document,
a dynamic-index-field will be created for each such field.
New fields added to the object after index creation time will be dynamically indexed as well. -
The actual dynamic-index-field name on which you can query will be the attribute field key.
e.g. Keyscolor
&size
will become the actual dynamic-index-fields.
{CODE-TABS} {CODE-TAB:java:JavaScript-index dynamic_fields_2_JS@Indexes\DynamicFields.java /} {CODE-TABS/}
The query:
-
You can now query the generated dynamic-index fields.
Property_
is Not queryable, it is only used in the index definition syntax. -
To get all documents with some 'size' use:
{CODE-TABS} {CODE-TAB:java:DocumentQuery dynamic_fields_3@Indexes\DynamicFields.java /} {CODE-TAB-BLOCK:sql:RQL} // 'size' is a dynamic-index-field that was indexed from the Attributes object from index 'Products/ByAttributeKey/JS' where size = 42 {CODE-TAB-BLOCK/} {CODE-TABS/}
{NOTE/}
{NOTE: }
The following allows you to:
- Define an index on a collection without needing any common structure between the indexed documents.
- After index is deployed, any new field added to the document will be indexed as well.
{INFO: }
Consider if that is a true necessity, as indexing every single field can end up costing time and disk space.
{INFO/}
The document:
{CODE:java dynamic_fields_4@Indexes\DynamicFields.java /}
{CODE-BLOCK:json} // Sample document content { "firstName": "John", "lastName": "Doe", "title": "Engineer", // ... } {CODE-BLOCK/}
The index:
-
The following index will index any field from the document,
a dynamic-index-field will be created for each field.
New fields added to the document after index creation time will be dynamically indexed as well. -
The actual dynamic-index-field name on which you can query will be the field key.
e.g. KeysfirstName
&lastName
will become the actual dynamic-index-fields.
{CODE-TABS} {CODE-TAB:java:JavaScript-index dynamic_fields_5_JS@Indexes\DynamicFields.java /} {CODE-TABS/}
The query:
- To get all documents with some 'lastName' use:
{CODE-TABS} {CODE-TAB:java:DocumentQuery dynamic_fields_6@Indexes\DynamicFields.java /} {CODE-TAB-BLOCK:sql:RQL} // 'lastName' is a dynamic-index-field that was indexed from the document from index 'Products/ByAnyField/JS' where lastName = "Doe" {CODE-TAB-BLOCK/} {CODE-TABS/}
{NOTE/}
{PANEL/}
{PANEL: Indexing documents fields VALUES}
{NOTE: }
This example shows:
- Only the basic concept of creating a dynamic-index-field from the value of a document field.
- Documents can then be queried based on those indexed values.
- For a more practical usage see the Example below.
The document: {CODE:java dynamic_fields_7@Indexes\DynamicFields.java /}
{CODE-BLOCK:json} // Sample document content { "productType": "Electronics", "pricePerUnit": 23 } {CODE-BLOCK/}
The index:
-
The following index will index the value of document field 'productType'.
-
This value will be the dynamic-index-field name on which you can query.
e.g. Field valueElectronics
will be the dynamic-index-field.
{CODE-TABS} {CODE-TAB:java:LINQ-index dynamic_fields_8@Indexes\DynamicFields.java /} {CODE-TABS/}
The query:
- To get all documents of some product type having a specific price per unit use:
{CODE-TABS} {CODE-TAB:java:DocumentQuery dynamic_fields_9@Indexes\DynamicFields.java /} {CODE-TAB-BLOCK:sql:RQL} // 'Electronics' is the dynamic-index-field that was indexed from document field 'productType' from index 'Products/ByProductType' where Electronics = 23 {CODE-TAB-BLOCK/} {CODE-TABS/}
{NOTE /}
{NOTE: }
The following allows you to:
- Index values from items in a list
- After index is deployed, any item added this list in the document will be dynamically indexed as well.
The document: {CODE:java dynamic_fields_10@Indexes\DynamicFields.java /}
{CODE-BLOCK:json}
// Sample document content
{
"name": "SomeName",
"attributes": [
{
"propName": "Color",
"propValue": "Blue"
},
{
"propName": "Width",
"propValue": "10"
},
{
"propName": "Length",
"propValue": "20"
},
...
]
}
{CODE-BLOCK/}
The index:
-
The following index will create a dynamic-index-field per item in the document's
attributes
list.
New items added to the attributes list after index creation time will be dynamically indexed as well. -
The actual dynamic-index-field name on which you can query will be the item's propName value.
e.g. 'propName' valueWidth
will be a dynamic-index-field.
{CODE-TABS} {CODE-TAB:java:LINQ-index dynamic_fields_11@Indexes\DynamicFields.java /} {CODE-TABS/}
The query:
- To get all documents matching a specific attribute property use:
{CODE-TABS} {CODE-TAB:java:DocumentQuery dynamic_fields_12@Indexes\DynamicFields.java /} {CODE-TAB-BLOCK:sql:RQL} // 'Width' is a dynamic-index-field that was indexed from the attributes list from index 'Attributes/ByName' where Width = 10 {CODE-TAB-BLOCK/} {CODE-TABS/}
{NOTE /} {PANEL/}
{PANEL: CreateField syntax}
{CODE:csharp syntax@Indexes\DynamicFields.cs /}
{CODE:nodejs syntax_JS@Indexes\DynamicFields.js /}
Parameters | ||
---|---|---|
fieldName | string |
Name of the dynamic-index-field |
fieldValue | object |
Value of the dynamic-index-field The field Terms are derived from this value. |
stored | bool |
Sets FieldStoragefalse - will set FieldStorage.No (default value)true - will set FieldStorate.Yes |
analyzed | bool |
Sets FieldIndexingnull - FieldIndexing.Default (default value)false - FieldIndexing.Exact true - FieldIndexing.Search |
options | CreateFieldOptions |
Dynamic-index-field options |
CreateFieldOptions | ||
---|---|---|
Storage | FieldStorage? |
Learn about storing data in the index. |
Indexing | FieldIndexing? |
Learn about using analyzers in the index. |
TermVector | FieldTermVector? |
Learn about term vectors in the index. |
{INFO: }
-
All above examples have used the character
_
in the dynamic-index-field definition.
However, using_
is just a convention. Any other string can be used instead. -
This property is Not queryable, it is only used in the index definition syntax.
The actual dynamic-index-fields that are generated are defined by theCreateField
method.
{INFO /}
{PANEL/}
{PANEL: Indexed fields & terms view}
The generated dynamic-index-fields and their indexed terms can be viewed in the Terms View.
Below are sample index fields & their terms generated from the last example.
{PANEL/}