Indexes: Dynamic Fields

To support searching in object graphs they cannot have the entire structure declared upfront.

RavenDB exposes an indexing API for creating fields dynamically.

With this feature, you can search for documents using fields which are created on the fly. For example, consider a Product object that is declared as follows:

public static class Product {
    private String id;
    private List<Attribute> attributes;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public List<Attribute> getAttributes() {
        return attributes;
    }

    public void setAttributes(List<Attribute> attributes) {
        this.attributes = attributes;
    }
}

public static class Attribute {
    private String name;
    private String value;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

Properties such as color or size are added only to some products, while other ones can have the weight and volume defined. Since Attribute has string fields, they can specify very different properties of products. In order to query on fields which aren't known at index creation time, we introduced the ability to create them dynamically during indexing.

The following index can be created in order to index each attribute value under its name as a separate field:

public static class Products_ByAttribute extends AbstractIndexCreationTask {
    public static class Result {
        private String color;
        private String size;

        public String getColor() {
            return color;
        }

        public void setColor(String color) {
            this.color = color;
        }

        public String getSize() {
            return size;
        }

        public void setSize(String size) {
            this.size = size;
        }
    }

    public Products_ByAttribute() {
        map = "docs.Products.Select(p => new { " +
            "    _ = p.Attributes.Select(attribute => this.CreateField(attribute.Name, attribute.Value, false, true)) " +
            "})";
    }
}

The _ character used as the field name in the mapping definition is just a convention. You can use any name, it won't be used by the index anyway. The actual field name that you want to query by is defined in CreateField(...). It will generate an index field based on the properties of indexed documents and passed parameters

The index can have more fields defined, just like in any other ordinary index.

Options

Field options like FieldStorage and FieldIndexing are configurable by arguments of the CreateField method:

  • stored
    • false (default) - FieldStorage.No
    • true - FieldStorage.Yes
  • analyzed
    • null (default) - FieldIndexing.Default
    • true - FieldIndexing.Search
    • false - FieldIndexing.Exact

Looking for products by attributes with the usage of such a defined index is supported as if it were real object properties:

List<Product> results = session
    .query(Products_ByAttribute.Result.class, Products_ByAttribute.class)
    .whereEquals("Color", "red")
    .ofType(Product.class)
    .toList();

Information

All types of values are supported by dynamically created fields. They can be numbers, dates, etc.