Skip to content

Instantly share code, notes, and snippets.

@harrison314
Created August 7, 2022 08:42
Show Gist options
  • Save harrison314/41b1dccc48520636b5b212c24fd65f72 to your computer and use it in GitHub Desktop.
Save harrison314/41b1dccc48520636b5b212c24fd65f72 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Raven.Client.Documents.Indexes;
using Raven.TestDriver;
namespace Area52.RavenDbTests;
public class DynamicIndexingTests : RavenTestDriver
{
[Fact]
public void DynamicIndexingTest()
{
using var store = this.GetDocumentStore();
store.ExecuteIndex(new LogEntityIndex());
using (var session = store.OpenSession())
{
session.Store(new LogEntity() { Name = "Spaces", Level = "Debug", Properties = new[] { new LogEntityProperty("Application", "Area52 Foo Bar"), new LogEntityProperty("Foo", "Bar1"), new LogEntityProperty("count", "45") } });
session.Store(new LogEntity() { Name = "Dots", Level = "Info", Properties = new[] { new LogEntityProperty("Application", "Area52.Foo.Bar"), new LogEntityProperty("Foo", "Bar2"), new LogEntityProperty("RequestId", "AN4589kiua") } });
session.Store(new LogEntity() { Name = "DoubleDots", Level = "Info", Properties = new[] { new LogEntityProperty("Application", "Area52:Foo:Bar"), new LogEntityProperty("Foo", "Bar3") } });
session.Store(new LogEntity() { Name = "Join", Level = "Debug", Properties = new[] { new LogEntityProperty("Application", "Area52FooBar"), new LogEntityProperty("Foo", "Bar3") } });
session.Store(new LogEntity() { Name = "Another application", Level = "Info", Properties = new[] { new LogEntityProperty("Application", "Another"), new LogEntityProperty("Foo", "Bar3") } });
session.SaveChanges();
}
this.WaitForIndexing(store);
using (var session = store.OpenSession())
{
var exactResult = session.Advanced.RawQuery<LogEntity>("from index 'LogEntityIndex' where exact(Application = 'Area52.Foo.Bar')").ToList();
Assert.Equal(1, exactResult.Count);
var startsWithResult = session.Advanced.RawQuery<LogEntity>("from index 'LogEntityIndex' where startsWith(Application, 'Area52')").ToList();
Assert.Equal(3, startsWithResult.Count);
}
}
}
public class LogEntityIndex : AbstractIndexCreationTask<LogEntity>
{
public LogEntityIndex()
{
this.Map = docs => from doc in docs
select new
{
Name = doc.Name,
_ = doc.Properties.Select(t => this.CreateField(t.Name, t.Value, new CreateFieldOptions()
{
Indexing = FieldIndexing.Exact,
Storage = FieldStorage.No,
TermVector = FieldTermVector.No
}))
};
}
}
public class LogEntity
{
public string Level
{
get;
set;
}
public string Name
{
get;
set;
}
public LogEntityProperty[] Properties
{
get;
set;
}
}
public class LogEntityProperty
{
public string Name
{
get;
set;
}
public string Value
{
get;
set;
}
public LogEntityProperty()
{
}
public LogEntityProperty(string name, string value)
{
this.Name = name;
this.Value = value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment