Created
February 7, 2013 20:08
-
-
Save mattjohnsonpint/4733743 to your computer and use it in GitHub Desktop.
Demonstrating boosting query terms
This file contains hidden or 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.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