Skip to content
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
8 changes: 5 additions & 3 deletions examples/solr/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
// Add Solr resource with custom port and core name
var solrWithCustomPort = builder.AddSolr("solr-custom", port: 8984, coreName: "mycore");

//Add Solr resource with a config set annotation
var solrWithConfigset = builder.AddSolr("solr-configset")
.WithConfigset("myconfigset", "/path/to/configset");

// Reference the Solr resources in a project (example)
// var exampleProject = builder.AddProject<Projects.ExampleProject>()
// .WithReference(solr)
// .WithReference(solrWithCustomPort);

// .WithReference(solr);
builder.Build().Run();
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>An Aspire hosting integration for Apache Solr.</Description>
<PackageTags>solr search hosting</PackageTags>
<AdditionalPackageTags>hosting solr search</AdditionalPackageTags>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Aspire.Hosting" />
Expand Down
56 changes: 56 additions & 0 deletions src/CommunityToolkit.Aspire.Hosting.Solr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,62 @@ var exampleProject = builder.AddProject<Projects.ExampleProject>()
builder.Build().Run();
```

## Using a Custom Config Set

You can configure Solr to use a custom config set by calling `WithConfigset` on the Solr resource builder. This mounts a local config set directory into the container and creates the core using that config set.

```csharp
var builder = DistributedApplication.CreateBuilder(args);

// Add Solr with a custom config set
var solr = builder.AddSolr("solr", coreName: "mycore")
.WithConfigset("my-config", "/path/to/configsets/my-config");

builder.Build().Run();
```

The `WithConfigset` method takes two parameters:

- `configSetName` — the name of the config set as it will appear inside the container (under `/opt/solr/server/solr/configsets/`)
- `configSetPath` — the absolute path on the host to the config set directory to mount

When a config set is provided, the core is created with `solr-create -c <coreName> -d <configSetName>` instead of the default `solr-precreate <coreName>`.

## Persisting Data

### Using a Named Volume

Use `WithDataVolume` to persist Solr data across container restarts with a Docker named volume:

```csharp
var builder = DistributedApplication.CreateBuilder(args);

// Use an auto-generated volume name
var solr = builder.AddSolr("solr")
.WithDataVolume();

// Or specify a custom volume name
var solrNamed = builder.AddSolr("solr")
.WithDataVolume("my-solr-data");

builder.Build().Run();
```

### Using a Bind Mount

Use `WithDataBindMount` to mount a host directory into the container for data storage:

```csharp
var builder = DistributedApplication.CreateBuilder(args);

var solr = builder.AddSolr("solr")
.WithDataBindMount("/path/to/solr/data");

