Skip to content

Commit f5ad42f

Browse files
committed
2 parents a6de6df + 6eeedf6 commit f5ad42f

File tree

4 files changed

+72
-5
lines changed

4 files changed

+72
-5
lines changed

src/Nest/CommonAbstractions/Infer/Field/Field.cs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Globalization;
23
using System.Linq.Expressions;
34
using System.Reflection;
45
using Elasticsearch.Net;
@@ -22,8 +23,11 @@ public Fields And<T>(Expression<Func<T, object>> field) where T : class =>
2223

2324
public static Field Create(string name, double? boost = null)
2425
{
25-
Field field = name;
26-
field.Boost = boost;
26+
if (name.IsNullOrEmpty()) return null;
27+
28+
double? b;
29+
Field field = ParseFieldName(name, out b);
30+
field.Boost = b ?? boost;
2731
return field;
2832
}
2933

@@ -34,14 +38,30 @@ public static Field Create(Expression expression, double? boost = null)
3438
return field;
3539
}
3640

41+
private static string ParseFieldName(string name, out double? boost)
42+
{
43+
boost = null;
44+
var parts = name.Split(new [] { '^' }, StringSplitOptions.RemoveEmptyEntries);
45+
if (parts.Length > 1)
46+
{
47+
name = parts[0];
48+
boost = Double.Parse(parts[1], CultureInfo.InvariantCulture);
49+
}
50+
return name;
51+
}
52+
3753
public static implicit operator Field(string name)
3854
{
39-
name.ThrowIfNullOrEmpty(nameof(name), "trying to implicitly convert from string to field");
55+
if (name.IsNullOrEmpty()) return null;
4056

41-
return name == null ? null : new Field
57+
double? boost;
58+
name = ParseFieldName(name, out boost);
59+
return new Field
4260
{
4361
Name = name,
44-
ComparisonValue = name
62+
ComparisonValue = name,
63+
Boost = boost
64+
4565
};
4666
}
4767

src/Nest/CommonAbstractions/Infer/Field/FieldResolver.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,21 @@ public FieldResolver(IConnectionSettingsValues settings)
2727
}
2828

2929
public string Resolve(Field field)
30+
{
31+
var name = ResolveFieldName(field);
32+
if (field.Boost.HasValue) name += $"^{field.Boost.Value.ToString(CultureInfo.InvariantCulture)}";
33+
return name;
34+
}
35+
36+
private string ResolveFieldName(Field field)
3037
{
3138
if (field.IsConditionless()) return null;
3239
if (!field.Name.IsNullOrEmpty()) return field.Name;
40+
3341
string f;
3442
if (this.Fields.TryGetValue(field, out f))
3543
return f;
44+
3645
f = this.Resolve(field.Expression, field.Property);
3746
this.Fields.TryAdd(field, f);
3847
return f;

src/Tests/ClientConcepts/HighLevel/Inferrence/FieldInference.doc.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ public void UsingStaticPropertyField()
7575
Expect("name")
7676
.WhenSerializing(fieldString)
7777
.WhenSerializing(fieldExpression);
78+
79+
fieldString = "name^2.1";
80+
fieldString.Boost.Should().Be(2.1);
81+
fieldExpression = Field<Project>(p => p.Name, 2.1);
82+
/** Now this is much much terser then our first example using the constructor! */
83+
84+
Expect("name^2.1")
85+
.WhenSerializing(fieldString)
86+
.WhenSerializing(fieldExpression);
7887
}
7988

8089
/** By default NEST will camelCase all the field names to be more javascripty */

src/Tests/QueryDsl/FullText/MultiMatch/MultiMatchUsageTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,33 @@ protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project>
8282
q => q.Fields = null
8383
};
8484
}
85+
86+
public class MultiMatchWithBoostUsageTests : QueryDslUsageTestsBase
87+
{
88+
public MultiMatchWithBoostUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(i, usage) { }
89+
90+
protected override object QueryJson => new
91+
{
92+
multi_match = new
93+
{
94+
query = "hello world",
95+
fields = new[] {
96+
"description^2.2",
97+
"myOtherField^0.3"
98+
}
99+
}
100+
};
101+
102+
protected override QueryContainer QueryInitializer => new MultiMatchQuery
103+
{
104+
Fields = Field<Project>(p=>p.Description, 2.2).And("myOtherField^0.3"),
105+
Query = "hello world",
106+
};
107+
108+
protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project> q) => q
109+
.MultiMatch(c => c
110+
.Fields(f => f.Field(p=>p.Description, 2.2).Field("myOtherField^0.3"))
111+
.Query("hello world")
112+
);
113+
}
85114
}

0 commit comments

Comments
 (0)