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
11 changes: 11 additions & 0 deletions src/Nest/DSL/Search/HighlightDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public interface IHighlightRequest

[JsonProperty("boundary_chars")]
string BoundaryChars { get; set; }

[JsonProperty("highlight_query")]
IQueryContainer HighlightQuery { get; set; }
}

public class HighlightRequest : IHighlightRequest
Expand All @@ -62,6 +65,7 @@ public class HighlightRequest : IHighlightRequest
public Dictionary<PropertyPathMarker, IHighlightField> Fields { get; set; }
public bool? RequireFieldMatch { get; set; }
public string BoundaryChars { get; set; }
public IQueryContainer HighlightQuery { get; set; }
}

public class HighlightDescriptor<T> : IHighlightRequest
Expand Down Expand Up @@ -93,6 +97,8 @@ public class HighlightDescriptor<T> : IHighlightRequest

string IHighlightRequest.BoundaryChars { get; set; }

IQueryContainer IHighlightRequest.HighlightQuery { get; set; }

public HighlightDescriptor<T> OnFields(params Action<HighlightFieldDescriptor<T>>[] fieldHighlighters)
{
fieldHighlighters.ThrowIfEmpty("fieldHighlighters");
Expand Down Expand Up @@ -182,5 +188,10 @@ public HighlightDescriptor<T> BoundaryMaxSize(int boundaryMaxSize)
Self.BoundaryMaxSize = boundaryMaxSize;
return this;
}
public HighlightDescriptor<T> HighlightQuery(Func<QueryDescriptor<T>, QueryContainer> query)
{
Self.HighlightQuery = query(new QueryDescriptor<T>());
return this;
}
}
}
21 changes: 21 additions & 0 deletions src/Nest/DSL/Search/HighlightFieldDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public interface IHighlightField

[JsonProperty("force_source")]
bool? ForceSource { get; set; }

[JsonProperty("matched_fields")]
IEnumerable<PropertyPathMarker> MatchedFields { get; set; }
}

public class HighlightField : IHighlightField
Expand All @@ -73,6 +76,7 @@ public class HighlightField : IHighlightField
public string BoundaryChars { get; set; }
public string Type { get; set; }
public bool? ForceSource { get; set; }
public IEnumerable<PropertyPathMarker> MatchedFields { get; set; }
}

public class HighlightFieldDescriptor<T> : IHighlightField where T : class
Expand Down Expand Up @@ -108,6 +112,8 @@ public class HighlightFieldDescriptor<T> : IHighlightField where T : class
string IHighlightField.Type { get; set; }

bool? IHighlightField.ForceSource { get; set; }

IEnumerable<PropertyPathMarker> IHighlightField.MatchedFields { get; set; }

public HighlightFieldDescriptor<T> OnField(string field)
{
Expand Down Expand Up @@ -138,6 +144,11 @@ public HighlightFieldDescriptor<T> Type(string type)
Self.Type = type;
return this;
}
public HighlightFieldDescriptor<T> Type(HighlighterType type)
{
Self.Type = type.GetStringValue();
return this;
}
public HighlightFieldDescriptor<T> PreTags(string preTags)
{
Self.PreTags = new[] { preTags };
Expand Down Expand Up @@ -203,5 +214,15 @@ public HighlightFieldDescriptor<T> BoundaryMaxSize(int boundaryMaxSize)
Self.BoundaryMaxSize = boundaryMaxSize;
return this;
}
public HighlightFieldDescriptor<T> MatchedFields(IEnumerable<string> fields)
{
Self.MatchedFields = fields.Select(f => (PropertyPathMarker)f);
return this;
}
public HighlightFieldDescriptor<T> MatchedFields(params Expression<Func<T, object>>[] objectPaths)
{
Self.MatchedFields = objectPaths.Select(f => (PropertyPathMarker)f);
return this;
}
}
}
18 changes: 18 additions & 0 deletions src/Nest/Enums/HighlighterType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace Nest
{
public enum HighlighterType
{
[EnumMember(Value = "plain")]
Plain,
[EnumMember(Value = "postings")]
Postings,
[EnumMember(Value = "fvh")]
Fvh
}
}
1 change: 1 addition & 0 deletions src/Nest/Nest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@
<Compile Include="Enums\FieldDataLoading.cs" />
<Compile Include="Enums\FieldDataNonStringFormat.cs" />
<Compile Include="Enums\FieldDataStringFormat.cs" />
<Compile Include="Enums\HighlighterType.cs" />
<Compile Include="Enums\NormsLoading.cs" />
<Compile Include="Enums\RoutingAllocationEnableOption.cs" />
<Compile Include="Enums\ScriptLang.cs" />
Expand Down
48 changes: 48 additions & 0 deletions src/Tests/Nest.Tests.Integration/Search/HighlightTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,53 @@ public void TestHighlightNoNullRef()
Assert.GreaterOrEqual(result.Total, 1);
Assert.AreEqual(result.Highlights.Count(), 0);
}

[Test]
public void TestHighlightQuery()
{
var result = this.Client.Search<ElasticsearchProject>(s => s
.From(0)
.Size(10)
.Query(q => q
.QueryString(qs => qs
.Query("elasticsearch.pm")
)
)
.Highlight(h => h
.HighlightQuery(hq => hq
.Bool(b => b
.Must(m => m
.Match(mm => mm
.OnField(p => p.Name)
.Query("elasticsearch.pm")
)
)
.Should(sh => sh
.MatchPhrase(mp => mp
.OnField(p => p.Name)
.Slop(1)
.Boost(10)
)
)
.MinimumShouldMatch(0)
)
)
.PreTags("<b>")
.PostTags("</b>")
.OnFields(
f => f
.OnField(e => e.Name)
.PreTags("<em>")
.PostTags("</em>")
)
)
);

Assert.IsTrue(result.IsValid);
Assert.DoesNotThrow(() => result.Highlights.Count());
Assert.IsNotNull(result.Highlights);
Assert.GreaterOrEqual(result.Total, 2);
Assert.AreEqual(result.Highlights.Count(), 2);
}
}
}
29 changes: 26 additions & 3 deletions src/Tests/Nest.Tests.Unit/Search/Highlight/HighlightTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ public void TestHighlight()
.From(0)
.Size(10)
.Highlight(h => h
.HighlightQuery(hq => hq
.Match(m => m
.OnField(p => p.Name)
.Query("nest")
)
)
.BoundaryCharacters(".,!? \t\n")
.BoundaryMaxSize(20)
.Encoder("html")
Expand All @@ -26,9 +32,14 @@ public void TestHighlight()
.OnFields(
f => f
.OnAll()
.NoMatchSize(200)
.NoMatchSize(200)
.PreTags("<em>")
.PostTags("</em>")
.Type(HighlighterType.Plain),
f => f
.OnField(p => p.Name)
.Type(HighlighterType.Postings)
.MatchedFields(mf => mf.Country, mf => mf.Content)
)
);
var json = TestElasticClient.Serialize(s);
Expand All @@ -48,11 +59,23 @@ public void TestHighlight()
_all: {
pre_tags: [""<em>""],
post_tags: [""</em>""],
no_match_size: 200
no_match_size: 200,
type: ""plain""
},
name: {
type: ""postings"",
matched_fields: [ ""country"", ""content"" ]
}
},
require_field_match: true,
boundary_chars: "".,!? \t\n""
boundary_chars: "".,!? \t\n"",
highlight_query: {
match: {
name: {
query: ""nest""
}
}
}
}
}";
Assert.True(json.JsonEquals(expected), json);
Expand Down