builder.Build().Run();
```

The `WithDataBindMount` method mounts the specified host path to `/var/solr` inside the container. The directory must be writable by the Solr process (UID 8983).

## Feedback & contributing

https://github.com/dotnet/aspire
76 changes: 70 additions & 6 deletions src/CommunityToolkit.Aspire.Hosting.Solr/SolrBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,91 @@ public static IResourceBuilder<SolrResource> AddSolr(this IDistributedApplicatio
ArgumentException.ThrowIfNullOrEmpty(name);

coreName ??= "solr";
ArgumentException.ThrowIfNullOrEmpty(coreName);

var resource = new SolrResource(name, coreName);
SolrResource resource = new(name, coreName);

var solrBuilder = builder.AddResource(resource)
IResourceBuilder<SolrResource> solrBuilder = builder.AddResource(resource)
.WithImage(SolrContainerImageTags.Image, SolrContainerImageTags.Tag)
.WithImageRegistry(SolrContainerImageTags.Registry)
.WithHttpEndpoint(targetPort: 8983, port: port, name: SolrResource.PrimaryEndpointName)
.WithArgs("solr-precreate", coreName);
.WithArgs(context =>
{
var configSetAnnotation = context.Resource.Annotations.OfType<SolrConfigSetAnnotation>().LastOrDefault();
if (configSetAnnotation is not null)
{
context.Args.Add("solr-create");
context.Args.Add("-c");
context.Args.Add(coreName);
context.Args.Add("-d");
Comment thread
Chicoo marked this conversation as resolved.
Resolved
context.Args.Add(configSetAnnotation.ConfigSetName);
}
else
{
context.Args.Add("solr-precreate");
context.Args.Add(coreName);
}
});

string healthCheckKey = $"{name}_check";
var endpoint = solrBuilder.Resource.GetEndpoint(SolrResource.PrimaryEndpointName);
EndpointReference endpoint = solrBuilder.Resource.GetEndpoint(SolrResource.PrimaryEndpointName);

builder.Services.AddHealthChecks()
.AddUrlGroup(options =>
{
var uri = new Uri(endpoint.Url);
Uri uri = new(endpoint.Url);
options.AddUri(new Uri(uri, $"solr/{coreName}/admin/ping"), setup => setup.ExpectHttpCode(200));
}, healthCheckKey);

solrBuilder.WithHealthCheck(healthCheckKey);

return solrBuilder;
}
}

/// <summary>
/// Specifies the path to the config set directory.
/// </summary>
/// <param name="builder">An <see cref="IResourceBuilder{T}"/> representing the Solr resource.</param>
/// <param name="configSetName">The name of the config set.</param>
/// <param name="configSetPath">Path to the config set directory.</param>
/// <returns>An <see cref="IResourceBuilder{T}"/> that can be used to further customize the resource.</returns>
public static IResourceBuilder<SolrResource> WithConfigset(this IResourceBuilder<SolrResource> builder, string configSetName, string configSetPath)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentException.ThrowIfNullOrEmpty(configSetName);
ArgumentException.ThrowIfNullOrEmpty(configSetPath);

builder.WithAnnotation(new SolrConfigSetAnnotation(configSetName, configSetPath));
builder.WithBindMount(configSetPath, $"/opt/solr/server/solr/configsets/{configSetName}");
return builder;
}

/// <summary>
/// Adds a named volume for the data folder to a Solr container resource.
/// </summary>
/// <param name="builder">The resource builder.</param>
/// <param name="name">The name of the volume. Defaults to an auto-generated name based on the application and resource names.</param>
/// <param name="isReadOnly">A flag that indicates if this is a read-only volume.</param>
/// <returns>The <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<SolrResource> WithDataVolume(this IResourceBuilder<SolrResource> builder, string? name = null, bool isReadOnly = false)
{
ArgumentNullException.ThrowIfNull(builder);

return builder.WithVolume(name ?? VolumeNameGenerator.Generate(builder, "data"), "/var/solr", isReadOnly);
}

/// <summary>
/// Adds a bind mount for the data folder to a Solr container resource.
/// </summary>
/// <param name="builder">The resource builder.</param>
/// <param name="source">The source directory on the host to mount into the container.</param>
/// <param name="isReadOnly">A flag that indicates if this is a read-only mount.</param>
/// <returns>The <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<SolrResource> WithDataBindMount(this IResourceBuilder<SolrResource> builder, string source, bool isReadOnly = false)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(source);

return builder.WithBindMount(source, "/var/solr", isReadOnly);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Aspire.Hosting.ApplicationModel;
namespace Aspire.Hosting;

/// <summary>
/// Represents an annotation for defining a config set for a Solr resource.
/// </summary>
public sealed class SolrConfigSetAnnotation : IResourceAnnotation
{
/// <summary>
/// Initializes a new instance of the <see cref="SolrConfigSetAnnotation"/> class.
/// </summary>
/// <param name="configSetName">The name of the config set.</param>
/// <param name="configSetPath">The path to the config set directory.</param>
public SolrConfigSetAnnotation(string configSetName, string configSetPath)
{
ArgumentException.ThrowIfNullOrEmpty(configSetName);
ArgumentException.ThrowIfNullOrEmpty(configSetPath);
ConfigSetName = configSetName;
ConfigSetPath = configSetPath;
}

/// <summary>
/// Gets the name of the config set.
/// </summary>
public string ConfigSetName { get; }

/// <summary>
/// Gets the path to the config set directory.
/// </summary>
public string ConfigSetPath { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,36 @@ public void AddSolr_WithPortAndCoreName_ConfiguresCorrectly()
Assert.Equal("testcore", solr.Resource.CoreName);
}

[Fact]
public void AddSolr_WithConfigsetAnnotation()
{
var builder = DistributedApplication.CreateBuilder();

var configSetsPath = Path.GetFullPath(Path.Combine(
builder.AppHostDirectory,
"./configsets/test"));

var solr = builder.AddSolr("solr", coreName: "testcore")
.WithConfigset("test", configSetsPath);

Assert.NotNull(solr.Resource);
Assert.Equal("solr", solr.Resource.Name);
Assert.Equal("testcore", solr.Resource.CoreName);

// Verify the SolrConfigSetAnnotation was added with correct values
var configSetAnnotation = solr.Resource.Annotations.OfType<SolrConfigSetAnnotation>().LastOrDefault();
Assert.NotNull(configSetAnnotation);
Assert.Equal("test", configSetAnnotation.ConfigSetName);
Assert.Equal(configSetsPath, configSetAnnotation.ConfigSetPath);

// Verify the bind mount annotation was configured correctly
var bindMountAnnotation = solr.Resource.Annotations
.OfType<ContainerMountAnnotation>()
.FirstOrDefault(cma => cma.Source == configSetsPath);
Assert.NotNull(bindMountAnnotation);
Assert.Equal("/opt/solr/server/solr/configsets/test", bindMountAnnotation.Target);
}

[Fact]
public void SolrResource_HasCorrectConnectionString()
{
Expand Down Expand Up @@ -87,4 +117,55 @@ public void SolrResource_DefaultCoreName_IsSolr()

Assert.Equal("solr", solr.Resource.CoreName);
}

[Fact]
public void WithDataVolume_NoName_GeneratesVolumeName()
{
var builder = DistributedApplication.CreateBuilder();
var solr = builder.AddSolr("solr").WithDataVolume();

using var app = builder.Build();
var appModel = app.Services.GetRequiredService<DistributedApplicationModel>();
var resource = appModel.Resources.OfType<SolrResource>().SingleOrDefault();

Assert.NotNull(resource);
Assert.True(resource.TryGetLastAnnotation(out ContainerMountAnnotation? mountAnnotation));
Assert.EndsWith("-data", mountAnnotation.Source);
Assert.Equal("/var/solr", mountAnnotation.Target);
Assert.Equal(ContainerMountType.Volume, mountAnnotation.Type);
}

[Fact]
public void WithDataVolume_WithName_UsesProvidedName()
{
var builder = DistributedApplication.CreateBuilder();
var solr = builder.AddSolr("solr").WithDataVolume("mysolrdata");

using var app = builder.Build();
var appModel = app.Services.GetRequiredService<DistributedApplicationModel>();
var resource = appModel.Resources.OfType<SolrResource>().SingleOrDefault();

Assert.NotNull(resource);
Assert.True(resource.TryGetLastAnnotation(out ContainerMountAnnotation? mountAnnotation));
Assert.Equal("mysolrdata", mountAnnotation.Source);
Assert.Equal("/var/solr", mountAnnotation.Target);
Assert.Equal(ContainerMountType.Volume, mountAnnotation.Type);
}

[Fact]
public void WithDataBindMount_AddsBindMountAnnotation()
{
var builder = DistributedApplication.CreateBuilder();
var solr = builder.AddSolr("solr").WithDataBindMount("./mysolrdata");

using var app = builder.Build();
var appModel = app.Services.GetRequiredService<DistributedApplicationModel>();
var resource = appModel.Resources.OfType<SolrResource>().SingleOrDefault();

Assert.NotNull(resource);
Assert.True(resource.TryGetLastAnnotation(out ContainerMountAnnotation? mountAnnotation));
Assert.EndsWith("mysolrdata", mountAnnotation.Source);
Assert.Equal("/var/solr", mountAnnotation.Target);
Assert.Equal(ContainerMountType.BindMount, mountAnnotation.Type);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Set of French contractions for ElisionFilter
# TODO: load this as a resource from the analyzer and sync it in build.xml
l
m
t
qu
n
s
j
d
c
jusqu
quoiqu
lorsqu
puisqu
Loading
Loading