Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Nest Client to load completion fields in Elasticsearch

I would like to use Nest to populate the completion suggestion field into an index. After reading this ElasticSearch blog post about implementing a completion field I see you can have the following properties:

  • array of inputs
  • single output
  • weight
  • payload

I am assuming that to load this data into an index I need to include an entity on my search object that contains the above fields?

like image 471
BrandonG Avatar asked Feb 14 '14 17:02

BrandonG


1 Answers

I was able to finally load the completion field by creating several classes, and following the FluentMappingFullExample unit test, specifically the following part:

                    .Completion(s=>s
                    .Name(p=>p.Name.Suffix("completion"))
                    .IndexAnalyzer("standard")
                    .SearchAnalyzer("standard")
                    .MaxInputLength(20)
                    .Payloads()
                    .PreservePositionIncrements()
                    .PreserveSeparators()
                )

For my search type entity, I created a field called suggest and made it of type CompletionField.

 public class CompletionField
{
    public CompletionField()
    {
        Input = new List<string>();
    }

    public List<string> Input { get; set; }
    //public string Output { get; set; }
    public int Weight { get; set; }
    public Payload Payload { get; set; }
}

public class Payload
{
    public int ID { get; set; }
}

After I loaded my entity from the db using dapper, I then looped over the results and loaded my completion field with the appropriate inputs that I wanted. I was then able to successfully call the suggest API and query on this data. I hope this helps someone else.

like image 175
BrandonG Avatar answered Nov 05 '22 19:11

BrandonG