Skip to content

Commit 1301ec8

Browse files
authored
Merge pull request #46 from FrankieTF/frankUpdate
Updated all except DotNetSample, DotNetHowToAutocomplete, DotNetHowTo…
2 parents c0961d6 + 85f5fb4 commit 1301ec8

File tree

21 files changed

+679
-574
lines changed

21 files changed

+679
-574
lines changed

DotNetETagsExplainer/DotNetETagsExplainer/DotNetETagsExplainer.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>netcoreapp2.0</TargetFramework>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="Microsoft.Azure.Search" Version="10.1.0" />
10-
<PackageReference Include="NewtonSoft.Json" Version="12.0.3" />
9+
<PackageReference Include="Azure.Search.Documents" Version="11.1.1" />
1110
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.1" />
1211
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.1" />
1312
</ItemGroup>
Lines changed: 55 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,85 @@
1-
namespace AzureSearch.SDKHowTo
1+
using Azure;
2+
using Azure.Search.Documents.Indexes;
3+
using Azure.Search.Documents.Indexes.Models;
4+
using Microsoft.Extensions.Configuration;
5+
using System;
6+
using System.Threading.Tasks;
7+
8+
namespace AzureSearch.SDKHowTo
29
{
3-
using System;
4-
using Microsoft.Azure.Search;
5-
using Microsoft.Azure.Search.Models;
6-
using Microsoft.Extensions.Configuration;
7-
using Microsoft.Rest.Azure;
8-
910
class Program
1011
{
1112
// This sample shows how ETags work by performing conditional updates and deletes
1213
// on an Azure Search index.
13-
static void Main(string[] args)
14+
static async Task Main(string[] args)
1415
{
1516
IConfigurationBuilder builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
1617
IConfigurationRoot configuration = builder.Build();
1718

18-
SearchServiceClient serviceClient = CreateSearchServiceClient(configuration);
19+
SearchIndexClient indexClient = CreateSearchIndexClient(configuration);
1920

2021
Console.WriteLine("Deleting index...\n");
21-
DeleteTestIndexIfExists(serviceClient);
22+
DeleteTestIndexIfExists(indexClient);
2223

2324
// Every top-level resource in Azure Search has an associated ETag that keeps track of which version
2425
// of the resource you're working on. When you first create a resource such as an index, its ETag is
2526
// empty.
26-
Index index = DefineTestIndex();
27+
SearchIndex index = DefineTestIndex();
28+
2729
Console.WriteLine(
28-
$"Test index hasn't been created yet, so its ETag should be blank. ETag: '{index.ETag}'");
30+
$"Test searchIndex hasn't been created yet, so its ETag should be blank. ETag: '{index.ETag}'");
2931

3032
// Once the resource exists in Azure Search, its ETag will be populated. Make sure to use the object
31-
// returned by the SearchServiceClient! Otherwise, you will still have the old object with the
33+
// returned by the SearchIndexClient! Otherwise, you will still have the old object with the
3234
// blank ETag.
33-
Console.WriteLine("Creating index...\n");
34-
index = serviceClient.Indexes.Create(index);
35-
35+
//Console.WriteLine("Creating index...\n");
36+
index = indexClient.CreateIndex(index);
3637
Console.WriteLine($"Test index created; Its ETag should be populated. ETag: '{index.ETag}'");
3738

39+
3840
// ETags let you do some useful things you couldn't do otherwise. For example, by using an If-Match
39-
// condition, we can update an index using CreateOrUpdate and be guaranteed that the update will only
41+
// condition, we can update an index using CreateOrUpdateIndexAsync() and be guaranteed that the update will only
4042
// succeed if the index already exists.
41-
index.Fields.Add(new Field("name", AnalyzerName.EnMicrosoft));
42-
index =
43-
serviceClient.Indexes.CreateOrUpdate(
44-
index,
45-
accessCondition: AccessCondition.GenerateIfExistsCondition());
43+
index.Fields.Add(new SearchField("name", SearchFieldDataType.String) { AnalyzerName = LexicalAnalyzerName.EnMicrosoft });
44+
index = indexClient.CreateOrUpdateIndex(index);
4645

46+
index = await indexClient.CreateOrUpdateIndexAsync(index);
4747
Console.WriteLine(
48-
$"Test index updated; Its ETag should have changed since it was created. ETag: '{index.ETag}'");
48+
$"Test searchIndex updated; Its ETag should have changed since it was created. ETag: '{index.ETag}'");
4949

5050
// More importantly, ETags protect you from concurrent updates to the same resource. If another
5151
// client tries to update the resource, it will fail as long as all clients are using the right
5252
// access conditions.
53-
Index indexForClient1 = index;
54-
Index indexForClient2 = serviceClient.Indexes.Get("test");
53+
SearchIndex indexForClientUpdate = index;
54+
SearchIndex indexForClientUpdateFailed = indexClient.GetIndex("test");
5555

5656
Console.WriteLine("Simulating concurrent update. To start, both clients see the same ETag.");
57-
Console.WriteLine($"Client 1 ETag: '{indexForClient1.ETag}' Client 2 ETag: '{indexForClient2.ETag}'");
57+
Console.WriteLine($"ClientUpdate ETag: '{indexForClientUpdate.ETag}' ClientUpdateFailed ETag: '{indexForClientUpdateFailed.ETag}'");
5858

59-
// Client 1 successfully updates the index.
60-
indexForClient1.Fields.Add(new Field("a", DataType.Int32));
61-
indexForClient1 =
62-
serviceClient.Indexes.CreateOrUpdate(
63-
indexForClient1,
64-
accessCondition: AccessCondition.IfNotChanged(indexForClient1));
59+
// indexForClientUpdate successfully updates the index.
60+
indexForClientUpdate.Fields.Add(new SearchField("a", SearchFieldDataType.Int32));
61+
indexForClientUpdate = indexClient.CreateOrUpdateIndex(indexForClientUpdate);
6562

66-
Console.WriteLine($"Test index updated by client 1; ETag: '{indexForClient1.ETag}'");
63+
Console.WriteLine($"Test index updated by ClientUpdate; ETag: '{indexForClientUpdate.ETag}'");
6764

68-
// Client 2 tries to update the index, but fails, thanks to the ETag check.
65+
// indexForClientUpdateFailed tries to update the index, but fails, thanks to the ETag check.
6966
try
7067
{
71-
indexForClient2.Fields.Add(new Field("b", DataType.Boolean));
72-
serviceClient.Indexes.CreateOrUpdate(
73-
indexForClient2,
74-
accessCondition: AccessCondition.IfNotChanged(indexForClient2));
68+
indexForClientUpdateFailed.Fields.Add(new SearchField("b", SearchFieldDataType.Boolean));
69+
indexClient.CreateOrUpdateIndex(indexForClientUpdateFailed);
7570

7671
Console.WriteLine("Whoops; This shouldn't happen");
7772
Environment.Exit(1);
7873
}
79-
catch (CloudException e) when (e.IsAccessConditionFailed())
74+
catch (RequestFailedException e) when (e.Status == 400)
8075
{
81-
Console.WriteLine("Client 2 failed to update the index, as expected.");
76+
Console.WriteLine("ClientUpdateFailed failed to update the index, as expected.");
8277
}
8378

8479
// You can also use access conditions with Delete operations. For example, you can implement an
8580
// atomic version of the DeleteTestIndexIfExists method from this sample like this:
8681
Console.WriteLine("Deleting index...\n");
87-
serviceClient.Indexes.Delete("test", accessCondition: AccessCondition.GenerateIfExistsCondition());
82+
indexClient.DeleteIndex("test");
8883

8984
// This is slightly better than using the Exists method since it makes only one round trip to
9085
// Azure Search instead of potentially two. It also avoids an extra Delete request in cases where
@@ -96,29 +91,33 @@ static void Main(string[] args)
9691
Console.ReadKey();
9792
}
9893

