Reputation: 1374
I am attempting to migrate from elasticsearch.net client 7.17 to elastic.clients.elasticsearch 8.x and am having trouble implementing the new syntax for the highlighter. I think the pre and post tags are the correct new syntax, but I cannot get the Fields syntax correct. The code below shows Fields using the 7.17 syntax, but I cannot find the analog in 8.x. I'm not sure whether it should still use the querydsl like that, or whether it should be a new FluentDictionary
, or some other syntax.
.Highlight(h => h
.PreTags(new List<string> { "<strong>" })
.PostTags(new List<string> { "</strong>" })
.Fields(fs => fs
.Field(p => p.searchPhrase)
)
)
Upvotes: 0
Views: 206
Reputation: 11
Unfortunately there is no proper documentations for the v8.x .NET client yet.
The following code is an example how to use the new elastic.client.elasticsearch
package in compare to the v7 Nest
package.
// v8.x version sample code
var searchRequest = new SearchRequest<YourDocumentEntity>()
{
Query = new SimpleQueryStringQuery()
{
Query = "the text you want to search",
Fields = new Field[]
{
// two sample fields:
new("title", boost: 2),
new("body", boost: 1)
},
DefaultOperator = Operator.Or,
MinimumShouldMatch = 3,
}
Highlight = new Highlight()
{
PreTags = ["<mark>"],
PostTags = ["</mark>"],
Encoder = HighlighterEncoder.Html,
Fields = new Dictionary<Field, HighlightField>
{
{ new Field("title"), new HighlightField() { PreTags = ["<mark>"], PostTags = ["</mark>"] }},
{ new Field("body"), new HighlightField() { PreTags = ["<mark>"], PostTags = ["</mark>"] }},
},
}
};
var searchResponse = await _client.SearchAsync<YourDocumentEntity>(searchRequest);
And the same code using Nest will be like this:
// v7.17 sample code
var searchResponse = await _client.SearchAsync<YourDocumentEntity>(s => s
.Query(q => q
.SimpleQueryString(m => m
.Query("the text you want to search")
.Fields(f => f
.Field(p => p.Title, boost: 2)
.Field(p => p.Body)
)
.DefaultOperator(Operator.Or)
.MinimumShouldMatch(3)
)
)
.Highlight(h => h
.PreTags("<mark>")
.PostTags("</mark>")
.Encoder(HighlighterEncoder.Html)
.Fields(
f => f.Field(p => p.Title).PreTags("<mark>").PostTags("</mark>"),
f => f.Field(p => p.Body).PreTags("<mark>").PostTags("</mark>")
)
)
);
Upvotes: 1