Question

Tweets in an index can have a structure like this (not strict JSON, just trying to convey the structure:)

{
  text: string
  created_at: DateTime
  mention: [
    {
      name: string
      screen_name: string
      start: int
      end: int
    }
  ],
  hashtag: [
    // multiple string/int props similar to mention
  ],
  link: [
    // multiple string/int props similar to mention
  ],
  user: {
    name: string
    screen_name: string
    profile_image_url: string
  }
}

I'm getting the expected hits when using the NEST client, but I was under the impression I could map POCO's to bind the results to get some stronger typing e.g.

public class TweetDocument
{
  public string text { get; set; }
  public DateTime created_at { get; set; }
  public List<Mention> mention { get; set; }
  public List<Hashtag> hashtag { get; set; }
  public List<Link> link { get; set; }
  public User user { get; set; }
}

public class Mention
{
  public string name { get; set; }
  public string screen_name { get; set; }
  public int start { get; set; }
  public int end { get; set; }
}

// ... other POCO's for Hashtag, Link and User

The string property text and DateTime property created_at bind ok, but the mention, hashtag and link properties are all null when I look at the response's hits e.g.

var response = client.Search<TweetDocument>(q);
var firstHit = response.Hits.Hits.FirstOrDefault();
// firstHit.Fields.hashtag, .mention, .link and .user are all null

I've seen some attributes, but haven't really figured out how to use them e.g. ElasticProperty and ElasticType. How do I populate these collections from my index including the non-collection .User child property?

Was it helpful?

Solution

You should totally be able to bind those.

Unless when you specify the .Fields() property in your .Search()

What happens is that your document will live under _source if you don't specify Fields the hits wil return the _source if you specify fields it will return the fields as an array i.e:

 fields: {
    mention.name: ["name1", "name2"]
 }

which is basically a flattened view of your original _source document.

It would help if you can paste result.ConnectionStatus.Result to see how your results look like.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top