99-
private static SearchServiceClient CreateSearchServiceClient(IConfigurationRoot configuration)
94+
private static SearchIndexClient CreateSearchIndexClient(IConfigurationRoot configuration)
10095
{
101-
string searchServiceName = configuration["SearchServiceName"];
96+
string searchServicEndpoint = configuration["SearchServicEndpoint"];
10297
string adminApiKey = configuration["SearchServiceAdminApiKey"];
10398

104-
SearchServiceClient serviceClient =
105-
new SearchServiceClient(searchServiceName, new SearchCredentials(adminApiKey));
106-
return serviceClient;
99+
SearchIndexClient indexClient =
100+
new SearchIndexClient(new Uri(searchServicEndpoint), new AzureKeyCredential(adminApiKey));
101+
return indexClient;
107102
}
108103

109-
private static void DeleteTestIndexIfExists(SearchServiceClient serviceClient)
104+
private static void DeleteTestIndexIfExists(SearchIndexClient indexClient)
110105
{
111-
if (serviceClient.Indexes.Exists("test"))
106+
try
107+
{
108+
if (indexClient.GetIndex("test") != null)
109+
{
110+
indexClient.DeleteIndex("test");
111+
}
112+
}
113+
catch (RequestFailedException e) when (e.Status == 404)
112114
{
113-
serviceClient.Indexes.Delete("test");
115+
//if exception occurred and status is "Not Found", this is work as expect
116+
Console.WriteLine("Failed to find index and this is because it's not there.");
114117
}
115118
}
116119

