Skip to content

Instantly share code, notes, and snippets.

@PureKrome
Created September 24, 2014 06:54
Show Gist options
  • Save PureKrome/5aa8d9c71ee9ad8891ca to your computer and use it in GitHub Desktop.
Save PureKrome/5aa8d9c71ee9ad8891ca to your computer and use it in GitHub Desktop.
RavenDb Search example where I don't understand the Terms that are generated.
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Raven.Abstractions.Indexing;
using Raven.Client;
using Raven.Client.Indexes;
using Raven.Tests.Helpers;
using Xunit;
namespace ClassLibrary1
{
public class TestFacts : RavenTestBase
{
[Fact]
public async Task HalpFactAsync()
{
using (var store = NewDocumentStore())
{
// Seed data.
using (var session = store.OpenAsyncSession())
{
await SeedData(session);
}
// Add the index.
new Locations_Index().Execute(store);
IList<Locations_Index.Result> results;
using (var session = store.OpenAsyncSession())
{
results = await session.Query<Locations_Index.Result, Locations_Index>()
.Search(x => x.Query, "water*", escapeQueryOptions: EscapeQueryOptions.AllowPostfixWildcard)
.AsProjection<Locations_Index.Result>()
.Customize(customization => customization.WaitForNonStaleResults())
.ToListAsync();
}
Trace.TraceInformation("Results count: {0}.", results.Any() ? results.Count : -1);
foreach (var result in results)
{
Trace.TraceInformation(string.Join(", ", result.Query));
}
Trace.TraceInformation("---------------------------------");
var stats = store.DatabaseCommands.GetStatistics();
Trace.TraceInformation("Document count: {0}.", stats.CountOfDocuments);
Trace.TraceInformation("Index count: {0}.", stats.CountOfIndexes);
Trace.TraceInformation("---------------------------------");
var terms = store.DatabaseCommands.GetTerms("Locations/Index", "Query", null, int.MaxValue).ToList();
Trace.TraceInformation("Terms count: {0}.", terms.Count);
foreach (var term in terms)
{
Trace.TraceInformation(term);
}
Trace.TraceInformation("---------------------------------");
var exists = results.SingleOrDefault(x => x.Query.Contains("Agnes Water"));
Assert.Null(exists);
}
}
private static async Task SeedData(IAsyncDocumentSession asyncDocumentSession)
{
var locations = new List<Location>
{
new Location {Name = "Abbeywood", State = "Victoria"},
new Location {Name = "Abbotsford", State = "Queensland"},
new Location {Name = "Agnes Water", State = "Victoria"},
new Location {Name = "Airdmillan", State = "Queensland"},
new Location {Name = "Waterbank", State = "Victoria"},
new Location {Name = "Waterfall", State = "Queensland"},
new Location {Name = "Waterford", State = "Victoria"},
new Location {Name = "Waterford Park", State = "Queensland"},
new Location {Name = "Waterford West", State = "Victoria"},
new Location {Name = "Waterhouse", State = "Queensland"},
new Location {Name = "Waterloo", State = "Victoria"},
};
var tasks = locations.Select(asyncDocumentSession.StoreAsync).ToArray();
await Task.WhenAll(tasks);
await asyncDocumentSession.SaveChangesAsync();
}
}
public class Location
{
public string Name { get; set; }
public string State { get; set; }
}
public class Locations_Index : AbstractIndexCreationTask<Location, Locations_Index.Result>
{
public Locations_Index()
{
Map = locations => from location in locations
select new
{
Query = new object[]
{
location.Name,
location.State
}
};
Index(x => x.Query, FieldIndexing.Analyzed);
Store(x => x.Query, FieldStorage.Yes);
}
public class Result
{
public object[] Query { get; set; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment