Indexes: Dynamic Index Fields


Indexing documents fields KEYS

Example - index any field


The following allows you to:

  • Index any field that is under the 'Attributes' object from the document.
  • After index is deployed, any new field added to the this object will be indexed as well.

The document:

public class Product
{
    public string Id { get; set; }
    
    // The KEYS under the Attributes object will be dynamically indexed 
    // Fields added to this object after index creation time will also get indexed
    public Dictionary<string, object> Attributes { get; set; }
}

// Sample document content
{
    "Attributes": {
        "Color": "Red",
        "Size": 42
    }
}

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. Keys Color & Size will become the actual dynamic-index-fields.

public class Products_ByAttributeKey : AbstractIndexCreationTask<Product>
{
    public Products_ByAttributeKey()
    {
        Map = products => from p in products
            select new
            {
                // Call 'CreateField' to generate dynamic-index-fields from the Attributes object keys
                // Using '_' is just a convention. Any other string can be used instead of '_'
                
                // The actual field name will be item.Key
                // The actual field terms will be derived from item.Value
                _ = p.Attributes.Select(item => CreateField(item.Key, item.Value))
            };
    }
}

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:

IList<Product> matchingDocuments = session
    .Advanced
    .DocumentQuery<Product, Products_ByAttributeKey>()
     // 'Size' is a dynamic-index-field that was indexed from the Attributes object
    .WhereEquals("Size", 42)
    .ToList();
// 'Size' is a dynamic-index-field that was indexed from the Attributes object
from index 'Products/ByAttributeKey' where Size = 42

Indexing documents fields VALUES

Example - basic


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:

public class Product
{
    public string Id { get; set; }
    
    // The VALUE of ProductType will be dynamically indexed
    public string ProductType { get; set; }
    public int PricePerUnit { get; set; }
}

// Sample document content
{
    "ProductType": "Electronics",
    "PricePerUnit": 23
}

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 value Electronics will be the dynamic-index-field.

public class Products_ByProductType : AbstractIndexCreationTask<Product>
{
    public Products_ByProductType()
    {
        Map = products => from p in products
            select new
            {
                // Call 'CreateField' to generate the dynamic-index-fields
                // The field name will be the value of document field 'ProductType'
                // The field terms will be derived from document field 'PricePerUnit'
                _ = CreateField(p.ProductType, p.PricePerUnit)
            };
    }
}

The query:

  • To get all documents of some product type having a specific price per unit use:

IList<Product> matchingDocuments = session
    .Advanced
    .DocumentQuery<Product, Products_ByProductType>()
     // 'Electronics' is the dynamic-index-field that was indexed from document field 'ProductType'
    .WhereEquals("Electronics", 23)
    .ToList();
// 'Electronics' is the dynamic-index-field that was indexed from document field 'ProductType'
from index 'Products/ByProductType' where Electronics = 23

Example - list


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:

public class Product
{
    public string Id { get; set; }
    public string Name { get; set; }
    
    // For each element in this list, the VALUE of property 'PropName' will be dynamically indexed
    // e.g. Color, Width, Length (in ex. below) will become dynamic-index-fields
    public List<Attribute> Attributes { get; set; }
}

public class Attribute
{
    public string PropName { get; set; }
    public string PropValue { get; set; }
}

// Sample document content
{
    "Name": "SomeName",
    "Attributes": [
       {  
           "PropName": "Color",
           "PropValue": "Blue"
       },
       {
           "PropName": "Width",
           "PropValue": "10"
       },
       {
           "PropName": "Length",
           "PropValue": "20"
       },
       ...
    ]
}

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' value Width will be a dynamic-index-field.

public class Attributes_ByName : AbstractIndexCreationTask<Product>
{
    public Attributes_ByName()
    {
        Map = products => from a in products
            select new
            {
                // Define the dynamic-index-fields by calling CreateField
                // A dynamic-index-field will be generated for each item in the Attributes list
                
                // For each item, the field name will be the value of field 'PropName'
                // The field terms will be derived from field 'PropValue'
                _ = a.Attributes.Select(item => CreateField(item.PropName, item.PropValue)),
                
                // A regular index field can be defined as well:
                Name = a.Name
            };
    }
}

The query:

  • To get all documents matching a specific attribute property use:

IList<Product> matchingDocuments = session
    .Advanced
    .DocumentQuery<Product, Attributes_ByName>()
     // 'Width' is a dynamic-index-field that was indexed from the Attributes list
    .WhereEquals("Width", 10)
    .ToList();
// 'Width' is a dynamic-index-field that was indexed from the Attributes list
from index 'Attributes/ByName' where Width = 10

CreateField syntax

object CreateField(string name, object value);

object CreateField(string name, object value, bool stored, bool analyzed);

object CreateField(string name, object value, CreateFieldOptions options);
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 FieldStorage

false - will set FieldStorage.No (default value)
true - will set FieldStorate.Yes
analyzed bool Sets FieldIndexing

null - 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.

  • 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 the CreateField method.