Skip to content

Commit c485410

Browse files
Ihar YakimushIhar Yakimush
authored andcommitted
set field serializer
1 parent 98eaa6f commit c485410

20 files changed

+170
-106
lines changed

README.md

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,10 @@ public class Product
1818
}
1919
```
2020
### IQueryable initialization
21-
Once you have solr operations interface, call `AsQuerable()` extension method to create `IQueryable<T>`. For instance
21+
Once you have solr operations interface, call `AsQueryable()` extension method to create `IQueryable<T>`. For instance
2222
```
2323
var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Product>>();
24-
IQueryable<Product> solrLinq = solr.AsQuerable()
25-
```
26-
It is possible to combine linq and regular SolrNet QueryOptions and main query. For instance if you need to append facets:
27-
```
28-
IQueryable<Product> solrLinq = solr.AsQuerable(options =>
29-
{
30-
// Configure SolrNet QueryOptions by setting params not available in linq
31-
}, new SolrQuery("some custom query"))
24+
IQueryable<Product> solrLinq = solr.AsQueryable()
3225
```
3326
Now you can use supported linq methods.
3427
### Getting results
@@ -37,10 +30,32 @@ To get result you can
3730
```
3831
SolrQueryResults<Product> result = solrLinq.Where(p => p.Popularity.HasValue).Take(10).ToSolrQueryResults();
3932
```
40-
- Trigger result by starting enumeration like any other IQuerable result triggering
33+
- Trigger result by starting enumeration like any other IQueryable result triggering
4134
```
4235
Product[] result = solrLinq.Where(p => p.Popularity.HasValue).Take(10).ToArray();
4336
```
37+
## Customization and workarounds for not suppported capabilities
38+
It is possible to combine linq and regular SolrNet QueryOptions and main query. For instance if you need to append facets:
39+
```
40+
IQueryable<Product> solrLinq = solr.AsQueryable(setup =>
41+
{
42+
// Set q parameter. By default *:* will be used.
43+
// LINQ Where method append fq (Filter Query) to query options and not affect main query
44+
options.MainQuery = new SolrQuery("some query");
45+
46+
// Configure SolrNet QueryOptions.
47+
// This function will be called after applying query options from LINQ
48+
// You can setup options not covered by LINQ, for instance facets
49+
options.SetupQueryOptions = queryOptions =>
50+
{
51+
queryOptions.AddFacets();
52+
};
53+
54+
// override default serializer if needed
55+
options.SolrFieldSerializer = new DefaultFieldSerializer();
56+
});
57+
```
58+
4459
## Supported methods
4560

4661
### Top, Skip

SolrNet.IntegrationOData/Controllers/ValuesController.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Community.OData.Linq;
66
using Community.OData.Linq.AspNetCore;
77
using Microsoft.AspNetCore.Mvc;
8+
using SolrNet.Impl.FieldSerializers;
89
using SolrNet.Linq;
910

1011
namespace SolrNet.IntegrationOData.Controllers
@@ -29,7 +30,23 @@ public IActionResult Get()
2930
[HttpGet("{id}")]
3031
public ActionResult<string> Get(int id, ODataQueryOptions odata)
3132
{
32-
return this.Ok(Product.SolrOperations.Value.AsQuerable().OData().ApplyQueryOptionsWithoutSelectExpand(odata).ToSolrQueryResults());
33+
Product.SolrOperations.Value.AsQueryable(options =>
34+
{
35+
// Set q parameter. By default *:* will be used
36+
options.MainQuery = new SolrQuery("some query");
37+
38+
// Configure SolrNet QueryOptions.
39+
// This function will be called after applying query options from LINQ
40+
// You can setup options not covered by LINQ. For instance facets
41+
options.SetupQueryOptions = queryOptions =>
42+
{
43+
queryOptions.AddFacets();
44+
};
45+
46+
// override default serializer if needed
47+
options.SolrFieldSerializer = new DefaultFieldSerializer();
48+
});
49+
return this.Ok(Product.SolrOperations.Value.AsQueryable().OData().ApplyQueryOptionsWithoutSelectExpand(odata).ToSolrQueryResults());
3350
}
3451

3552
// POST api/values

SolrNet.Linq.IntegrationTests/ResultTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@ public class ResultTests
1010
[Fact]
1111
public void AsEnumerable()
1212
{
13-
IEnumerable<Product> result = Product.SolrOperations.Value.AsQuerable().AsEnumerable();
13+
IEnumerable<Product> result = Product.SolrOperations.Value.AsQueryable().AsEnumerable();
1414

1515
Assert.True(result.Any());
1616
}
1717

1818
[Fact]
1919
public void ToSolrQueryResults()
2020
{
21-
SolrQueryResults<Product> result = Product.SolrOperations.Value.AsQuerable().ToSolrQueryResults();
21+
SolrQueryResults<Product> result = Product.SolrOperations.Value.AsQueryable().ToSolrQueryResults();
2222

2323
Assert.True(result.NumFound > 0);
2424
}
2525

2626
[Fact]
2727
public async Task ToSolrQueryResultsAsync()
2828
{
29-
SolrQueryResults<Product> result = await Product.SolrOperations.Value.AsQuerable().ToSolrQueryResultsAsync();
29+
SolrQueryResults<Product> result = await Product.SolrOperations.Value.AsQueryable().ToSolrQueryResultsAsync();
3030

3131
Assert.True(result.NumFound > 0);
3232
}

SolrNet.Linq.IntegrationTests/SortingTests.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ public class SortingTests
99
[Fact]
1010
public void ByProperty()
1111
{
12-
var asc = Product.SolrOperations.Value.AsQuerable().OrderBy(p => p.Price).ThenBy(p => p.Id).ToList();
13-
var desc = Product.SolrOperations.Value.AsQuerable().OrderByDescending(p => p.Price).ThenByDescending(p => p.Id).ToList();
12+
var asc = Product.SolrOperations.Value.AsQueryable().OrderBy(p => p.Price).ThenBy(p => p.Id).ToList();
13+
var desc = Product.SolrOperations.Value.AsQueryable().OrderByDescending(p => p.Price).ThenByDescending(p => p.Id).ToList();
1414

1515
Assert.Equal(asc.First().Id, desc.Last().Id);
1616
Assert.Equal(desc.First().Id, asc.Last().Id);
@@ -20,44 +20,44 @@ public void ByProperty()
2020
public void DoubleOrders()
2121
{
2222
Assert.Throws<InvalidOperationException>(() =>
23-
Product.SolrOperations.Value.AsQuerable().OrderBy(p => p.Price).OrderBy(p => p.Id).ToList());
23+
Product.SolrOperations.Value.AsQueryable().OrderBy(p => p.Price).OrderBy(p => p.Id).ToList());
2424
}
2525

2626
[Fact]
2727
public void ByNotMapped()
2828
{
2929
Assert.Throws<InvalidOperationException>(() =>
30-
Product.SolrOperations.Value.AsQuerable().OrderBy(p => p.NotMapped).ToList());
30+
Product.SolrOperations.Value.AsQueryable().OrderBy(p => p.NotMapped).ToList());
3131
}
3232

3333
[Fact]
3434
public void ByConversion()
3535
{
36-
var result = Product.SolrOperations.Value.AsQuerable().OrderBy(p => (int)p.Price).ToList();
36+
var result = Product.SolrOperations.Value.AsQueryable().OrderBy(p => (int)p.Price).ToList();
3737

3838
Assert.True(result.Any());
3939
}
4040

4141
[Fact]
4242
public void ByNullable()
4343
{
44-
var result = Product.SolrOperations.Value.AsQuerable().OrderBy(p => p.Popularity).ToList();
44+
var result = Product.SolrOperations.Value.AsQueryable().OrderBy(p => p.Popularity).ToList();
4545

4646
Assert.True(result.Any());
4747
}
4848

4949
[Fact]
5050
public void ByDiv()
5151
{
52-
var result = Product.SolrOperations.Value.AsQuerable().OrderBy(p => p.Sequence / 10).ToList();
52+
var result = Product.SolrOperations.Value.AsQueryable().OrderBy(p => p.Sequence / 10).ToList();
5353

5454
Assert.True(result.Any());
5555
}
5656

5757
[Fact]
5858
public void ByAbs()
5959
{
60-
var result = Product.SolrOperations.Value.AsQuerable().OrderBy(p => Math.Abs(p.Sequence)).ToList();
60+
var result = Product.SolrOperations.Value.AsQueryable().OrderBy(p => Math.Abs(p.Sequence)).ToList();
6161

6262
Assert.True(result.Any());
6363
}

SolrNet.Linq.IntegrationTests/TakeSkipTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ public class TakeSkipTests
99
[Fact]
1010
public void TakeSkip()
1111
{
12-
Product t1 = Product.SolrOperations.Value.AsQuerable().Take(1).AsEnumerable().Single();
13-
Product t2 = Product.SolrOperations.Value.AsQuerable().Skip(1).Take(1).AsEnumerable().Single();
14-
Product t3 = Product.SolrOperations.Value.AsQuerable().Skip(1).Take(1).AsEnumerable().Single();
12+
Product t1 = Product.SolrOperations.Value.AsQueryable().Take(1).AsEnumerable().Single();
13+
Product t2 = Product.SolrOperations.Value.AsQueryable().Skip(1).Take(1).AsEnumerable().Single();
14+
Product t3 = Product.SolrOperations.Value.AsQueryable().Skip(1).Take(1).AsEnumerable().Single();
1515

1616
Assert.NotEqual(t1.Id, t2.Id);
1717
Assert.Equal(t3.Id, t2.Id);

SolrNet.Linq.IntegrationTests/WhereTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ public class WhereTests
88
[Fact]
99
public void ByMember()
1010
{
11-
Product t1 = Product.SolrOperations.Value.AsQuerable().Where(p => p.InStock).AsEnumerable()
11+
Product t1 = Product.SolrOperations.Value.AsQueryable().Where(p => p.InStock).AsEnumerable()
1212
.FirstOrDefault();
1313

14-
Product t2 = Product.SolrOperations.Value.AsQuerable().Where(p => !p.InStock).AsEnumerable()
14+
Product t2 = Product.SolrOperations.Value.AsQueryable().Where(p => !p.InStock).AsEnumerable()
1515
.FirstOrDefault();
1616

1717
Assert.Null(t1);

SolrNet.Linq.Tests/MemberExpressionExtensionsTest.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Linq.Expressions;
33
using Xunit;
44
using SolrNet.Linq.Expressions;
5+
using SolrNet.Linq.Expressions.Context;
56

67
namespace SolrNet.Linq.Tests
78
{
@@ -11,16 +12,16 @@ public class MemberExpressionExtensionsTest
1112
public void Member()
1213
{
1314
Expression<Func<Product,object>> exp = (Product p) => p.Popularity;
14-
15-
Assert.Equal("popularity", exp.Body.GetSolrMemberProduct(typeof(Product)));
15+
16+
Assert.Equal("popularity", MemberContext.ForType<Product>().GetSolrMemberProduct(exp.Body));
1617
}
1718

1819
[Fact]
1920
public void Div()
2021
{
2122
Expression<Func<Product, object>> exp = (Product p) => p.Popularity / 10;
2223

23-
Assert.Equal("div(popularity,10)", exp.Body.GetSolrMemberProduct(typeof(Product)));
24+
Assert.Equal("div(popularity,10)", MemberContext.ForType<Product>().GetSolrMemberProduct(exp.Body));
2425
}
2526

2627
[Fact]
@@ -29,79 +30,79 @@ public void DivVariable()
2930
double qwe = 12;
3031
Expression<Func<Product, object>> exp = (Product p) => p.Sequence / qwe;
3132

32-
Assert.Equal("div(sequence_i,12)", exp.Body.GetSolrMemberProduct(typeof(Product)));
33+
Assert.Equal("div(sequence_i,12)", MemberContext.ForType<Product>().GetSolrMemberProduct(exp.Body));
3334
}
3435

3536
[Fact]
3637
public void Sub()
3738
{
3839
Expression<Func<Product, object>> exp = (Product p) => p.Popularity - 10;
3940

40-
Assert.Equal("sub(popularity,10)", exp.Body.GetSolrMemberProduct(typeof(Product)));
41+
Assert.Equal("sub(popularity,10)", MemberContext.ForType<Product>().GetSolrMemberProduct(exp.Body));
4142
}
4243

4344
[Fact]
4445
public void Sum()
4546
{
4647
Expression<Func<Product, object>> exp = (Product p) => p.Popularity + 10;
4748

48-
Assert.Equal("sum(popularity,10)", exp.Body.GetSolrMemberProduct(typeof(Product)));
49+
Assert.Equal("sum(popularity,10)", MemberContext.ForType<Product>().GetSolrMemberProduct(exp.Body));
4950
}
5051

5152
[Fact]
5253
public void Mul()
5354
{
5455
Expression<Func<Product, object>> exp = (Product p) => p.Popularity * 10;
5556

56-
Assert.Equal("mul(popularity,10)", exp.Body.GetSolrMemberProduct(typeof(Product)));
57+
Assert.Equal("mul(popularity,10)", MemberContext.ForType<Product>().GetSolrMemberProduct(exp.Body));
5758
}
5859

5960
[Fact]
6061
public void Abs()
6162
{
6263
Expression<Func<Product, object>> exp = (Product p) => Math.Abs(p.Sequence);
6364

64-
Assert.Equal("abs(sequence_i)", exp.Body.GetSolrMemberProduct(typeof(Product)));
65+
Assert.Equal("abs(sequence_i)", MemberContext.ForType<Product>().GetSolrMemberProduct(exp.Body));
6566
}
6667

6768
[Fact]
6869
public void Log()
6970
{
7071
Expression<Func<Product, object>> exp = (Product p) => Math.Log10(p.Sequence);
7172

72-
Assert.Equal("log(sequence_i)", exp.Body.GetSolrMemberProduct(typeof(Product)));
73+
Assert.Equal("log(sequence_i)", MemberContext.ForType<Product>().GetSolrMemberProduct(exp.Body));
7374
}
7475

7576
[Fact]
7677
public void Max()
7778
{
7879
Expression<Func<Product, object>> exp = (Product p) => Math.Max(p.Sequence, 11);
7980

80-
Assert.Equal("max(sequence_i,11)", exp.Body.GetSolrMemberProduct(typeof(Product)));
81+
Assert.Equal("max(sequence_i,11)", MemberContext.ForType<Product>().GetSolrMemberProduct(exp.Body));
8182
}
8283

8384
[Fact]
8485
public void Min()
8586
{
8687
Expression<Func<Product, object>> exp = (Product p) => Math.Min(p.Sequence, 11);
8788

88-
Assert.Equal("min(sequence_i,11)", exp.Body.GetSolrMemberProduct(typeof(Product)));
89+
Assert.Equal("min(sequence_i,11)", MemberContext.ForType<Product>().GetSolrMemberProduct(exp.Body));
8990
}
9091

9192
[Fact]
9293
public void Pow()
9394
{
9495
Expression<Func<Product, object>> exp = (Product p) => Math.Pow(p.Sequence, 11);
9596

96-
Assert.Equal("pow(sequence_i,11)", exp.Body.GetSolrMemberProduct(typeof(Product)));
97+
Assert.Equal("pow(sequence_i,11)", MemberContext.ForType<Product>().GetSolrMemberProduct(exp.Body));
9798
}
9899

99100
[Fact]
100101
public void Sqrt()
101102
{
102103
Expression<Func<Product, object>> exp = (Product p) => Math.Sqrt(p.Sequence);
103104

104-
Assert.Equal("sqrt(sequence_i)", exp.Body.GetSolrMemberProduct(typeof(Product)));
105+
Assert.Equal("sqrt(sequence_i)", MemberContext.ForType<Product>().GetSolrMemberProduct(exp.Body));
105106
}
106107
}
107108
}
Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
using System;
2+
using System.Linq;
23
using System.Linq.Expressions;
4+
using SolrNet.Impl;
5+
using SolrNet.Impl.FieldParsers;
6+
using SolrNet.Impl.FieldSerializers;
37

48
namespace SolrNet.Linq.Expressions.Context
59
{
610
public abstract class MemberContext
7-
{
11+
{
12+
private static readonly DefaultFieldSerializer DefaultFieldSerializer = new DefaultFieldSerializer();
13+
private ISolrFieldSerializer _fieldSerializer;
814
public abstract bool HasMemberAccess(Expression expression);
915

1016
public abstract string GetSolrMemberProduct(Expression expression, bool disableFunctions = false);
1117

1218
public abstract bool IsAccessToMember(MemberExpression expression);
1319

14-
public static string TrueStringSerialized { get; } =
15-
Expression.Constant(true).GetSolrMemberProduct(typeof(MemberContext), true);
16-
20+
public string TrueStringSerialized => this.FieldSerializer.Serialize(true).Single().FieldValue;
21+
1722
public static MemberContext ForType<T>()
1823
{
1924
return new TypeContext(typeof(T));
@@ -24,9 +29,18 @@ public static MemberContext ForType(Type type)
2429
return new TypeContext(type);
2530
}
2631

27-
public static MemberContext ForLambda(LambdaExpression lambdaExpression, string fieldName)
32+
public static MemberContext ForLambda(MemberContext parent, LambdaExpression lambdaExpression, string fieldName)
33+
{
34+
ParamContext context = new ParamContext(lambdaExpression, fieldName);
35+
context.FieldSerializer = parent.FieldSerializer;
36+
37+
return context;
38+
}
39+
40+
public ISolrFieldSerializer FieldSerializer
2841
{
29-
return new ParamContext(lambdaExpression, fieldName);
42+
get => _fieldSerializer ?? DefaultFieldSerializer;
43+
set => _fieldSerializer = value;
3044
}
3145
}
3246
}

SolrNet.Linq/Expressions/Context/ParamContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public override string GetSolrMemberProduct(Expression expression, bool disableF
5555
return _fieldName;
5656
}
5757

58-
return expression.GetSolrMemberProduct(typeof(ParamContext), disableFunctions);
58+
return expression.GetSolrMemberProduct(this, disableFunctions);
5959
}
6060

6161
public override bool IsAccessToMember(MemberExpression expression)

SolrNet.Linq/Expressions/Context/TypeContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public override bool HasMemberAccess(Expression expression)
1818

1919
public override string GetSolrMemberProduct(Expression expression, bool disableFunctions = false)
2020
{
21-
return expression.GetSolrMemberProduct(_type, disableFunctions);
21+
return expression.GetSolrMemberProduct(this, disableFunctions);
2222
}
2323

2424
public override bool IsAccessToMember(MemberExpression expression)

0 commit comments

Comments
 (0)