-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Making an InQuery work. (Not in use yet)
- Loading branch information
jmd
authored and
jmd
committed
Sep 13, 2023
1 parent
1d0460e
commit c9049bf
Showing
19 changed files
with
2,576 additions
and
225 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using DotJEM.Json.Index2.Documents.Fields; | ||
using DotJEM.Json.Index2.IO; | ||
using DotJEM.Json.Index2.QueryParsers.Query; | ||
using DotJEM.Json.Index2.Results; | ||
using DotJEM.Json.Index2.Searching; | ||
using Lucene.Net.Analysis.Standard; | ||
using Lucene.Net.Index; | ||
using Lucene.Net.Search; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using NUnit.Framework; | ||
|
||
namespace DotJEM.Json.Index2.QueryParsers.Test; | ||
|
||
public class JsonIndexTest | ||
{ | ||
[Test] | ||
public async Task SayHello_ReturnsHello() | ||
{ | ||
IJsonIndex index = new JsonIndexBuilder("myIndex") | ||
.UsingMemmoryStorage() | ||
.WithAnalyzer(cfg => new StandardAnalyzer(cfg.Version)) | ||
.WithFieldResolver(new FieldResolver("uuid", "type")) | ||
.UseSimplifiedLuceneQueryParser() | ||
.Build(); | ||
|
||
IJsonIndexWriter writer = index.CreateWriter(); | ||
writer.Create(JObject.FromObject(new { uuid = Guid.NewGuid(), type = "CAR" })); | ||
writer.Create(JObject.FromObject(new { uuid = Guid.NewGuid(), type = "CAR" })); | ||
writer.Create(JObject.FromObject(new { uuid = Guid.NewGuid(), type = "CAR" })); | ||
writer.Create(JObject.FromObject(new { uuid = Guid.NewGuid(), type = "CAR" })); | ||
writer.Create(JObject.FromObject(new { uuid = Guid.NewGuid(), type = "CAR" })); | ||
writer.Commit(); | ||
|
||
IJsonIndexSearcher? searcher = index.CreateSearcher(); | ||
int count = searcher.Search("type:CAR").Count(); | ||
//int count = searcher.Search(new MatchAllDocsQuery()).Count(); | ||
Assert.AreEqual(5, count); | ||
} | ||
[Test] | ||
public async Task SayHello_ReturnsHell2o() | ||
{ | ||
IJsonIndex index = new JsonIndexBuilder("myIndex") | ||
.UsingMemmoryStorage() | ||
.WithAnalyzer(cfg => new StandardAnalyzer(cfg.Version)) | ||
.WithFieldResolver(new FieldResolver("uuid", "type")) | ||
.UseSimplifiedLuceneQueryParser() | ||
.Build(); | ||
|
||
IJsonIndexWriter writer = index.CreateWriter(); | ||
writer.Create(JObject.FromObject(new { uuid = Guid.NewGuid(), type = "AXE" })); | ||
writer.Create(JObject.FromObject(new { uuid = Guid.NewGuid(), type = "AXE" })); | ||
writer.Create(JObject.FromObject(new { uuid = Guid.NewGuid(), type = "AXE" })); | ||
|
||
writer.Create(JObject.FromObject(new { uuid = Guid.NewGuid(), type = "CAR" })); | ||
writer.Create(JObject.FromObject(new { uuid = Guid.NewGuid(), type = "CAR" })); | ||
writer.Create(JObject.FromObject(new { uuid = Guid.NewGuid(), type = "CAR" })); | ||
|
||
writer.Create(JObject.FromObject(new { uuid = Guid.NewGuid(), type = "FAT" })); | ||
writer.Create(JObject.FromObject(new { uuid = Guid.NewGuid(), type = "FAT" })); | ||
writer.Create(JObject.FromObject(new { uuid = Guid.NewGuid(), type = "FAT" })); | ||
|
||
writer.Create(JObject.FromObject(new { uuid = Guid.NewGuid(), type = "CAT" })); | ||
writer.Create(JObject.FromObject(new { uuid = Guid.NewGuid(), type = "CAT" })); | ||
writer.Create(JObject.FromObject(new { uuid = Guid.NewGuid(), type = "CAT" })); | ||
|
||
writer.Create(JObject.FromObject(new { uuid = Guid.NewGuid(), type = "HAT" })); | ||
writer.Create(JObject.FromObject(new { uuid = Guid.NewGuid(), type = "HAT" })); | ||
writer.Create(JObject.FromObject(new { uuid = Guid.NewGuid(), type = "HAT" })); | ||
writer.Commit(); | ||
|
||
|
||
IJsonIndexSearcher? searcher = index.CreateSearcher(); | ||
//new TermQuery(new Term("type", "AXE")) | ||
|
||
//ISearch? search = searcher.Search(new InQuery("type", "car", "foo", "fat")); | ||
ISearch? search = searcher.Search("type IN (car, foo, fat)"); | ||
//int count = searcher.Search(new MatchAllDocsQuery()).Count(); | ||
|
||
foreach (SearchResult result in search.Take(100).Execute()) | ||
{ | ||
Console.Write(result.Data.ToString(Formatting.None)); | ||
} | ||
|
||
Assert.That(search.Count(), Is.EqualTo(6)); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/DotJEM.Json.Index2.QueryParsers.Test/DotJEM.Json.Index2.QueryParsers.Test.csproj
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<Import Project="..\DotJEM.Json.Index2.common.props" /> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<GeneratePackageOnBuild>False</GeneratePackageOnBuild> | ||
<GenerateDocumentationFile>False</GenerateDocumentationFile> | ||
<ProduceReferenceAssembly>False</ProduceReferenceAssembly> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" /> | ||
<PackageReference Include="NUnit" Version="3.13.3" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\DotJEM.Json.Index2.QueryParsers\DotJEM.Json.Index2.QueryParsers.csproj" /> | ||
<ProjectReference Include="..\DotJEM.Json.Index2\DotJEM.Json.Index2.csproj" /> | ||
</ItemGroup> | ||
|
||
|
||
</Project> |
34 changes: 34 additions & 0 deletions
34
src/DotJEM.Json.Index2.QueryParsers/IndexQueryParserExtensions.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System; | ||
using DotJEM.Json.Index2.Configuration; | ||
using DotJEM.Json.Index2.Documents.Info; | ||
using DotJEM.Json.Index2.Results; | ||
using DotJEM.Json.Index2.Searching; | ||
using Lucene.Net.Analysis; | ||
|
||
namespace DotJEM.Json.Index2.QueryParsers; | ||
|
||
public static class IndexQueryParserExtensions | ||
{ | ||
public static IJsonIndexBuilder UseSimplifiedLuceneQueryParser(this IJsonIndexBuilder self) | ||
=> self.TryWithService<ILuceneQueryParser>(config=>new SimplifiedLuceneQueryParser(config.FieldInformationManager, config.Analyzer)); | ||
|
||
public static ISearch Search(this IJsonIndexSearcher self, string query) | ||
{ | ||
ILuceneQueryParser parser = self.Index.ResolveParser(); | ||
LuceneQueryInfo queryInfo = parser.Parse(query); | ||
return self.Search(queryInfo.Query).OrderBy(queryInfo.Sort); | ||
} | ||
|
||
public static ISearch Search(this IJsonIndex self, string query) | ||
{ | ||
ILuceneQueryParser parser = self.ResolveParser(); | ||
LuceneQueryInfo queryInfo = parser.Parse(query); | ||
return self.CreateSearcher().Search(queryInfo.Query).OrderBy(queryInfo.Sort); | ||
} | ||
|
||
private static ILuceneQueryParser ResolveParser(this IJsonIndex self) | ||
{ | ||
return self.Configuration.Get<ILuceneQueryParser>() ?? throw new Exception("Query parser not configured."); | ||
} | ||
|
||
} |
35 changes: 0 additions & 35 deletions
35
src/DotJEM.Json.Index2.QueryParsers/IndexSearcherExtensions.cs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.