Skip to content

Commit

Permalink
RavenDB-4836 Add default analyzer as additional parameter to MoreLike…
Browse files Browse the repository at this point in the history
…ThisQuery

Dispose DocumentStore on some tests
  • Loading branch information
ricardo.brandao committed Jul 18, 2016
1 parent 5831478 commit f668c0b
Show file tree
Hide file tree
Showing 9 changed files with 1,430 additions and 34 deletions.
7 changes: 7 additions & 0 deletions Raven.Abstractions/Data/MoreLikeThisQuery.cs
Expand Up @@ -102,6 +102,11 @@ public MoreLikeThisQuery()
/// </summary>
public string AdditionalQuery { get; set; }

/// <summary>
/// The default analyzer to be used for fields without any analyzer specified in the index definition. Default is LowerCaseKeywordAnalyzer.
/// </summary>
public string DefaultAnalyzerName { get; set; }

/// <summary>
/// Values for the the mapping group fields to use as the basis for comparison
/// </summary>
Expand Down Expand Up @@ -158,6 +163,8 @@ public string GetRequestUri()
}
if(string.IsNullOrWhiteSpace(AdditionalQuery) == false)
uri.Append("query=").Append(Uri.EscapeDataString(AdditionalQuery)).Append("&");
if (string.IsNullOrWhiteSpace(DefaultAnalyzerName) == false)
uri.Append("defaultAnalyzer=").Append(Uri.EscapeDataString(DefaultAnalyzerName)).Append("&");
if (Boost != null && Boost != DefaultBoost)
uri.Append("boost=true&");
if (BoostFactor != null && BoostFactor != DefaultBoostFactor)
Expand Down
6 changes: 5 additions & 1 deletion Raven.Database/Queries/MoreLikeThisQueryRunner.cs
Expand Up @@ -17,6 +17,7 @@
using Raven.Abstractions.Util.Encryptors;
using Raven.Database.Bundles.MoreLikeThis;
using Raven.Database.Data;
using Raven.Database.Extensions;
using Raven.Database.Impl;
using Raven.Database.Indexing;
using Raven.Database.Linq;
Expand Down Expand Up @@ -102,7 +103,10 @@ public MoreLikeThisQueryResult ExecuteMoreLikeThisQuery(MoreLikeThisQuery query,
RavenPerFieldAnalyzerWrapper perFieldAnalyzerWrapper = null;
try
{
perFieldAnalyzerWrapper = index.CreateAnalyzer(new LowerCaseKeywordAnalyzer(), toDispose, true);
var defaultAnalyzer = !string.IsNullOrWhiteSpace(query.DefaultAnalyzerName)
? IndexingExtensions.CreateAnalyzerInstance(Constants.AllFields, query.DefaultAnalyzerName)
: new LowerCaseKeywordAnalyzer();
perFieldAnalyzerWrapper = index.CreateAnalyzer(defaultAnalyzer, toDispose, true);
mlt.Analyzer = perFieldAnalyzerWrapper;

var mltQuery = mlt.Like(td.ScoreDocs[0].Doc);
Expand Down
3 changes: 2 additions & 1 deletion Raven.Database/Server/Controllers/MoreLikeThisController.cs
Expand Up @@ -67,7 +67,8 @@ public static MoreLikeThisQuery GetParametersFromPath(string path, NameValueColl
MinimumTermFrequency = query.Get("minTermFreq").ToNullableInt(),
MinimumWordLength = query.Get("minWordLen").ToNullableInt(),
StopWordsDocumentId = query.Get("stopWords"),
AdditionalQuery= query.Get("query")
AdditionalQuery= query.Get("query"),
DefaultAnalyzerName = query.Get("defaultAnalyzer")
};

var keyValues = query.Get("docid").Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
Expand Down
4 changes: 4 additions & 0 deletions Raven.Tests.Issues/Raven.Tests.Issues.csproj
Expand Up @@ -62,6 +62,9 @@
<Reference Include="Lucene.Net">
<HintPath>..\SharedLibs\Lucene.Net.dll</HintPath>
</Reference>
<Reference Include="Lucene.Net.Contrib.Analyzers">
<HintPath>..\SharedLibs\Lucene.Net.Contrib.Analyzers.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Owin, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Owin.3.0.1\lib\net45\Microsoft.Owin.dll</HintPath>
<Private>True</Private>
Expand Down Expand Up @@ -584,6 +587,7 @@
<Compile Include="RavenDB_2907.cs" />
<Compile Include="RavenDB_3555.cs" />
<Compile Include="RavenDB_3994.cs" />
<Compile Include="RavenDB_4836.cs" />
<Compile Include="RDBQA_1.cs" />
<Compile Include="RDBQA_11.cs" />
<Compile Include="RDBQA_13.cs" />
Expand Down
59 changes: 27 additions & 32 deletions Raven.Tests.Issues/RavenDB_4461.cs
Expand Up @@ -3,7 +3,6 @@
using Lucene.Net.Analysis.Standard;
using Raven.Abstractions.Data;
using Raven.Abstractions.Indexing;
using Raven.Client;
using Raven.Client.Bundles.MoreLikeThis;
using Raven.Client.Indexes;
using Raven.Tests.Helpers;
Expand All @@ -13,23 +12,18 @@ namespace Raven.Tests.Issues
{
public class RavenDB_4461 : RavenTestBase
{
private readonly IDocumentStore store;

public RavenDB_4461()
{
store = NewDocumentStore();
}

[Fact]
public void AdditionalQueryFiltersResults()
{
store.ExecuteIndex(new Posts_ByPostCategory());

using (var session = store.OpenSession())
using (var store = NewDocumentStore())
{
var dataQueriedFor = new MockPost { Id = "posts/123", Body = "This is a test. Isn't it great? I hope I pass my test!", Category = "IT" };
store.ExecuteIndex(new Posts_ByPostCategory());

var someData = new List<MockPost>
using (var session = store.OpenSession())
{
var dataQueriedFor = new MockPost { Id = "posts/123", Body = "This is a test. Isn't it great? I hope I pass my test!", Category = "IT" };

var someData = new List<MockPost>
{
dataQueriedFor,
new MockPost { Id = "posts/234", Body = "I have a test tomorrow. I hate having a test", Category = "School"},
Expand All @@ -38,29 +32,30 @@ public void AdditionalQueryFiltersResults()
new MockPost { Id = "posts/3458", Body = "test", Category = "Test" },
new MockPost { Id = "posts/3459", Body = "test", Category = "Test" }
};
someData.ForEach(session.Store);
someData.ForEach(session.Store);

session.SaveChanges();
}
session.SaveChanges();
}

WaitForIndexing(store);
WaitForIndexing(store);

using (var session = store.OpenSession())
{
Assert.NotEmpty(session.Advanced
.MoreLikeThis<MockPost, Posts_ByPostCategory>(new MoreLikeThisQuery
{
DocumentId = "posts/123",
Fields = new[] { "Body" }
}).ToList());
using (var session = store.OpenSession())
{
Assert.NotEmpty(session.Advanced
.MoreLikeThis<MockPost, Posts_ByPostCategory>(new MoreLikeThisQuery
{
DocumentId = "posts/123",
Fields = new[] { "Body" }
}).ToList());

Assert.Empty(session.Advanced
.MoreLikeThis<MockPost, Posts_ByPostCategory>(new MoreLikeThisQuery
{
DocumentId = "posts/123",
Fields = new[] { "Body" },
AdditionalQuery = "Category:IT"
}).ToList());
Assert.Empty(session.Advanced
.MoreLikeThis<MockPost, Posts_ByPostCategory>(new MoreLikeThisQuery
{
DocumentId = "posts/123",
Fields = new[] { "Body" },
AdditionalQuery = "Category:IT"
}).ToList());
}
}
}
}
Expand Down
108 changes: 108 additions & 0 deletions Raven.Tests.Issues/RavenDB_4836.cs
@@ -0,0 +1,108 @@
using System.Collections.Generic;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.BR;
using Lucene.Net.Documents;
using Raven.Abstractions.Data;
using Raven.Abstractions.Indexing;
using Raven.Client.Bundles.MoreLikeThis;
using Raven.Client.Indexes;
using Raven.Database.Indexing;
using Raven.Database.Plugins;
using Raven.Tests.Helpers;
using Xunit;

namespace Raven.Tests.Issues
{
public class RavenDB_4836 : RavenTestBase
{
[Fact]
public void MoreLikeThisQueryDefaultAnalyzer()
{
using (var store = NewDocumentStore())
{
store.Configuration.Catalog.Catalogs.Add(new TypeCatalog(typeof(MyAnalyzerGenerator)));
store.ExecuteIndex(new Posts_Index());

using (var session = store.OpenSession())
{
var dataQueriedFor = new Post { Id = "posts/123", Body = "Isto é um teste. Não é fixe? Espero que o teste passe!" };

var someData = new List<Post>
{
dataQueriedFor,
new Post { Id = "posts/234", Body = "Tenho um teste amanhã. Detesto ter testes" },
new Post { Id = "posts/3456", Body = "Bolo é espetacular" },
new Post { Id = "posts/3457", Body = "Este document só tem a palavra teste uma vez" },
new Post { Id = "posts/3458", Body = "teste", },
new Post { Id = "posts/3459", Body = "testes", }
};
someData.ForEach(session.Store);

session.SaveChanges();
}

WaitForIndexing(store);

using (var session = store.OpenSession())
{
Assert.NotEmpty(session.Advanced
.MoreLikeThis<Post, Posts_Index>(new MoreLikeThisQuery
{
DocumentId = "posts/123",
Fields = new[] { "Body" },
DefaultAnalyzerName = typeof(BrazilianAnalyzer).AssemblyQualifiedName
}).ToList());
}
}
}
}

class MyAnalyzerGenerator : AbstractAnalyzerGenerator
{
public override Analyzer GenerateAnalyzerForIndexing(string indexName, Document document, Analyzer previousAnalyzer)
{
return DefaultAnalyzer(previousAnalyzer);
}

public override Analyzer GenerateAnalyzerForQuerying(string indexName, string query, Analyzer previousAnalyzer)
{
return DefaultAnalyzer(previousAnalyzer);
}

private static Analyzer DefaultAnalyzer(Analyzer previousAnalyzer)
{
var perFieldAnalyzerWrapper = previousAnalyzer as RavenPerFieldAnalyzerWrapper;
if (perFieldAnalyzerWrapper != null)
{
perFieldAnalyzerWrapper.AddAnalyzer("Body", new BrazilianAnalyzer(Lucene.Net.Util.Version.LUCENE_30));
}

return previousAnalyzer;
}
}

class Post
{
public string Id { get; set; }
public string Title { get; set; }
public string Body { get; set; }
}

class Posts_Index : AbstractIndexCreationTask<Post>
{
public Posts_Index()
{
Map = posts => from post in posts
select new
{
post.Id,
post.Title,
post.Body
};

Stores.Add(x => x.Body, FieldStorage.Yes);
}
}
}

0 comments on commit f668c0b

Please sign in to comment.