Skip to content

Commit

Permalink
update client to support Typesense v0.23.0 (#86)
Browse files Browse the repository at this point in the history
* upgrade version to 0.23.0

* add support for sort on fields

* implements emplace on import documents

* adds query parameters max candidates and facet query number of typos
  • Loading branch information
runeanielsen authored Jun 11, 2022
1 parent d5143d7 commit 8e681e3
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ executors:
auth:
username: $DOCKER_LOGIN
password: $DOCKER_ACCESSTOKEN
- image: typesense/typesense:0.22.2 # For integration testing
- image: typesense/typesense:0.23.0 # For integration testing
auth:
username: $DOCKER_LOGIN
password: $DOCKER_ACCESSTOKEN
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ var deleteSynonym = await typesenseClient.DeleteSynonym("Addresses", "Address_Sy

### Typesense API Errors

Typesense API exceptions in the [Typesense-api-errors](https://typesense.org/docs/0.22.2/api/api-errors.html) spec.
Typesense API exceptions in the [Typesense-api-errors](https://typesense.org/docs/0.23.0/api/api-errors.html) spec.

| Type | Description |
|:-------------------------------------------|:---------------------------------------------------------------------------|
Expand Down Expand Up @@ -331,5 +331,5 @@ dotnet test --filter Category=Integration
To enable running integration tests you can run Typesense in a docker container using the command below.

```sh
docker run -p 8108:8108 -v/tmp/data:/data typesense/typesense:0.22.2 --data-dir /data --api-key=key
docker run -p 8108:8108 -v/tmp/data:/data typesense/typesense:0.23.0 --data-dir /data --api-key=key
```
19 changes: 18 additions & 1 deletion src/Typesense/Field.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public record Field
public bool? Optional { get; init; }
[JsonPropertyName("index")]
public bool? Index { get; init; }
[JsonPropertyName("sort")]
public bool? Sort { get; init; }

public Field(string name, FieldType type)
{
Expand All @@ -39,7 +41,6 @@ public Field(string name, FieldType type, bool facet, bool? optional)
Optional = optional;
}

[JsonConstructor]
public Field(string name, FieldType type, bool facet, bool? optional, bool? index)
{
Name = name;
Expand All @@ -49,6 +50,22 @@ public Field(string name, FieldType type, bool facet, bool? optional, bool? inde
Index = index;
}

[JsonConstructor]
public Field(string name,
FieldType type,
bool facet,
bool? optional,
bool? index,
bool? sort)
{
Name = name;
Type = type;
Facet = facet;
Optional = optional;
Index = index;
Sort = sort;
}

[Obsolete("A better choice going forward is using the constructor with 'FieldType' enum instead.")]
public Field(string name, string type, bool facet, bool optional = false, bool index = true)
{
Expand Down
3 changes: 2 additions & 1 deletion src/Typesense/ImportType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ public enum ImportType
{
Create,
Upsert,
Update
Update,
Emplace
}
16 changes: 16 additions & 0 deletions src/Typesense/SearchParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,28 @@ public record SearchParameters
/// </summary>
public bool? PreSegmentedQuery { get; set; }

/// <summary>
/// Treat space as typo: search for q=basket ball if q=basketball is not found or vice-versa.
/// </summary>
public bool? SplitJoinTokens { get; set; }

/// <summary>
/// If you have some overrides defined but want to disable all of them during
/// query time, you can do that by setting this parameter to false
/// </summary>
public bool? EnableOverrides { get; set; }

/// <sumarry>
/// Control the number of words that Typesense considers for typo and prefix searching.
/// Default: 4 (or 10000 if exhaustive_search is enabled).
/// </summary>
public int? MaxCandiates { get; set; }

/// <sumarry>
/// Controls the fuzziness of the facet query filter.
/// </summary>
public int? FacetQueryNumberTypos { get; set; }

[Obsolete("Use multi-arity constructor instead.")]
public SearchParameters()
{
Expand Down
9 changes: 9 additions & 0 deletions src/Typesense/TypesenseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ public async Task<List<ImportResponse>> ImportDocuments<T>(
case ImportType.Upsert:
path += "&action=upsert";
break;
case ImportType.Emplace:
path += "&action=emplace";
break;
default:
throw new ArgumentException($"Could not handle {nameof(ImportType)} with name '{Enum.GetName(importType)}'", nameof(importType));
}
Expand Down Expand Up @@ -442,6 +445,12 @@ private static string CreateUrlSearchParameters(SearchParameters searchParameter
urlParameters += $"&pre_segmented_query={searchParameters.PreSegmentedQuery.Value.ToString().ToLowerInvariant()}";
if (searchParameters.EnableOverrides is not null)
urlParameters += $"&enable_overrides={searchParameters.EnableOverrides.Value.ToString().ToLowerInvariant()}";
if (searchParameters.SplitJoinTokens is not null)
urlParameters += $"&split_join_tokens={searchParameters.SplitJoinTokens.Value.ToString().ToLowerInvariant()}";
if (searchParameters.MaxCandiates is not null)
urlParameters += $"&max_candidates={searchParameters.MaxCandiates.Value.ToString().ToLowerInvariant()}";
if (searchParameters.FacetQueryNumberTypos is not null)
urlParameters += $"&facet_query_num_typos={searchParameters.FacetQueryNumberTypos.Value.ToString().ToLowerInvariant()}";

return urlParameters;
}
Expand Down
62 changes: 48 additions & 14 deletions test/Typesense.Tests/TypesenseClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public async Task Create_schema()
0,
new List<Field>
{
new Field("company_name", FieldType.String, false, false, true),
new Field("num_employees", FieldType.Int32, false, false, true),
new Field("country", FieldType.String, true, false, true),
new Field("company_name", FieldType.String, false, false, true, false),
new Field("num_employees", FieldType.Int32, false, false, true, true),
new Field("country", FieldType.String, true, false, true, false),
},
"num_employees");

Expand Down Expand Up @@ -70,7 +70,7 @@ public async Task Create_schema_with_wildcard_field()
0,
new List<Field>
{
new Field(".*", FieldType.String, false, true, true),
new Field(".*", FieldType.String, false, true, true, false),
},
"");

Expand All @@ -97,9 +97,9 @@ public async Task Retrieve_collection()
0,
new List<Field>
{
new Field("company_name", FieldType.String, false, false, true),
new Field("num_employees", FieldType.Int32, false, false, true),
new Field("country", FieldType.String, true, false, true),
new Field("company_name", FieldType.String, false, false, true, false),
new Field("num_employees", FieldType.Int32, false, false, true, true),
new Field("country", FieldType.String, true, false, true, false),
},
"num_employees");

Expand All @@ -118,9 +118,9 @@ public async Task Retrieve_collections()
0,
new List<Field>
{
new Field("company_name", FieldType.String, false, false, true),
new Field("num_employees", FieldType.Int32, false, false, true),
new Field("country", FieldType.String, true, false, true),
new Field("company_name", FieldType.String, false, false, true, false),
new Field("num_employees", FieldType.Int32, false, false, true, true),
new Field("country", FieldType.String, true, false, true, false),
},
"num_employees")
};
Expand All @@ -137,9 +137,9 @@ public async Task Delete_collection()
0,
new List<Field>
{
new Field("company_name", FieldType.String, false, false, true),
new Field("num_employees", FieldType.Int32, false, false, true),
new Field("country", FieldType.String, true, false, true),
new Field("company_name", FieldType.String, false, false, true, false),
new Field("num_employees", FieldType.Int32, false, false, true, true),
new Field("country", FieldType.String, true, false, true, false),
},
"num_employees");

Expand Down Expand Up @@ -226,7 +226,8 @@ public async Task Import_documents_create()
}
};

var response = await _client.ImportDocuments<Company>("companies", companies, 40, ImportType.Create);
var response = await _client.ImportDocuments<Company>(
"companies", companies, 40, ImportType.Create);

response.Should().BeEquivalentTo(expected);
}
Expand Down Expand Up @@ -296,6 +297,39 @@ public async Task Import_documents_upsert()
response.Should().BeEquivalentTo(expected);
}

[Fact, TestPriority(7)]
public async Task Import_documents_emplace()
{
var expected = new List<ImportResponse>
{
new ImportResponse(true),
new ImportResponse(true),
};

var companies = new List<Company>
{
new Company
{
Id = "125",
CompanyName = "Future Technology",
NumEmployees = 1232,
Country = "UK",
},
new Company
{
Id = "126",
CompanyName = "Random Corp.",
NumEmployees = 531,
Country = "AU",
}
};

var response = await _client.ImportDocuments<Company>(
"companies", companies, 40, ImportType.Emplace);

response.Should().BeEquivalentTo(expected);
}

[Fact, TestPriority(8)]
public async Task Export_documents()
{
Expand Down

0 comments on commit 8e681e3

Please sign in to comment.