New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Search query to convert either "foo":"bar" OR "foo":["bar","baz"] into an IList<String> ? #227
Comments
The mapping I have is a little odd because it is representative of how an Elasticsearch JDBC river import data from the related database tables (an entity table with a zero-to-many relationship to a code table). |
Hey @mvincent You can create a custom json converter for a type/field and give it to Nest by calling I hope this helps! If you manage to write a converter please post it back, might be worthwhile to add it to Nest ElasticProperty i.e
|
Thanks for pointing me in the right direction. I referenced this approach for a converter ( http://bit.ly/nest227 ) in order to create the following JsonToListConverter: public class JsonToListConverter<T> : CustomCreationConverter<IList<T>>
{
public override IList<T> Create(Type objectType)
{
// Default value is an empty list.
return new List<T>();
}
public override object ReadJson(JsonReader reader, Type objectType,
object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.StartArray)
{
// JSON object was already an array, so just deserialize it as usual.
object result = serializer.Deserialize(reader, objectType);
return result;
}
else
{
// JSON object was not an array,
// so deserialize the object and wrap it in a List.
var resultObject = serializer.Deserialize<T>(reader);
var correctedResult = new List<T>();
correctedResult.Add(resultObject);
return correctedResult;
}
}
} Then I applied the JsonConverter attribute on the model property: public class EntityCodes
{
[JsonConverter(typeof(JsonToListConverter<String>))]
public IList<String> Ancestry { get; set; }
} |
Thank you, gentlemen. This saved a bit of nerves to me. |
Is something like this added into a current NEST version? |
When I execute this NEST query:
where my Entity mapping is:
end my entity class is:
the response from elasticsearch varies depending on whether there is one or many ancestry values (e.g. whether the result is a simple name/value pair or an actual JSON array with square brackets). An example response is below:
If the ancestry has more than one entry, the JSON to class conversion goes well (because it's represented as a proper JSON array with square brackets). However, if the ancestry is just a simple name/value pair (as in the result above with Id of 2), then I receive this error:
Do you have any recommendations on the best way to achieve this conversion? Thank you.
The text was updated successfully, but these errors were encountered: