0

I have a document that contains an array of tags. I need to create a suggestion field corresponding to this tag field (to generate tag suggestions based on the values in the tag array). I am using NEST to interact with elastic search mostly. But I am not able to updated the suggestion property. The class used for the document contains following Document structure:

public class SuggestField
{
    public IEnumerable<string> Input { get; set; }
    public string Output { get; set; }
    public object Payload { get; set; }
    public int? Weight { get; set; }
}


public class Source{

        [ElasticProperty(Index = FieldIndexOption.NotAnalyzed)]
        public string[] tags { get; set; }

        public SuggestField[] tag_suggest { get; set; }
}

I add the mapping as follows:

var response = client.Map<Source>(m => m
                                     .MapFromAttributes()
                                     .Properties(p => p
                                     .Completion(c => c
                                     .Name(cp => cp.tag_suggest)
                                         .Payloads()
                                     )));

For updating tags, I use external scripts. I was hoping to change this same script to add changes to tag_suggest field also. But I tried the following but it is not working. Following is the script I tried:

if (ctx._source.tags.indexOf(newTag) < 0) {
      ctx._source.tags[ctx._source.tags.length] = newTag;
      ctx._source.tag_suggest[ctx._source.tag_suggest.length] = { input :newTag }
}
labyrinth
  • 1,104
  • 3
  • 11
  • 32

1 Answers1

1

I would change type of tag_suggest property from SuggestField[] to SuggestField. You can store all tags in SuggestField.Input.

public class Source
{

    [ElasticProperty(Index = FieldIndexOption.NotAnalyzed)]
    public string[] tags { get; set; }

    public SuggestField tag_suggest { get; set; }
}

Regarding your update script, after this change you can modify it to:

if (ctx._source.tags.indexOf(newTag) < 0) {
      ctx._source.tags[ctx._source.tags.length] = newTag;
      ctx._source.tag_suggest.input[ctx._source.tag_suggest.length] = newTag;
}

Hope it helps.

Rob
  • 9,664
  • 3
  • 41
  • 43