Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
fatihyildizhan committed Sep 27, 2021
1 parent 4290920 commit 17e0c11
Showing 1 changed file with 89 additions and 2 deletions.
91 changes: 89 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,89 @@
# SolrNET5WebAPI
How to connect Solr with .NET5
# How to connect Solr with .NET5

This is a very basic sample to show Solr & .NET5 integration.

### Nuget Packages
- SolrNet.Core
- SolrNet.Microsoft.DependencyInjection

### Init Solr Service in Startup.cs

```csharp
var credentials = Encoding.ASCII.GetBytes("username:password");
var credentialsBase64 = Convert.ToBase64String(credentials);
services.AddSolrNet<WeatherForecast>("https://solr-server-ip:port/solr/name", options =>
{
options.HttpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", credentialsBase64);
});
```

### Model

```csharp
public class WeatherForecast
{
[SolrUniqueKey("id")]
public string Id { get; set; }

[SolrField("temperature")]
public int Temperature { get; set; }

[SolrField("summary")]
public string Summary { get; set; }
}
```

### Api Controller
```csharp
private readonly ILogger<WeatherForecastController> _logger;
private readonly ISolrOperations<WeatherForecast> _solr;

public WeatherForecastController(ILogger<WeatherForecastController> logger, ISolrOperations<WeatherForecast> solr)
{
_logger = logger;
_solr = solr;
}
```

### Query

```csharp
[HttpGet]
public async Task<IActionResult> QueryTemperatureAsync([FromQuery]string parameter)
{
// sample
// parameter = "Freezing";
_logger.LogInformation("Search: " + parameter);
SolrQueryResults<WeatherForecast> results = await _solr.QueryAsync(new SolrQuery($"summary: {parameter}"));

return Ok(results);
}
```

### Add

```csharp
[HttpPost]
public async Task<IActionResult> AddTemperatureAsync([FromQuery]WeatherForecast weather)
{
_logger.LogInformation("Add: " + weather.Summary);

// sample
//Random rng = new();
//WeatherForecast weather = new()
//{
// Id = "1",
// Summary = Summaries[rng.Next(Summaries.Length)],
// Temperature = 10
//};
await _solr.AddAsync(weather);
await _solr.CommitAsync();

return Ok(weather);
}
```

### License

React is [MIT licensed](./LICENSE).

0 comments on commit 17e0c11

Please sign in to comment.