Jamee
Jamee

Reputation: 121

Using Nest for elasticsearch

Hi I'm using Nest as an interface to Elastisearch. Everything works fine, there is only one thing I am unable to do. And that is Highlighting.

I have the following 'model'

[ElasticType(Name = "WebResource", SearchAnalyzer = "full_name", IndexAnalyzer = "partial_name", DateDetection = true, NumericDetection = true)]
public class WebResource
{
    public string _id;
    [ElasticProperty(Type = FieldType.integer_type, Index = FieldIndexOption.not_analyzed)]
    public string Id
    {
        get
        {
            if (_id == null || _id == Guid.Empty.ToString())
            {
                _id = Guid.NewGuid().ToString();
            }
            return _id;
        }
        set
        {
            _id = value;
        }
    }

    [ElasticProperty(Type = FieldType.string_type, Index = FieldIndexOption.analyzed)]
    public string Keywords { get; set; }

    [ElasticProperty(Type = FieldType.string_type, Index = FieldIndexOption.analyzed)]
    public string Content { get; set; }
}

I have an index and a search returns documents but the highlight is always zero

Client.Search<WebResource>(g => g.Query(k => k.Term(l => l.Content, searchText) || k.Term(l => l.Keywords, searchText)).Highlight(k => k.OnFields(p => p.OnField("Keywords"), p => p.OnField("Content")).FragmentSize(200)));

Where searchText is the searchtext. Any help is appreciated.

Kind regards JR

Upvotes: 0

Views: 563

Answers (1)

Jamee
Jamee

Reputation: 121

Turns out this question is related to NEST (elasticsearch) Highlighting in multiple fields

My solution was to break it up in to a more readable form

Action<HighlightFieldDescriptor<WebResource>> actWeb = (t) => t.OnField(g => g.Content);
Action<HighlightFieldDescriptor<WebResource>> actKey = (t) => t.OnField(g => g.Keywords);

Action<HighlightDescriptor<WebResource>> higDesc = t => t.OnFields(actWeb,actKey);

SearchDescriptor<WebResource> searchdesc = new SearchDescriptor<WebResource>();

searchdesc.Query( t => t.Term( k => k.Content,searchText) || t.Term( l =>l.Keywords,searchText));                
searchdesc.Highlight(higDesc);

 var resp = Client.Search(searchdesc);

Turns out it's the way you combine the fields.

Upvotes: 1

Related Questions