Skip to content

Commit

Permalink
Making an InQuery work. (Not in use yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmd authored and jmd committed Sep 13, 2023
1 parent 1d0460e commit c9049bf
Show file tree
Hide file tree
Showing 19 changed files with 2,576 additions and 225 deletions.
20 changes: 11 additions & 9 deletions src/DotJEM.Json.Index2.Contexts.Test/LuceneIndexContextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,22 @@ public async Task SayHello_ReturnsHello()
{
IJsonIndexContextBuilder builder = new JsonIndexContextBuilder();
builder
.ByDefault(x => x.UsingMemmoryStorage().Build());
.ByDefault(x => x
.UsingMemmoryStorage()
.WithAnalyzer(cfg => new StandardAnalyzer(cfg.Version))
.WithFieldResolver(new FieldResolver("uuid", "type"))
.Build());
builder
.For("IndexName", b => b.UsingStorage(new RamJsonIndexStorage()).Build());


.For("IndexName", x => x
.UsingMemmoryStorage()
.WithAnalyzer(cfg => new StandardAnalyzer(cfg.Version))
.WithFieldResolver(new FieldResolver("uuid", "type"))
.Build());

IJsonIndexContext context = builder.Build();
context.Open("IndexName");

IJsonIndex index = new JsonIndexBuilder("myIndex")
.UsingMemmoryStorage()
.WithAnalyzer(cfg => new StandardAnalyzer(cfg.Version))
.WithFieldResolver(new FieldResolver("uuid", "type"))
.Build();
IJsonIndex index = context.Open("IndexName");

IJsonIndexWriter writer = index.CreateWriter();
writer.Create(JObject.FromObject(new { uuid = Guid.NewGuid(), type = "CAR" }));
Expand Down
42 changes: 34 additions & 8 deletions src/DotJEM.Json.Index2.Contexts/LuceneIndexContext.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using DotJEM.Json.Index2.Configuration;
using DotJEM.Json.Index2.Contexts.Searching;
using DotJEM.Json.Index2.Contexts.Storage;
Expand All @@ -14,7 +15,7 @@ public interface IJsonIndexContext : IJsonIndexSearcherProvider

public class JsonIndexContext : IJsonIndexContext
{
private readonly ILuceneJsonIndexFactory factory;
private readonly IJsonIndexFactory factory;
private readonly ConcurrentDictionary<string, IJsonIndex> indices = new ConcurrentDictionary<string, IJsonIndex>();

//public IServiceResolver Services { get; }
Expand All @@ -24,7 +25,7 @@ public class JsonIndexContext : IJsonIndexContext
//public JsonIndexContext(string path, IServiceCollection services = null)
// : this(new LuceneIndexContextBuilder(path), services) { }

public JsonIndexContext(ILuceneJsonIndexFactory factory)
public JsonIndexContext(IJsonIndexFactory factory)
{
this.factory = factory ?? throw new ArgumentNullException(nameof(factory));
//this.Services = resolver ?? throw new ArgumentNullException(nameof(resolver));
Expand All @@ -40,11 +41,6 @@ public IJsonIndexSearcher CreateSearcher()
return new LuceneJsonMultiIndexSearcher(indices.Values);
}
}

public interface ILuceneJsonIndexFactory
{
IJsonIndex Create(string name);
}

public interface IJsonIndexContextBuilder
{
Expand All @@ -64,12 +60,42 @@ public IJsonIndexContextBuilder ByDefault(Func<IJsonIndexBuilder, IJsonIndex> de

public IJsonIndexContextBuilder For(string name, Func<IJsonIndexBuilder, IJsonIndex> defaultConfig)
{
if (name == null) throw new ArgumentNullException(nameof(name));
if (defaultConfig == null) throw new ArgumentNullException(nameof(defaultConfig));
if (name is "*" or "") throw new ArgumentException("Invalid name for an index.", nameof(name));

configurators.AddOrUpdate(name, s => defaultConfig, (s, func) => defaultConfig);
return this;
}

public IJsonIndexContext Build()
{
return new JsonIndexContext(null);
return new JsonIndexContext(new JsonIndexFactory(new Dictionary<string, Func<IJsonIndexBuilder, IJsonIndex>>(configurators)));
}
}

public interface IJsonIndexFactory
{
IJsonIndex Create(string name);
}

public class JsonIndexFactory : IJsonIndexFactory
{
private readonly IReadOnlyDictionary<string, Func<IJsonIndexBuilder, IJsonIndex>> configurators;

public JsonIndexFactory(IReadOnlyDictionary<string, Func<IJsonIndexBuilder, IJsonIndex>> configurators)
{
this.configurators = configurators;
}

public IJsonIndex Create(string name)
{
if (configurators.TryGetValue(name, out Func<IJsonIndexBuilder, IJsonIndex> func))
return func(new JsonIndexBuilder(name));

if(configurators.TryGetValue("*", out func))
return func(new JsonIndexBuilder(name));

throw new InvalidOperationException("");
}
}
87 changes: 87 additions & 0 deletions src/DotJEM.Json.Index2.QueryParsers.Test/Class1.cs
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));
}
}
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 src/DotJEM.Json.Index2.QueryParsers/IndexQueryParserExtensions.cs
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 src/DotJEM.Json.Index2.QueryParsers/IndexSearcherExtensions.cs

This file was deleted.

Loading

0 comments on commit c9049bf

Please sign in to comment.