Created
October 3, 2012 10:25
-
-
Save jesuslpm/3826266 to your computer and use it in GitHub Desktop.
RavenDB FullTextSearch on all properties. The query should return the matching values and the property names
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using Raven.Client; | |
using Raven.Client.Indexes; | |
using Raven.Client.Document; | |
using Raven.Client.Embedded; | |
using Raven.Client.Linq; | |
using Xunit; | |
namespace Raven.Tests | |
{ | |
public class Place | |
{ | |
public string Id { get; set; } | |
public string Name { get; set; } | |
public string Country { get; set; } | |
public string City { get; set; } | |
} | |
public class PropertyValue | |
{ | |
public string Name { get; set; } | |
public string Value { get; set; } | |
} | |
public class Places_FullText : AbstractIndexCreationTask<Place, PropertyValue> | |
{ | |
public Places_FullText() | |
{ | |
Map = places => places.SelectMany(p => new[] | |
{ | |
new { Id = p.Id, Name = "Name", Value = p.Name }, | |
new { Id = p.Id, Name = "Country", Value = p.Country }, | |
new { Id = p.Id, Name = "City", Value = p.City } | |
}).Select(x => new { __document_id = x.Id, Name = x.Name, Value = x.Value }); | |
this.Index("Value", Abstractions.Indexing.FieldIndexing.Analyzed); | |
this.Store("Value", Abstractions.Indexing.FieldStorage.Yes); | |
this.Store("Name", Abstractions.Indexing.FieldStorage.Yes); | |
} | |
} | |
public class Places_FullTextLazyMan : AbstractIndexCreationTask<Place, PropertyValue> | |
{ | |
public Places_FullTextLazyMan() | |
{ | |
Map = places => places.SelectMany(p => | |
AsDocument(p).Select(token => new { __document_id = p.Id, Name = token.Key, Value = token.Value }) | |
).Select(x => new { __document_id = x.__document_id, Name = x.Name, Value = x.Value }); | |
this.Index("Value", Abstractions.Indexing.FieldIndexing.Analyzed); | |
this.Store("Value", Abstractions.Indexing.FieldStorage.Yes); | |
this.Store("Name", Abstractions.Indexing.FieldStorage.Yes); | |
} | |
} | |
public class FullTextSearch : IDisposable | |
{ | |
DocumentStore Store; | |
public FullTextSearch() | |
{ | |
Store = new EmbeddableDocumentStore { RunInMemory = true }; | |
Store.Initialize(); | |
new Places_FullText().Execute(this.Store); | |
new Places_FullTextLazyMan().Execute(this.Store); | |
} | |
[Fact] | |
public void FullTextSeachPropertyValuesShouldWork() | |
{ | |
FullTextSeach<Places_FullText>(); | |
} | |
[Fact] | |
public void FullTextSeachPropertyValuesLazyManShouldWork() | |
{ | |
FullTextSeach<Places_FullTextLazyMan>(); | |
} | |
private void FullTextSeach<TIndexCreation>() where TIndexCreation : AbstractIndexCreationTask<Place, PropertyValue>, new() | |
{ | |
using (var session = this.Store.OpenSession()) | |
{ | |
session.Store(new Place | |
{ | |
Id = "Places/1", | |
Name = "Place 1", | |
Country = "Holland", | |
City = "Amsterdam" | |
}); | |
session.Store(new Place | |
{ | |
Id = "Places/2", | |
Name = "Place 2", | |
Country = "America", | |
City = "New York" | |
}); | |
session.SaveChanges(); | |
} | |
using (var session = this.Store.OpenSession()) | |
{ | |
var query = session.Query<PropertyValue, TIndexCreation>() | |
.Search(pv => pv.Value, "Am*", escapeQueryOptions: EscapeQueryOptions.AllowAllWildcards) | |
.OrderBy( pv => pv.Name) | |
.AsProjection<PropertyValue>() | |
.Customize( x => x.WaitForNonStaleResults()); | |
var propertyValues = query.ToList(); | |
var statistics = session.Advanced.DocumentStore.DatabaseCommands.GetStatistics(); | |
Assert.Equal(0, statistics.Errors.Length); | |
Assert.Equal(2, propertyValues.Count); | |
Assert.Equal("City", propertyValues[0].Name); | |
Assert.Equal("Amsterdam", propertyValues[0].Value); | |
Assert.Equal("Country", propertyValues[1].Name); | |
Assert.Equal("America", propertyValues[1].Value); | |
var placesQuery = session.Query<PropertyValue, TIndexCreation>() | |
.Where( p => p.Name == "City" && p.Value == "Amsterdam") | |
.Customize(x => x.WaitForNonStaleResults()) | |
.As<Place>(); | |
var places = placesQuery.ToList(); | |
Assert.Equal(1, places.Count); | |
Assert.Equal(places[0].City, "Amsterdam"); | |
Assert.Equal(places[0].Country, "Holland"); | |
Assert.Equal(places[0].Id, "Places/1"); | |
Assert.Equal(places[0].Name, "Place 1"); | |
} | |
} | |
#region IDisposable Members | |
public void Dispose() | |
{ | |
if (this.Store != null) this.Store.Dispose(); | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You may want to search on all property values of a document, and you want to know what values matched and on what properties.