Skip to content

Commit

Permalink
Merge pull request #46 from FrankieTF/frankUpdate
Browse files Browse the repository at this point in the history
Updated all except DotNetSample, DotNetHowToAutocomplete, DotNetHowTo…
  • Loading branch information
HeidiSteen authored Oct 20, 2020
2 parents c0961d6 + 85f5fb4 commit 1301ec8
Show file tree
Hide file tree
Showing 21 changed files with 679 additions and 574 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Search" Version="10.1.0" />
<PackageReference Include="NewtonSoft.Json" Version="12.0.3" />
<PackageReference Include="Azure.Search.Documents" Version="11.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.1" />
</ItemGroup>
Expand Down
111 changes: 55 additions & 56 deletions DotNetETagsExplainer/DotNetETagsExplainer/Program.cs
Original file line number Diff line number Diff line change
@@ -1,90 +1,85 @@
namespace AzureSearch.SDKHowTo
using Azure;
using Azure.Search.Documents.Indexes;
using Azure.Search.Documents.Indexes.Models;
using Microsoft.Extensions.Configuration;
using System;
using System.Threading.Tasks;

namespace AzureSearch.SDKHowTo
{
using System;
using Microsoft.Azure.Search;
using Microsoft.Azure.Search.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Rest.Azure;

class Program
{
// This sample shows how ETags work by performing conditional updates and deletes
// on an Azure Search index.
static void Main(string[] args)
static async Task Main(string[] args)
{
IConfigurationBuilder builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
IConfigurationRoot configuration = builder.Build();

SearchServiceClient serviceClient = CreateSearchServiceClient(configuration);
SearchIndexClient indexClient = CreateSearchIndexClient(configuration);

Console.WriteLine("Deleting index...\n");
DeleteTestIndexIfExists(serviceClient);
DeleteTestIndexIfExists(indexClient);

// Every top-level resource in Azure Search has an associated ETag that keeps track of which version
// of the resource you're working on. When you first create a resource such as an index, its ETag is
// empty.
Index index = DefineTestIndex();
SearchIndex index = DefineTestIndex();

Console.WriteLine(
$"Test index hasn't been created yet, so its ETag should be blank. ETag: '{index.ETag}'");
$"Test searchIndex hasn't been created yet, so its ETag should be blank. ETag: '{index.ETag}'");

// Once the resource exists in Azure Search, its ETag will be populated. Make sure to use the object
// returned by the SearchServiceClient! Otherwise, you will still have the old object with the
// returned by the SearchIndexClient! Otherwise, you will still have the old object with the
// blank ETag.
Console.WriteLine("Creating index...\n");
index = serviceClient.Indexes.Create(index);

//Console.WriteLine("Creating index...\n");
index = indexClient.CreateIndex(index);
Console.WriteLine($"Test index created; Its ETag should be populated. ETag: '{index.ETag}'");


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

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

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

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

// Client 1 successfully updates the index.
indexForClient1.Fields.Add(new Field("a", DataType.Int32));
indexForClient1 =
serviceClient.Indexes.CreateOrUpdate(
indexForClient1,
accessCondition: AccessCondition.IfNotChanged(indexForClient1));
// indexForClientUpdate successfully updates the index.
indexForClientUpdate.Fields.Add(new SearchField("a", SearchFieldDataType.Int32));
indexForClientUpdate = indexClient.CreateOrUpdateIndex(indexForClientUpdate);

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

// Client 2 tries to update the index, but fails, thanks to the ETag check.
// indexForClientUpdateFailed tries to update the index, but fails, thanks to the ETag check.
try
{
indexForClient2.Fields.Add(new Field("b", DataType.Boolean));
serviceClient.Indexes.CreateOrUpdate(
indexForClient2,
accessCondition: AccessCondition.IfNotChanged(indexForClient2));
indexForClientUpdateFailed.Fields.Add(new SearchField("b", SearchFieldDataType.Boolean));
indexClient.CreateOrUpdateIndex(indexForClientUpdateFailed);

Console.WriteLine("Whoops; This shouldn't happen");
Environment.Exit(1);
}
catch (CloudException e) when (e.IsAccessConditionFailed())
catch (RequestFailedException e) when (e.Status == 400)
{
Console.WriteLine("Client 2 failed to update the index, as expected.");
Console.WriteLine("ClientUpdateFailed failed to update the index, as expected.");
}

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

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

private static SearchServiceClient CreateSearchServiceClient(IConfigurationRoot configuration)
private static SearchIndexClient CreateSearchIndexClient(IConfigurationRoot configuration)
{
string searchServiceName = configuration["SearchServiceName"];
string searchServicEndpoint = configuration["SearchServicEndpoint"];
string adminApiKey = configuration["SearchServiceAdminApiKey"];

SearchServiceClient serviceClient =
new SearchServiceClient(searchServiceName, new SearchCredentials(adminApiKey));
return serviceClient;
SearchIndexClient indexClient =
new SearchIndexClient(new Uri(searchServicEndpoint), new AzureKeyCredential(adminApiKey));
return indexClient;
}

private static void DeleteTestIndexIfExists(SearchServiceClient serviceClient)
private static void DeleteTestIndexIfExists(SearchIndexClient indexClient)
{
if (serviceClient.Indexes.Exists("test"))
try
{
if (indexClient.GetIndex("test") != null)
{
indexClient.DeleteIndex("test");
}
}
catch (RequestFailedException e) when (e.Status == 404)
{
serviceClient.Indexes.Delete("test");
//if exception occurred and status is "Not Found", this is work as expect
Console.WriteLine("Failed to find index and this is because it's not there.");
}
}

private static Index DefineTestIndex() =>
new Index()
{
Name = "test",
Fields = new[] { new Field("id", DataType.String) { IsKey = true } }
};
private static SearchIndex DefineTestIndex() =>
new SearchIndex("test", new[] { new SearchField("id", SearchFieldDataType.String) { IsKey = true } });
}
}
}
4 changes: 2 additions & 2 deletions DotNetETagsExplainer/DotNetETagsExplainer/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"SearchServiceName": "Put your search service name here",
"SearchServiceAdminApiKey": "Put your primary or secondary API key here"
"SearchServicEndpoint": "Your Search Service Endpoint",
"SearchServiceAdminApiKey": "Your Search Service Admin APikey"
}
10 changes: 5 additions & 5 deletions DotNetHowTo/DotNetHowTo/Address.Methods.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace AzureSearch.SDKHowTo
{
using System;
using System.Text;
using Newtonsoft.Json;
using System;
using System.Text;
using System.Text.Json.Serialization;

namespace AzureSearch.SDKHowTo
{
public partial class Address
{
// This implementation of ToString() is only for the purposes of the sample console application.
Expand Down
15 changes: 6 additions & 9 deletions DotNetHowTo/DotNetHowTo/Address.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
using System;
using Microsoft.Azure.Search;
using Microsoft.Azure.Search.Models;
using Newtonsoft.Json;
using Azure.Search.Documents.Indexes;

namespace AzureSearch.SDKHowTo
{
public partial class Address
{
[IsSearchable]
[SearchableField(IsFilterable = true)]
public string StreetAddress { get; set; }

[IsSearchable, IsFilterable, IsSortable, IsFacetable]
[SearchableField(IsFilterable = true, IsSortable = true, IsFacetable = true)]
public string City { get; set; }

[IsSearchable, IsFilterable, IsSortable, IsFacetable]
[SearchableField(IsFilterable = true, IsSortable = true, IsFacetable = true)]
public string StateProvince { get; set; }

[IsSearchable, IsFilterable, IsSortable, IsFacetable]
[SearchableField(IsFilterable = true, IsSortable = true, IsFacetable = true)]
public string PostalCode { get; set; }

[IsSearchable, IsFilterable, IsSortable, IsFacetable]
[SearchableField(IsFilterable = true, IsSortable = true, IsFacetable = true)]
public string Country { get; set; }
}
}
4 changes: 3 additions & 1 deletion DotNetHowTo/DotNetHowTo/DotNetHowTo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Search" Version="10.1.0" />
<PackageReference Include="Azure.Search.Documents" Version="11.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
<PackageReference Include="Microsoft.Spatial" Version="7.7.0" />
<PackageReference Include="System.Text.Json" Version="4.7.2" />
</ItemGroup>

<ItemGroup>
Expand Down
10 changes: 5 additions & 5 deletions DotNetHowTo/DotNetHowTo/Hotel.Methods.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace AzureSearch.SDKHowTo
{
using System;
using System.Text;
using System;
using System.Text;

namespace AzureSearch.SDKHowTo
{
public partial class Hotel
{
// This implementation of ToString() is only for the purposes of the sample console application.
Expand Down Expand Up @@ -31,7 +31,7 @@ public override string ToString()
{
builder.AppendFormat("Description (French): {0}\n", DescriptionFr);
}

if (!String.IsNullOrEmpty(Category))
{
builder.AppendFormat("Category: {0}\n", Category);
Expand Down
40 changes: 19 additions & 21 deletions DotNetHowTo/DotNetHowTo/Hotel.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
namespace AzureSearch.SDKHowTo
{
using System;
using Microsoft.Azure.Search;
using Microsoft.Azure.Search.Models;
using Microsoft.Spatial;
using Newtonsoft.Json;
using System;
using Microsoft.Spatial;
using System.Text.Json.Serialization;
using Azure.Search.Documents.Indexes;
using Azure.Search.Documents.Indexes.Models;

namespace AzureSearch.SDKHowTo
{
public partial class Hotel
{
[System.ComponentModel.DataAnnotations.Key]
[IsFilterable]
[SimpleField(IsKey = true, IsFilterable = true)]
public string HotelId { get; set; }

[IsSearchable, IsSortable]
[SearchableField(IsSortable = true)]
public string HotelName { get; set; }

[IsSearchable]
[Analyzer(AnalyzerName.AsString.EnLucene)]
[SearchableField(AnalyzerName = LexicalAnalyzerName.Values.EnLucene)]
public string Description { get; set; }

[IsSearchable]
[Analyzer(AnalyzerName.AsString.FrLucene)]
[JsonProperty("Description_fr")]
[SearchableField(AnalyzerName = LexicalAnalyzerName.Values.FrLucene)]
[JsonPropertyName("Description_fr")]
public string DescriptionFr { get; set; }

[IsSearchable, IsFilterable, IsSortable, IsFacetable]
[SearchableField(IsFilterable = true, IsSortable = true, IsFacetable = true)]
public string Category { get; set; }

[IsSearchable, IsFilterable, IsFacetable]
[SearchableField(IsFilterable = true, IsFacetable = true)]
public string[] Tags { get; set; }

[IsFilterable, IsSortable, IsFacetable]
[SimpleField(IsFilterable = true, IsSortable = true, IsFacetable = true)]
public bool? ParkingIncluded { get; set; }

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

[IsFilterable, IsSortable, IsFacetable]
[SimpleField(IsFilterable = true, IsSortable = true, IsFacetable = true)]
public DateTimeOffset? LastRenovationDate { get; set; }

[IsFilterable, IsSortable, IsFacetable]
[SimpleField(IsFilterable = true, IsSortable = true, IsFacetable = true)]
public double? Rating { get; set; }

[SearchableField]
public Address Address { get; set; }

[IsFilterable, IsSortable]
[SearchableField(IsFilterable = true, IsSortable = true)]
public GeographyPoint Location { get; set; }

public Room[] Rooms { get; set; }
Expand Down
Loading

0 comments on commit 1301ec8

Please sign in to comment.