Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add advanced query language support. #9

Merged
merged 2 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
324 changes: 275 additions & 49 deletions README.md

Large diffs are not rendered by default.

36 changes: 25 additions & 11 deletions src/ZoneTree.FullTextSearch.Playground/SearchEngineApp.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
using System.Diagnostics;
using System.Runtime;
using ZoneTree.FullTextSearch.SearchEngines;
using ZoneTree.FullTextSearch.Core.Tokenizer;
using ZoneTree.FullTextSearch.Core.Util;
using ZoneTree.FullTextSearch.Normalizers;
using ZoneTree.FullTextSearch.Hashing;
using ZoneTree.FullTextSearch.Tokenizer;
using ZoneTree.FullTextSearch.Misc;

namespace ZoneTree.FullTextSearch.Playground;

public sealed class SearchEngineApp : IDisposable
{
readonly string DataPath = "data";

public readonly string DefaultIndexPath = @"D:\code\modern";
public readonly string DefaultIndexPath = @"D:\code";

public readonly string DefaultFilePattern = "*.cs";

readonly bool UseSecondaryIndex = false;

readonly bool UseDiacriticNormalizer = false;

readonly HashedSearchEngine<long> SearchEngine;

readonly RecordTable<long, string> RecordTable;

public SearchEngineApp()
{
var hashGenerator = UseDiacriticNormalizer ? new NormalizableHashCodeGenerator(
new DiacriticNormalizer(), false) : null;

SearchEngine = new HashedSearchEngine<long>(
DataPath,
UseSecondaryIndex,
new WordTokenizer(3));
new WordTokenizer(3),
hashCodeGenerator: hashGenerator);
RecordTable = new RecordTable<long, string>(DataPath);
}

Expand Down Expand Up @@ -59,7 +67,7 @@ void MainMenu()
case "1":
var o = ConfigureIndex();
CreateIndex(o.indexPath, o.pattern, true);
return;
break;
case "2":
Search();
break;
Expand Down Expand Up @@ -258,20 +266,20 @@ void Search()
while (true)
{
Console.Clear();
Console.WriteLine("Start with '-' to delete the search results.");
Console.WriteLine("Start with '[DEL]' to delete the search results.");
Console.WriteLine("Enter search query (or 'q' to return to main menu):");
var text = Console.ReadLine();
if (text.Equals("q", StringComparison.InvariantCultureIgnoreCase)) break;

var isDeleteRequest = false;
if (text.StartsWith('-'))
if (text.StartsWith("[DEL]", StringComparison.InvariantCultureIgnoreCase))
{
text = text.Substring(1);
text = text.Substring(5);
isDeleteRequest = true;
}

var sw = Stopwatch.StartNew();
var result = SearchEngine.Search(text, true, 0, 5);
var result = SearchEngine.Search(text, 0, 5);
var elapsed = sw.ElapsedMilliseconds;
Console.WriteLine($"Found {result.Length} records in {elapsed} ms.");

Expand All @@ -289,19 +297,25 @@ void Search()
}

var i = 1;
var continueLoop = false;
foreach (var record in result)
{
RecordTable.TryGetValue(record, out var path);
Console.WriteLine($"{i}. {path}");

if (i % 10 == 0)
{
Console.WriteLine("Press 'Enter' to continue viewing the next set of records, or 'q' to return to the main menu.");
Console.WriteLine("Press 'Enter' to continue viewing the next set of records, or 'q' to return to the search.");
var key = Console.ReadKey().KeyChar;
if (key == 'q' || key == 'Q') return;
if (key == 'q' || key == 'Q')
{
continueLoop = true;
break;
}
}
++i;
}
if (continueLoop) continue;

Console.WriteLine("End of results. Press any key to perform another search or 'q' to return to the main menu...");
if (Console.ReadKey().KeyChar == 'q') break;
Expand Down
Loading
Loading