117-
private static Index DefineTestIndex() =>
118-
new Index()
119-
{
120-
Name = "test",
121-
Fields = new[] { new Field("id", DataType.String) { IsKey = true } }
122-
};
120+
private static SearchIndex DefineTestIndex() =>
121+
new SearchIndex("test", new[] { new SearchField("id", SearchFieldDataType.String) { IsKey = true } });
123122
}
124-
}
123+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"SearchServiceName": "Put your search service name here",
3-
"SearchServiceAdminApiKey": "Put your primary or secondary API key here"
2+
"SearchServicEndpoint": "Your Search Service Endpoint",
3+
"SearchServiceAdminApiKey": "Your Search Service Admin APikey"
44
}

DotNetHowTo/DotNetHowTo/Address.Methods.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
namespace AzureSearch.SDKHowTo
2-
{
3-
using System;
4-
using System.Text;
5-
using Newtonsoft.Json;
1+
using System;
2+
using System.Text;
3+
using System.Text.Json.Serialization;
64

5+
namespace AzureSearch.SDKHowTo
6+
{
77
public partial class Address
88
{
99
// This implementation of ToString() is only for the purposes of the sample console application.

DotNetHowTo/DotNetHowTo/Address.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
1-
using System;
2-
using Microsoft.Azure.Search;
3-
using Microsoft.Azure.Search.Models;
4-
using Newtonsoft.Json;
1+
using Azure.Search.Documents.Indexes;
52

63
namespace AzureSearch.SDKHowTo
74
{
85
public partial class Address
96
{
10-
[IsSearchable]
7+
[SearchableField(IsFilterable = true)]
118
public string StreetAddress { get; set; }
129

13-
[IsSearchable, IsFilterable, IsSortable, IsFacetable]
10+
[SearchableField(IsFilterable = true, IsSortable = true, IsFacetable = true)]
1411
public string City { get; set; }
1512

16-
[IsSearchable, IsFilterable, IsSortable, IsFacetable]
13+
[SearchableField(IsFilterable = true, IsSortable = true, IsFacetable = true)]
1714
public string StateProvince { get; set; }
1815

19-
[IsSearchable, IsFilterable, IsSortable, IsFacetable]
16+
[SearchableField(IsFilterable = true, IsSortable = true, IsFacetable = true)]
2017
public string PostalCode { get; set; }
2118

22-
[IsSearchable, IsFilterable, IsSortable, IsFacetable]
19+
[SearchableField(IsFilterable = true, IsSortable = true, IsFacetable = true)]
2320
public string Country { get; set; }
2421
}
2522
}

DotNetHowTo/DotNetHowTo/DotNetHowTo.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="Microsoft.Azure.Search" Version="10.1.0" />
9+
<PackageReference Include="Azure.Search.Documents" Version="11.1.1" />
1010
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
1111
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
12+
<PackageReference Include="Microsoft.Spatial" Version="7.7.0" />
13+
<PackageReference Include="System.Text.Json" Version="4.7.2" />
1214
</ItemGroup>
1315

1416
<ItemGroup>

DotNetHowTo/DotNetHowTo/Hotel.Methods.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
namespace AzureSearch.SDKHowTo
2-
{
3-
using System;
4-
using System.Text;
1+
using System;
2+
using System.Text;
53

4+
namespace AzureSearch.SDKHowTo
5+
{
66
public partial class Hotel
77
{
88
// This implementation of ToString() is only for the purposes of the sample console application.
@@ -31,7 +31,7 @@ public override string ToString()
3131
{
3232
builder.AppendFormat("Description (French): {0}\n", DescriptionFr);
3333
}
34-
34+
3535
if (!String.IsNullOrEmpty(Category))
3636
{
3737
builder.AppendFormat("Category: {0}\n", Category);

DotNetHowTo/DotNetHowTo/Hotel.cs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,33 @@
1-
namespace AzureSearch.SDKHowTo
2-
{
3-
using System;
4-
using Microsoft.Azure.Search;
5-
using Microsoft.Azure.Search.Models;
6-
using Microsoft.Spatial;
7-
using Newtonsoft.Json;
1+
using System;
2+
using Microsoft.Spatial;
3+
using System.Text.Json.Serialization;
4+
using Azure.Search.Documents.Indexes;
5+
using Azure.Search.Documents.Indexes.Models;
86

7+
namespace AzureSearch.SDKHowTo
8+
{
99
public partial class Hotel
1010
{
11-
[System.ComponentModel.DataAnnotations.Key]
12-
[IsFilterable]
11+
[SimpleField(IsKey = true, IsFilterable = true)]
1312
public string HotelId { get; set; }
1413

15-
[IsSearchable, IsSortable]
14+
[SearchableField(IsSortable = true)]
1615
public string HotelName { get; set; }
1716

18-
[IsSearchable]
19-
[Analyzer(AnalyzerName.AsString.EnLucene)]
17+
[SearchableField(AnalyzerName = LexicalAnalyzerName.Values.EnLucene)]
2018
public string Description { get; set; }
2119

22-
[IsSearchable]
23-
[Analyzer(AnalyzerName.AsString.FrLucene)]
24-
[JsonProperty("Description_fr")]
20+
[SearchableField(AnalyzerName = LexicalAnalyzerName.Values.FrLucene)]
21+
[JsonPropertyName("Description_fr")]
2522
public string DescriptionFr { get; set; }
2623

27-
[IsSearchable, IsFilterable, IsSortable, IsFacetable]
24+
[SearchableField(IsFilterable = true, IsSortable = true, IsFacetable = true)]
2825
public string Category { get; set; }
2926

30-
[IsSearchable, IsFilterable, IsFacetable]
27+
[SearchableField(IsFilterable = true, IsFacetable = true)]
3128
public string[] Tags { get; set; }
3229

33-
[IsFilterable, IsSortable, IsFacetable]
30+
[SimpleField(IsFilterable = true, IsSortable = true, IsFacetable = true)]
3431
public bool? ParkingIncluded { get; set; }
3532

3633
// SmokingAllowed reflects whether any room in the hotel allows smoking.
@@ -39,15 +36,16 @@ public partial class Hotel
3936
[JsonIgnore]
4037
public bool? SmokingAllowed => (Rooms != null) ? Array.Exists(Rooms, element => element.SmokingAllowed == true) : (bool?)null;
4138

42-
[IsFilterable, IsSortable, IsFacetable]
39+
[SimpleField(IsFilterable = true, IsSortable = true, IsFacetable = true)]
4340
public DateTimeOffset? LastRenovationDate { get; set; }
4441

45-
[IsFilterable, IsSortable, IsFacetable]
42+
[SimpleField(IsFilterable = true, IsSortable = true, IsFacetable = true)]
4643
public double? Rating { get; set; }
4744

45+
[SearchableField]
4846
public Address Address { get; set; }
4947

50-
[IsFilterable, IsSortable]
48+
[SearchableField(IsFilterable = true, IsSortable = true)]
5149
public GeographyPoint Location { get; set; }
5250

5351
public Room[] Rooms { get; set; }

0 commit comments

Comments
 (0)