-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
114 lines (104 loc) · 4.02 KB
/
Program.cs
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
using LSO.SemanticSearch.Api;
using LSO.SemanticSearch.Client;
using LSO.SemanticSearch.Model;
using Minio;
using Newtonsoft.Json.Linq;
namespace semantic_search_sample_dotnet
{
static class Program
{
static async Task Main()
{
// Init
var searcherConf = new Configuration
{
BasePath = "http://localhost:8082/api/v1"
};
var indexerConf = new Configuration
{
BasePath = "http://localhost:8081/api/v1"
};
var searchClient = new SearchApi(searcherConf);
var indexerClient = new IndexApi(indexerConf);
var minio = MinioUtil.InitClient("localhost:9000", "admin", "adminadmin");
var indexName = "example";
// create Index
try
{
await indexerClient.CreateIndexAsync(new CreateIndexRequest(
indexName, new List<IndexFieldMapping>
{
new("enabled", IndexFieldMapping.TypeEnum.BOOLEAN),
new("type", IndexFieldMapping.TypeEnum.TEXT)
}
));
}
catch (Exception e)
{
Console.WriteLine("Error creating index (maybe it exists?)");
}
// index the document
// searching in ./test/*
await IndexDocument(indexName, "Bärlauch.docx", minio, indexerClient);
await IndexDocument(indexName, "Heilpflanze.docx", minio, indexerClient);
await IndexDocument(indexName, "Knoblauch.docx", minio, indexerClient);
await IndexDocument(indexName, "Schnittlauch.docx", minio, indexerClient);
await IndexDocument(indexName, "Zwiebel.docx", minio, indexerClient);
// Search the index
var searchRequest = new SearchQueryRequest(new List<TextQuery>
{
new()
{
Query = "Wofür wird knoblauch verwendet?",
SemanticWeight = 1,
FullTextWeight = 1,
}
}, /*new List<SearchQueryRequestFilterQueriesInner>
{
new(new KeyValueFilter(
"type",
new KeyValueFilterValue(true),
KeyValueFilter.ModalTypeEnum.MUST
))
}, */
limit:10, offset:0, minScore: 1M);
var results = await searchClient.SearchIndexAsync(indexName, searchRequest);
Console.WriteLine(results.ToJson());
}
static async Task IndexDocument(string indexName, string fileName, IMinioClient minio, IndexApi indexer)
{
var documentId = Guid.NewGuid();
// create and upload the __meta__.json
var meta = new JObject(
new JProperty("meta", new JObject(
new JProperty("enabled", true),
new JProperty("type", "text")
)),
new JProperty("files", new JArray(
new JObject(
new JProperty("meta", new JObject()),
new JProperty("file_name", fileName)
)
))
);
await MinioUtil.UploadStringAsFile(meta.ToString(),
$"{documentId}/__meta__.json",
indexName,
minio
);
// Upload the file to index
await MinioUtil.UploadFile(
$"test/{fileName}",
$"{documentId}/{fileName}",
indexName,
minio
);
// Start the indexing
await indexer.CreateDocumentAsync(
indexName,
documentId.ToString(),
new CreateDocumentRequest(documentId.ToString())
);
}
}
}