Skip to content

Commit a606ba0

Browse files
Merge #571
571: Add federated multisearch to MeilisearchClient r=curquiza a=apt-TebbeM # Pull Request ## Related issue Fixes #560 ## What does this PR do? - increases meilisearch version to 1.10.0 in docker-compose.yaml - adds new method on MeilisearchClient for federated search - extracts shared properties of "FederatedSearchQuery" and "SearchQuery" into a SearchQueryBase - add custom JsonConverter to handle federated search in request (empty object needs to be included, cause that causes federated search to work) - added tests ## PR checklist Please check if your PR fulfills the following requirements: - [ x ] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [ x ] Have you read the contributing guidelines? - [ x ] Have you made sure that the title is accurate and descriptive of the changes? Thank you so much for contributing to Meilisearch! Co-authored-by: TebbeM <[email protected]> Co-authored-by: apt-TebbeM <[email protected]>
2 parents 7714afc + 3b9be81 commit a606ba0

File tree

12 files changed

+414
-149
lines changed

12 files changed

+414
-149
lines changed

.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
MEILISEARCH_VERSION=v1.9.0
1+
MEILISEARCH_VERSION=v1.10.0
22
PROXIED_MEILISEARCH=http://nginx/api/
33
MEILISEARCH_URL=http://meilisearch:7700

src/Meilisearch/Constants.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System.Text.Json;
22
using System.Text.Json.Serialization;
33

4+
using Meilisearch.Converters;
5+
46
namespace Meilisearch
57
{
68
/// <summary>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System;
2+
using System.Linq;
3+
using System.Reflection;
4+
using System.Text.Json;
5+
using System.Text.Json.Serialization;
6+
7+
namespace Meilisearch.Converters
8+
{
9+
/// <summary>
10+
/// Always include property in json. MultiSearchFederationOptions will be serialized as "{}"
11+
/// </summary>
12+
public class MultiSearchFederationOptionsConverter : JsonConverter<MultiSearchFederationOptions>
13+
{
14+
/// <summary>
15+
/// Would override the default read logic, but here we use the default
16+
/// </summary>
17+
/// <param name="reader"></param>
18+
/// <param name="typeToConvert"></param>
19+
/// <param name="options"></param>
20+
/// <returns></returns>
21+
public override MultiSearchFederationOptions Read(ref Utf8JsonReader reader, Type typeToConvert,
22+
JsonSerializerOptions options)
23+
{
24+
return JsonSerializer.Deserialize<MultiSearchFederationOptions>(ref reader, options);
25+
}
26+
27+
/// <summary>
28+
/// Write json for MultiSearchFederationOptions and include it always as empty object
29+
/// </summary>
30+
/// <param name="writer"></param>
31+
/// <param name="value"></param>
32+
/// <param name="options"></param>
33+
public override void Write(Utf8JsonWriter writer, MultiSearchFederationOptions value,
34+
JsonSerializerOptions options)
35+
{
36+
if (value == null || !HasAnyValueSet(value))
37+
{
38+
WriteEmptyObject(writer);
39+
}
40+
else
41+
{
42+
JsonSerializer.Serialize(writer, value);
43+
}
44+
}
45+
private static void WriteEmptyObject(Utf8JsonWriter writer)
46+
{
47+
writer.WriteStartObject();
48+
writer.WriteEndObject();
49+
}
50+
51+
private bool HasAnyValueSet(MultiSearchFederationOptions value)
52+
{
53+
foreach (var property in
54+
value.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
55+
{
56+
var propertyValue = property.GetValue(value);
57+
var defaultValue = GetDefaultValue(property.PropertyType);
58+
59+
if (!Equals(propertyValue, defaultValue))
60+
{
61+
return true;
62+
}
63+
}
64+
return false;
65+
}
66+
67+
private object GetDefaultValue(Type type)
68+
{
69+
return type.IsValueType ? Activator.CreateInstance(type) : null;
70+
}
71+
}
72+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Collections.Generic;
2+
using System.Text.Json.Serialization;
3+
4+
using Meilisearch.Converters;
5+
6+
namespace Meilisearch
7+
{
8+
/// <summary>
9+
/// Search query used in federated multi-index search
10+
/// </summary>
11+
public class FederatedMultiSearchQuery
12+
{
13+
/// <summary>
14+
/// Default constructor that ensures FederationOptions are always set
15+
/// </summary>
16+
public FederatedMultiSearchQuery()
17+
{
18+
FederationOptions = new MultiSearchFederationOptions();
19+
}
20+
21+
/// <summary>
22+
/// The queries
23+
/// </summary>
24+
[JsonPropertyName("queries")]
25+
public List<FederatedSearchQuery> Queries { get; set; }
26+
27+
/// <summary>
28+
/// The federated search query options
29+
/// </summary>
30+
[JsonInclude]
31+
[JsonPropertyName("federation")]
32+
[JsonConverter(typeof(MultiSearchFederationOptionsConverter))]
33+
public MultiSearchFederationOptions FederationOptions { get; set; }
34+
}
35+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace Meilisearch
4+
{
5+
/// <summary>
6+
/// Search query for federated multi-index search
7+
/// </summary>
8+
public class FederatedSearchQuery : SearchQueryBase
9+
{
10+
/// <summary>
11+
/// Federated search options
12+
/// </summary>
13+
[JsonPropertyName("federationOptions")]
14+
public MultiSearchFederationOptions FederationOptions { get; set; }
15+
}
16+
}

src/Meilisearch/Meilisearch.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
<PackageIcon>logo.png</PackageIcon>
1414
<PackageReadmeFile>README.md</PackageReadmeFile>
1515
<PackageLicenseExpression>MIT</PackageLicenseExpression>
16-
1716
<GenerateDocumentationFile>True</GenerateDocumentationFile>
1817
<!-- Warnings for missing XML documentation are only visible during dev time -->
1918
<!-- You may want to remove the NoWarn node in case documentation will be mandatory in the future -->

0 commit comments

Comments
 (0)