Skip to content
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

Index document without id #308

Closed
sevagh opened this issue May 5, 2018 · 5 comments
Closed

Index document without id #308

sevagh opened this issue May 5, 2018 · 5 comments

Comments

@sevagh
Copy link

sevagh commented May 5, 2018

Hello,
I've been digging through the docs (and I'm not too familiar with elasticsearch) - apologies if this is a trivial question.
It seems that it should be possible to index a document without specifying the ID, but the only examples I can find require an Id: https://github.com/elastic-rs/elastic/blob/master/src/elastic/examples/index.rs#L50
Is there a way to put a document without an id?

@KodrAus
Copy link
Member

KodrAus commented May 6, 2018

Hi @sevagh!

Yeh this is a bit of an oversight, the high-level document_index method requires an id, but it doesn't technically need one. I think I'll remove that parameter in the next breaking release. I have some grand plans to refactor those methods so we can infer the id and index fields from your document, but in the meantime you can send an index request manually using an IndexRequest:

use elastic::prelude::*;

#[derive(ElasticType, Serialize, Deserialize)]
struct MyType { ... }

// Serialize the document as json
let body = serde_json::to_string(&doc)?;

// Send an `IndexRequest` manually, using the index and type
// The response is then deserialized as an `IndexResponse`
let response = client
    .request(IndexRequest::for_index_ty("some-index", MyType::name(), body))
    .send()?
    .into_response::<IndexResponse>()?;

let indexed_id = response.id();

@KodrAus
Copy link
Member

KodrAus commented May 7, 2018

Alrighty, in the next breaking version of elastic, which will be 0.21.0, you'll be able to call document_index without having to supply an id:

client.document_index(index("some-index", doc)).send()?;

@hack3p
Copy link

hack3p commented Jun 1, 2018

This solution not worked for elastic >= 6.x #308 (comment)

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Api(Other({"reason": String("Rejecting mapping update to [idx] as the final mapping would have more than 1 type: [mytype, body]"), "root_cause": Array([Object({"reason": String("Rejecting mapping update to [idx] as the final mapping would have more than 1 type: [mytype, body]"), "type": String("illegal_argument_exception")})]), "type": String("illegal_argument_exception")}))', libcore/result.rs:945:5

Do you plan to support elastic >= 6.x ?

@KodrAus
Copy link
Member

KodrAus commented Jun 6, 2018

Hi @hack3p. Yes, we plan to support 6.x eventually, it's tracked in #277.

I need to find some time to sit down and polish up and push out the current set of breaking changes (all that's left in a nicer bulk API) and then look at 6.x

@KodrAus
Copy link
Member

KodrAus commented Feb 18, 2019

We've got a prerelease build for 0.21.0 up on crates.io now that lets you index documents without specifying the type and id inline (we infer them if we can from the document though) so I'll go ahead and close this one. But please feel free to re-open or create new issues if anything doesn't behave the way you'd expect!

@KodrAus KodrAus closed this as completed Feb 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants