-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
129 lines (106 loc) · 4.27 KB
/
Program.cs
File metadata and controls
129 lines (106 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using Indx.Api;
namespace IndxConsoleAppJson
{
internal class Program
{
private static void Main()
{
//
// CREATE INSTANCE
//
SearchEngine engine = new SearchEngine();
// Load a license like this: new SearchEngineJson("file.license");
// Get a developer license on https://indx.co
//
// STREAM DATA FROM FILE AND ANALYZE JSON
//
string file = "data/imdb_top10k.json";
FileStream fstream = File.Open(file, FileMode.Open, FileAccess.Read);
// INITIALIZE
engine.Init(fstream);
//
// SETUP FIELDS
//
engine.GetField("Movie_Name")!.Searchable = true;
engine.GetField("Movie_Name")!.Weight = Weight.High;
engine.GetField("Stars")!.Searchable = true;
engine.GetField("Stars")!.Weight = Weight.Low;
engine.GetField("Description")!.Searchable = true;
engine.GetField("Description")!.Weight = Weight.Med;
engine.GetField("Year_of_Release")!.Facetable = true;
// LOAD JSON
engine.Load(fstream);
fstream.Close(); // close file stream
// RUN INDEXING
engine.Index();
Console.Clear();
bool continueSearch = true;
while (continueSearch)
{
//
// SET UP SEARCH QUERY
//
Console.Write("🔍 Search for a movie: ");
var text = Console.ReadLine() ?? ""; // pattern to be searched for
int num = 10;
Query query = new Query(text, num);
//
// SEARCH
//
var result = engine.Search(query);
if (result != null)
{
foreach (var rec in result.Records)
{
long key = rec.DocumentKey;
string json = engine.GetJsonDataOfKey(key); // Get JSON object
Console.WriteLine(json);
Console.WriteLine(rec.Score);
}
}
//
// PRINT FACETS
//
// Enable facets in the query. This is by default set to false.
query.EnableFacets = true;
// Search again with facets enabled
result = engine.Search(query);
// Set up KeyValuePair for facets
Dictionary<string, KeyValuePair<string, int>[]>? facets = result.Facets;
// Print facets
foreach (var field in engine.DocumentFields.GetFacetableFieldList())
{
var fName = field.Name;
if (facets != null)
{
if (facets.TryGetValue(fName, out KeyValuePair<string, int>[]? histogram))
{
if (histogram != null)
{
Console.WriteLine("");
Console.WriteLine(field.Name);
foreach (var item in histogram)
{
Console.WriteLine($"{item.Key} ({item.Value})");
};
}
}
}
}
Console.WriteLine("");
//
// META INFORMATION
//
Console.WriteLine($"\nCoverage matches found: {result.TruncationIndex + 1}"); // this will be 0 if query has large typos
Console.WriteLine("Version: " + engine.Status.Version);
// Continue prompt
Console.ForegroundColor = ConsoleColor.DarkBlue;
Console.Write("\nPress ESC to quit or any other key to continue.");
Console.ResetColor();
ConsoleKeyInfo keyInfo = Console.ReadKey(true);
if (keyInfo.Key == ConsoleKey.Escape) continueSearch = false;
else Console.Clear();
}
}
}
}