Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(AutoComplete): Refactor and optimize the user experience #159

Merged
merged 3 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public AutoCompleteClient(
_enableMultipleCondition = enableMultipleCondition;
}

public override async Task<GetResponse<TAudoCompleteDocument, TValue>> GetAsync<TAudoCompleteDocument, TValue>(
public override async Task<Masa.BuildingBlocks.SearchEngine.AutoComplete.Response.GetResponse<TAudoCompleteDocument>> GetBySpecifyDocumentAsync<TAudoCompleteDocument>(
string keyword,
AutoCompleteOptions? options = null,
CancellationToken cancellationToken = default)
Expand All @@ -39,7 +39,7 @@ public override async Task<GetResponse<TAudoCompleteDocument, TValue>> GetAsync<
keyword = keyword.Trim();

if (string.IsNullOrEmpty(keyword))
return new GetResponse<TAudoCompleteDocument, TValue>(true, string.Empty, new List<TAudoCompleteDocument>());
return new Masa.BuildingBlocks.SearchEngine.AutoComplete.Response.GetResponse<TAudoCompleteDocument>(true, string.Empty, new List<TAudoCompleteDocument>());

if (searchType == SearchType.Fuzzy)
{
Expand All @@ -52,7 +52,7 @@ public override async Task<GetResponse<TAudoCompleteDocument, TValue>> GetAsync<
newOptions.PageSize,
_defaultOperator)
, cancellationToken);
return new GetResponse<TAudoCompleteDocument, TValue>(ret.IsValid, ret.Message)
return new Masa.BuildingBlocks.SearchEngine.AutoComplete.Response.GetResponse<TAudoCompleteDocument>(ret.IsValid, ret.Message)
{
Total = ret.Total,
TotalPages = ret.TotalPages,
Expand All @@ -68,7 +68,8 @@ public override async Task<GetResponse<TAudoCompleteDocument, TValue>> GetAsync<
.Query(q => GetQueryDescriptor(q, newOptions.Field, keyword.ToLower()))
, cancellationToken
);
return new GetResponse<TAudoCompleteDocument, TValue>(ret.IsValid, ret.ServerError?.ToString() ?? "")
return new Masa.BuildingBlocks.SearchEngine.AutoComplete.Response.GetResponse<TAudoCompleteDocument>(ret.IsValid,
ret.ServerError?.ToString() ?? "")
{
Data = ret.Hits.Select(hit => hit.Source).ToList(),
Total = ret.Total,
Expand Down Expand Up @@ -121,15 +122,15 @@ private BoolQueryDescriptor<T> GetBoolQueryDescriptor<T>(BoolQueryDescriptor<T>
}


public override Task<SetResponse> SetAsync<TAudoCompleteDocument, TValue>(IEnumerable<TAudoCompleteDocument> documents,
public override Task<SetResponse> SetBySpecifyDocumentAsync<TAudoCompleteDocument>(IEnumerable<TAudoCompleteDocument> documents,
SetOptions? options = null,
CancellationToken cancellationToken = default)
{
SetOptions newOptions = options ?? new();
if (newOptions.IsOverride)
return SetMultiAsync<TAudoCompleteDocument, TValue>(documents, cancellationToken);
return SetMultiAsync(documents, cancellationToken);

return SetByNotOverrideAsync<TAudoCompleteDocument, TValue>(documents, cancellationToken);
return SetByNotOverrideAsync(documents, cancellationToken);
}

/// <summary>
Expand All @@ -139,16 +140,15 @@ public override Task<SetResponse> SetAsync<TAudoCompleteDocument, TValue>(IEnume
/// <param name="documents"></param>
/// <param name="cancellationToken"></param>
/// <typeparam name="TDocument"></typeparam>
/// <typeparam name="TValue"></typeparam>
/// <returns></returns>
private async Task<SetResponse> SetMultiAsync<TDocument, TValue>(
private async Task<SetResponse> SetMultiAsync<TDocument>(
IEnumerable<TDocument> documents,
CancellationToken cancellationToken = default)
where TDocument : AutoCompleteDocument<TValue> where TValue : notnull
where TDocument : AutoCompleteDocument
{
var request = new SetDocumentRequest<TDocument>(_indexName);
foreach (var document in documents)
request.AddDocument(document, document.Id);
request.AddDocument(document, document.GetDocumentId());

var ret = await _client.SetDocumentAsync(request, cancellationToken);
return new SetResponse(ret.IsValid, ret.Message)
Expand All @@ -164,16 +164,15 @@ private async Task<SetResponse> SetMultiAsync<TDocument, TValue>(
/// <param name="documents"></param>
/// <param name="cancellationToken"></param>
/// <typeparam name="TDocument"></typeparam>
/// <typeparam name="TValue"></typeparam>
/// <returns></returns>
private async Task<SetResponse> SetByNotOverrideAsync<TDocument, TValue>(
private async Task<SetResponse> SetByNotOverrideAsync<TDocument>(
IEnumerable<TDocument> documents,
CancellationToken cancellationToken = default)
where TDocument : AutoCompleteDocument<TValue> where TValue : notnull
where TDocument : AutoCompleteDocument
{
var request = new CreateMultiDocumentRequest<TDocument>(_indexName);
foreach (var document in documents)
request.AddDocument(document, document.Id);
request.AddDocument(document, document.GetDocumentId());

var ret = await _client.CreateMultiDocumentAsync(request, cancellationToken);
return new SetResponse(ret.IsValid, ret.Message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

namespace Masa.Contrib.SearchEngine.AutoComplete.Options;

public class AutoCompleteOptions<TDocument, TValue>
where TDocument : AutoCompleteDocument<TValue> where TValue : notnull
public class AutoCompleteOptions<TDocument>
where TDocument : AutoCompleteDocument
{
internal string IndexName { get; private set; }

Expand All @@ -22,7 +22,7 @@ public class AutoCompleteOptions<TDocument, TValue>

internal Action<IIndexSettings>? IndexSettingAction { get; private set; }

public AutoCompleteOptions<TDocument, TValue> UseIndexName(string indexName)
public AutoCompleteOptions<TDocument> UseIndexName(string indexName)
{
IndexName = indexName;
return this;
Expand All @@ -33,7 +33,7 @@ public AutoCompleteOptions<TDocument, TValue> UseIndexName(string indexName)
/// </summary>
/// <param name="alias">When it is null, no alias is set</param>
/// <returns></returns>
public AutoCompleteOptions<TDocument, TValue> UseAlias(string alias)
public AutoCompleteOptions<TDocument> UseAlias(string alias)
{
Alias = alias;
return this;
Expand All @@ -43,13 +43,13 @@ public AutoCompleteOptions<TDocument, TValue> UseAlias(string alias)
/// Set the default AutoComplete
/// </summary>
/// <returns></returns>
public AutoCompleteOptions<TDocument, TValue> UseDefault()
public AutoCompleteOptions<TDocument> UseDefault()
{
IsDefault = true;
return this;
}

public AutoCompleteOptions<TDocument, TValue> UseDefaultSearchType(SearchType defaultSearchType)
public AutoCompleteOptions<TDocument> UseDefaultSearchType(SearchType defaultSearchType)
{
DefaultSearchType = defaultSearchType;
return this;
Expand All @@ -60,25 +60,25 @@ public AutoCompleteOptions<TDocument, TValue> UseDefaultSearchType(SearchType de
/// </summary>
/// <param name="action"></param>
/// <returns></returns>
public AutoCompleteOptions<TDocument, TValue> Mapping(Action<TypeMappingDescriptor<TDocument>> action)
public AutoCompleteOptions<TDocument> Mapping(Action<TypeMappingDescriptor<TDocument>> action)
{
Action = action;
return this;
}

public AutoCompleteOptions<TDocument, TValue> IndexSettings(Action<IIndexSettings> indexSettingAction)
public AutoCompleteOptions<TDocument> IndexSettings(Action<IIndexSettings> indexSettingAction)
{
IndexSettingAction = indexSettingAction;
return this;
}

public AutoCompleteOptions<TDocument, TValue> UseDefaultOperator(Operator defaultOperator)
public AutoCompleteOptions<TDocument> UseDefaultOperator(Operator defaultOperator)
{
DefaultOperator = defaultOperator;
return this;
}

public AutoCompleteOptions<TDocument, TValue> UseMultipleConditions(bool enableMultipleCondition = true)
public AutoCompleteOptions<TDocument> UseMultipleConditions(bool enableMultipleCondition = true)
{
EnableMultipleCondition = enableMultipleCondition;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,70 +4,142 @@

Example:

```c#
``` c#
Install-Package Masa.Contrib.SearchEngine.AutoComplete.ElasticSearch
```

Basic usage:

Using AutoComplete

```` C#
* Use the default model `AutoCompleteDocument<TValue>`, where TValue does not support classes

``` C#
string userIndexName = "user_index_01";
string userAlias ​​= "user_index";
string userAlias = "user_index";
builder.Services
.AddElasticsearchClient("es", option => option.UseNodes("http://localhost:9200").UseDefault())
.AddAutoComplete<long>(option => option.UseIndexName(userIndexName).UseAlias(userAlias));
````
.AddElasticsearchClient("es", option => option.UseNodes("http://localhost:9200").UseDefault())
.AddAutoComplete<long>(option => option.UseIndexName(userIndexName).UseAlias(userAlias));
```

* Use a custom model, eg: `UserDocument`

``` C#
string userIndexName = "user_index_01";
string userAlias = "user_index";
builder.Services
.AddElasticsearchClient("es", option => option.UseNodes("http://localhost:9200").UseDefault())
.AddAutoCompleteBySpecifyDocument<User>(option => option.UseIndexName(userIndexName).UseAlias(userAlias));

public class User : AutoCompleteDocument
{
public int Id { get; set; }

public string Name { get; set; }

public string Phone { get; set; }

protected override string GetText()
{
return $"{Name}:{Phone}";
}

/// <summary>
/// If you want the id to be unique
/// </summary>
/// <returns></returns>
public override string GetDocumentId() => Id.ToString();
}
```

##### Setting data (SetAsync)

1. Set up a single document:

```` C#
public async Task SetAsync([FromServices] IAutoCompleteClient client)
{
await client.SetAsync(new AutoCompleteDocument<long>()
{
Text = "Edward Adam Davis",
Value = 1
});
}
````
1.1. Using the default model (key-value model):

``` C#
public async Task SetAsync([FromServices] IAutoCompleteClient client)
{
await client.SetAsync(new AutoCompleteDocument<long>("Edward Adam Davis", 1));
}
```

1.2 Using a custom model

``` C#
public async Task SetAsync([FromServices] IAutoCompleteClient client)
{
await client.SetBySpecifyDocumentAsync(new User
{
Id = 1,
Name = "Tony",
Phone = "13999999999"
});
}
```

2. Set up multiple documents:

```` C#
public async Task SetAsync([FromServices] IAutoCompleteClient client)
{
await client.SetAsync(new AutoCompleteDocument<long>[]
{
new()
{
Text = "Edward Adam Davis",
Value = 1
},
new()
{
Text = "Edward Jim",
Value = 1
}
});
}
````
2.1 Use the default model (key-value model):

``` C#
public async Task SetAsync([FromServices] IAutoCompleteClient client)
{
await client.SetAsync(new AutoCompleteDocument<long>[]
{
new("Edward Adam Davis", 1),
new("Edward Jim", 1)
});
}
```

2.2 Using a custom model

``` C#
public async Task SetAsync([FromServices] IAutoCompleteClient client)
{
await client.SetBySpecifyDocumentAsync(new User[]
{
new()
{
Id = 1,
Name = "Jim",
Phone = "13999999999"
},
new()
{
Id = 2,
Name = "Tony",
Phone = "13888888888"
}
});
}
```

##### Get data (GetAsync)

1. Search by keyword:

```` C#
public async Task<string> GetAsync([FromServices] IAutoCompleteClient client)
{
var response = await client.GetAsync<long>("Edward Adam Davis");
return System.Text.Json.JsonSerializer.Serialize(response);
}
````
1.1 Use the default model (key-value model):

``` C#
public async Task<string> GetAsync([FromServices] IAutoCompleteClient client)
{
var response = await client.GetAsync<long>("Edward Adam Davis");
return System.Text.Json.JsonSerializer.Serialize(response);
}
```

1.2 Using a custom model

```
public async Task<string> GetAsync([FromServices] IAutoCompleteClient client)
{
var response = await client.GetBySpecifyDocumentAsync<User>("Tony");
return System.Text.Json.JsonSerializer.Serialize(response);
}
```

##### Delete document (DeleteAsync)

Expand Down
Loading