-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchClientHelper.cs
109 lines (96 loc) · 3.57 KB
/
SearchClientHelper.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
using Microsoft.Azure.Search;
using Microsoft.Azure.Search.Models;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
namespace AzureSearchDocuments
{
public class SearchClientHelper
{
private static SearchServiceClient _searchClient;
private string IndexName;
public static string errorMessage;
public SearchClientHelper()
{
string searchServiceName = ConfigurationManager.AppSettings["SearchServiceName"];
string searchServiceApiKey = ConfigurationManager.AppSettings["SearchServiceApiKey"];
IndexName = ConfigurationManager.AppSettings["SearchIndex"];
try
{
_searchClient = new SearchServiceClient(searchServiceName, new SearchCredentials(searchServiceApiKey));
}
catch (Exception e)
{
errorMessage = e.Message.ToString();
}
}
public JObject GetFacets(string searchText, string facetName, int maxCount = 30)
{
// Execute search based on query string
try
{
SearchParameters sp = new SearchParameters()
{
SearchMode = SearchMode.Any,
//Top = 0,
Facets = new List<String>() { $"{facetName}" },
HighlightPreTag = "<em><b>",
HighlightPostTag="</b></em>",
HighlightFields= new List<String>() { "text" },
QueryType = QueryType.Full
};
DocumentSearchResult response = _searchClient.Indexes.GetClient(IndexName).Documents.Search(searchText, sp);
JObject dataset = new JObject();
if (response != null)
{
dataset =
new JObject(
new JProperty("facets",
new JArray(
from f in response.Facets
select new JObject(
new JProperty("key", f.Key),
new JProperty("value",
new JArray(
from v in f.Value
select new JObject(
new JProperty("type", v.Type),
new JProperty("from", v.From),
new JProperty("to", v.To),
new JProperty("value", v.Value),
new JProperty("count", v.Count)
))),
new JProperty("min", null),
new JProperty("max", null),
new JProperty("type", "System.String")
))),
new JProperty("results",
new JArray(
from r in response.Results
select new JObject(
new JProperty("score", r.Score),
new JProperty("highlights", r.Highlights !=null ?
new JObject(r.Highlights.Select(field => new JProperty(field.Key, field.Value))):
null),
new JProperty("document",
new JObject(r.Document.Select(field => new JProperty(field.Key, field.Value)))
)))),
new JProperty("count", response.Results.Count)
);
}
return dataset;
}
catch (Exception ex)
{
Console.WriteLine("Error querying index: {0}\r\n", ex.Message.ToString());
}
return null;
}
private static string GetAppSetting(string key)
{
return Environment.GetEnvironmentVariable(key, EnvironmentVariableTarget.Process);
}
}
}