-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
185 lines (156 loc) · 7.09 KB
/
Program.cs
File metadata and controls
185 lines (156 loc) · 7.09 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
namespace netCoreAzureSearch
{
using System;
using Microsoft.Azure.Search;
using System.Linq;
using System.Collections.Generic;
using Microsoft.Azure.Search.Models;
class Program
{
static void Main(string[] args)
{
var azureSearchName = "[Your Azure Search Name]";
// The key to you cosmosdb
var azureSearchKey = "[Your Azure Search Key]";
//Index name variable
string indexName = "books";
//Create a service client connection
ISearchServiceClient azureSearchService = new SearchServiceClient(azureSearchName, new SearchCredentials(azureSearchKey));
//Get AzureSerch Index
ISearchIndexClient indexClient = azureSearchService.Indexes.GetClient(indexName);
//Delete AzureSearch Index if exist
Console.WriteLine( "Deleting index...\n");
if (azureSearchService.Indexes.Exists(indexName))
{
azureSearchService.Indexes.Delete(indexName);
}
//Create Index Model
Console.WriteLine( "Creating index Model...\n");
Index indexModel = new Index()
{
Name = indexName,
Fields = new[]
{
new Field("ISBN", DataType.String) { IsKey = true, IsRetrievable = true, IsFacetable = false },
new Field("Titulo", DataType.String) {IsRetrievable = true, IsSearchable = true, IsFacetable = false },
new Field("Autores", DataType.Collection(DataType.String)) {IsSearchable = true, IsRetrievable = true, IsFilterable = true, IsFacetable = false },
new Field("FechaPublicacion", DataType.DateTimeOffset) { IsFilterable = true, IsRetrievable = false, IsSortable = true, IsFacetable = false },
new Field("Categoria", DataType.String) { IsFacetable = true, IsFilterable= true, IsRetrievable = true }
}
};
//Create Index in AzureSearch
Console.WriteLine( "Creating index...\n");
var resultIndex = azureSearchService.Indexes.Create(indexModel);
//Add documents in our Index
Console.WriteLine( "Add documents...\n");
var listBooks = new BookModel().GetBooks();
indexClient.Documents.Index(IndexBatch.MergeOrUpload<BookModel>(listBooks));
System.Threading.Thread.Sleep(1000);
//Search by word
Console.WriteLine("{0}", "Searching documents 'Cloud'...\n");
Search(indexClient, searchText: "Cloud");
System.Threading.Thread.Sleep(1000);
//Search all and filter
Console.WriteLine("\n{0}", "Filter documents by Autores 'Eugenio Betts'...\n");
Search(indexClient, searchText: "*", filter: "Autores / any(t: t eq 'Eugenio Betts')");
System.Threading.Thread.Sleep(1000);
//Search all and order
Console.WriteLine("\n{0}", "order by FechaPublicacion\n");
Search(indexClient, searchText: "*", order: new List<string>() { "FechaPublicacion" });
System.Threading.Thread.Sleep(1000);
//Search all and facet
Console.WriteLine("\n{0}", "Facet by Categoria \n");
Search(indexClient, searchText: "*", facets: new List<string>() { "Categoria" });
System.Threading.Thread.Sleep(1000);
}
private static void Search(ISearchIndexClient indexClient, string searchText, string filter = null, List<string> order = null, List<string> facets = null)
{
// Execute search based on search text and optional filter
var sp = new SearchParameters();
//Add Filter
if (!String.IsNullOrEmpty(filter))
{
sp.Filter = filter;
}
//Order
if (order != null && order.Count > 0)
{
sp.OrderBy = order;
}
//facets
if (facets != null && facets.Count > 0)
{
sp.Facets = facets;
}
//Search
DocumentSearchResult<BookModel> response = indexClient.Documents.Search<BookModel>(searchText, sp);
foreach (SearchResult<BookModel> result in response.Results)
{
Console.WriteLine(result.Document + " - Score: " + result.Score);
}
if (response.Facets != null)
{
foreach (var facet in response.Facets)
{
Console.WriteLine("\n Facet Name: " + facet.Key);
foreach (var value in facet.Value)
{
Console.WriteLine("Value :" + value.Value + " - Count: " + value.Count);
}
}
}
}
}
/// <summary>
/// A simple class representing a book
/// </summary>
public class BookModel
{
public string ISBN { get; set; }
public string Titulo { get; set; }
public List<string> Autores { get; set; }
public DateTimeOffset FechaPublicacion { get; set; }
public string Categoria { get; set; }
public List<BookModel> GetBooks()
{
var listBooks = new List<BookModel>();
listBooks.Add(new BookModel()
{
ISBN = "9781430224792",
Titulo = "Windows Azure Platform (Expert's Voice in .NET)",
Categoria = "Comic",
Autores = new List<string>() {"Redkar", "Tejasw" },
FechaPublicacion = DateTimeOffset.Now.AddDays(-2)
});
listBooks.Add(new BookModel()
{
ISBN = "9780470506387",
Titulo = "Cloud Computing with the Windows Azure Platform",
Categoria = "Terror",
Autores = new List<string>() { "Roger Jennings"},
FechaPublicacion = DateTimeOffset.Now
});
listBooks.Add(new BookModel()
{
ISBN = "9780889222861",
Titulo = "Azure Blues",
Categoria = "Terror",
Autores = new List<string>() { "Gilbert", "Gerry", "Rogery Landing"},
FechaPublicacion = DateTimeOffset.Now.AddMonths(-3)
});
listBooks.Add(new BookModel()
{
ISBN = "9780735649675",
Titulo = "Moving Applications to the Cloud on the Microsoft Azure(TM) Platform",
Categoria = "Fiction",
Autores = new List<string>() { "Pace", "Eugenio Betts", "Dominic Densmore", "Scott; Dunn", "Ryan Narumoto", "Masashi Woloski", "Matias" },
FechaPublicacion = DateTimeOffset.Now
});
return listBooks;
}
public override string ToString()
{
return "ISBN: " + this.ISBN + " - titulo: " + this.Titulo + " - Autores: " + String.Join(", ", this.Autores);
}
}
}