Skip to content

Instantly share code, notes, and snippets.

@tzarger
Forked from mattjohnsonpint/BoostingTests.cs
Created February 8, 2013 00:41
Show Gist options
  • Save tzarger/4735601 to your computer and use it in GitHub Desktop.
Save tzarger/4735601 to your computer and use it in GitHub Desktop.
using System.Diagnostics;
using System.Linq;
using Raven.Abstractions.Indexing;
using Raven.Client;
using Raven.Client.Indexes;
using Raven.Imports.Newtonsoft.Json;
using Raven.Tests.Helpers;
using Xunit;
namespace RavenTests
{
public class BoostingTests : RavenTestBase
{
public class Post
{
public string Title { get; set; }
public string Body { get; set; }
}
[Fact]
public void Test()
{
using (var documentStore = NewDocumentStore())
{
documentStore.ExecuteIndex(new TestIndex());
using (var session = documentStore.OpenSession())
{
session.Store(new Post { Title = "Foo Foo Foo", Body = "Foo Foo Foo" });
session.Store(new Post { Title = "Microsoft", Body = "Bar Bar" });
session.Store(new Post { Title = "Foo RavenDB Foo", Body = "Foo Foo Foo" });
session.Store(new Post { Title = "Foo Foo Foo", Body = "Foo Microsoft Foo" });
session.Store(new Post { Title = "Foo Microsoft Foo", Body = "Foo Foo Foo" });
session.Store(new Post { Title = "Bar Bar Bar", Body = "Foo Google Foo" });
session.SaveChanges();
}
WaitForIndexing(documentStore);
using (var session = documentStore.OpenSession())
{
var boostValue = 10;
var boostTerms = new[] { "RavenDB", "Microsoft", "Google" };
var boostQuery = "* " + string.Join(" ", boostTerms.Select(x => x + "^" + boostValue));
var q = session.Query<SearchResult, TestIndex>()
.Search(x => x.Query, "Foo")
.Search(x => x.Title, boostQuery, 1, SearchOptions.And, EscapeQueryOptions.RawQuery)
.As<Post>();
Debug.WriteLine("Here's the query: {0}", q);
Debug.WriteLine("");
Debug.WriteLine("And here's the results:");
foreach (var result in q)
Debug.WriteLine(JsonConvert.SerializeObject(result, Formatting.Indented));
}
}
}
public class SearchResult
{
public string Title { get; set; }
public string Query { get; set; }
}
public class TestIndex : AbstractIndexCreationTask<Post, SearchResult>
{
public TestIndex()
{
Map = posts => from post in posts
select new
{
post.Title,
Query = new[] { post.Title, post.Body }
};
Index(x => x.Title, FieldIndexing.Analyzed);
Index(x => x.Query, FieldIndexing.Analyzed);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment