Skip to content

Indexing JObject documents does not work as expected #1609

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

Closed
JulianRooze opened this issue Nov 9, 2015 · 5 comments
Closed

Indexing JObject documents does not work as expected #1609

JulianRooze opened this issue Nov 9, 2015 · 5 comments
Assignees

Comments

@JulianRooze
Copy link

An issue occurs for us when you try to index a JObject as a document (using Nest) like this:

client.Index<JObject>(finalProposition, i => i
.Index("telecom_propositions")
.Type("proposition")
.Id(docId))

The request fails with a 400 because of "malformed content". When you inspect the request being sent, the JSON at first appears to miss its opening bracket. It took me a while to figure it out but it's not a malformed object that's being sent, but rather a list of JSON properties concatenated by a newline. Properties don't start with a bracket which explains the missing bracket.

The reason that it's not serializing the JObject as an object is this code:

https://github.com/elastic/elasticsearch-net/blob/develop/src/Elasticsearch.Net/Connection/RequestHandlers/RequestHandlerBase.cs#L62

It checks if the data is an IEnumerable<object>, which is true because a JObject is an IEnumerable<object> and from then on it treats the JObject as a list of items to serialize and not a singular object.

Expected behavior for me would be that it just serialized the whole JObject at once and that it would be possible to index a document from a JObject.

The reason this behavior is unexpected to me is that using the search API to get back a JObject works perfectly. It also works for the update document API (we use the DocAsUpsert route as a workaround for this issue) but not with the index API. It also works as expected when you use the bulk API to index JObject documents.

Suggested fix would be either a special-case check if data is JObject or perhaps for data is IDictionary<string, object> in which case it would just serialize the whole object as happens on line 66.

@amccool
Copy link

amccool commented Jan 14, 2016

I'm getting this when creating bulk descriptors of indexes using JObjects

@amccool
Copy link

amccool commented Jan 14, 2016

private async void blahblah(IEnumerable < JObject > jos) { var bdesc = new BulkDescriptor(); foreach (var jo in jos) { bdesc.Index(i => i .Index(Index) .Type("Trace") .Document(jo)); } //this yeilds the same as the above loop //bdesc.IndexMany(jos, (f,g) => f.Index(Index).Type("Trace")); var x = Client.Bulk(bdesc); }

@gmarz gmarz added v2.0.0 and removed v2.0.0-beta1 labels Jan 14, 2016
@gmarz gmarz added this to the 2.0.0 RC milestone Jan 14, 2016
@gmarz gmarz removed this from the 2.0.0 milestone Jan 25, 2016
russcam added a commit that referenced this issue Jan 28, 2016
@russcam
Copy link
Contributor

russcam commented Jan 28, 2016

I've added some integration tests to NEST 2.x for JObject and they get indexed as expected.

This looks to be an issue only with NEST 1.x - @JulianRooze, @amccool, which version of NEST are you using?

@amccool
Copy link

amccool commented Jan 28, 2016

Using ElasticSearch.net 1.7.1

russcam added a commit that referenced this issue Feb 5, 2016
This is to handle serializing JObject using the Json.NET contract for it and not treating it as an IEnumerable<object>
See #1609
@Mpdreamz
Copy link
Member

Mpdreamz commented Mar 7, 2016

Fixed in #1804 by @russcam

@Mpdreamz Mpdreamz closed this as completed Mar 7, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants