Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Define the _id field as id for an object #7

Closed
agindre opened this issue Nov 2, 2012 · 5 comments
Closed

Define the _id field as id for an object #7

agindre opened this issue Nov 2, 2012 · 5 comments

Comments

@agindre
Copy link

agindre commented Nov 2, 2012

Actually, I have to implement my own json parser to handle the elasticsearch json because I want to use the _id as value for my object's variable id. For me, it's the easy way to have an auto-increment id from elasticsearch that I can use as identifier for my doc.

The other way is :

  1. create the document form java
  2. get the latest id by sending a request to ES server
  3. insert the id into the document
  4. index the doc into ES

Here a good example to do so by using the _version as auto-increment : http://blogs.perl.org/users/clinton_gormley/2011/10/elasticsearchsequence---a-blazing-fast-ticket-server.html

@ferhatsb
Copy link
Member

Hi Alex,

Sorry for late response, did you check @JestId annotation? Does it fit for you case?

I think, we are not clearly understand the exact problem. Can you please gist your modifications?

Ferhat

@agindre
Copy link
Author

agindre commented Nov 28, 2012

Hi Ferhat,

Thank you for you answer. My ticket is not clear, sorry for that :s
What I meant is that I'm looking for an easy way to insert my document into elasticsearch with an id that is generated by elasticsearch. Let me give you an example.

Let's say that I've this document into my scala code :

case class Test (
 @JestId
 id: Int = 1,
 content: String = "lopsum",
 author: String = "Me"
)

If I well understood the usage of @JestId when I use Index index = new Index.Builder(source).index("app").type("document").build();, it will index a document in elasticsearch as follow :

{
  "id" : 1,
  "content" : "lopsum",
 "author" : "Me"
}

and if I want to get it with Get get = new Get.Builder("1").index("app").type("document").build();, I will receive something like this :

{
  "_index" : "jobzazjswwmi7cqb",
  "_type" : "document",
  "_id" : "1",
  "_version" : 2,
  "exists" : true, "_source" : {"id" : 1, "content": "lopsum", "author": "Me"} 
}

What I want to do is that I want to insert into elasticsearch without to specify the id of my document to let elasticsearch manage the auto-increment. So I have the same case class Test but I made my own parser to index the document as follow :

{
 "content" : "lopsum",
 "author" : "Me"
}

and my own parser to make the binding with my class as follow :

Test.id = elasticSearch.answer._id
Test.content = elasticSearch.answer._source.content
Test.author = elasticSearch.answer._source.author

Without this, if I want to use the index method from Jest, I have to get the lastest Id from my index/type (eg: 1 in my example) then set this value + 1 into my Test.id then index it with Jest.

Here a link to show want I'm looking for and a possible solution :
http://blogs.perl.org/users/clinton_gormley/2011/10/elasticsearchsequence---a-blazing-fast-ticket-server.html

@ferhatsb
Copy link
Member

Hi Alex,

Thanks for information. I have some improvements and now results are;

Mark your id field with JestId but set it to null (or just be sure it is null global variables etc.).

case class Test (
 @JestId
 id: String = null,
 content: String = "lopsum",
 author: String = "Me"
)

If you index this Test object

Test source = new Test();
Index index = new Index.Builder(source).index("app").type("document").build();

Id of this document will automatically created by ES.

Now if we search from ES;

 Search search = new Search(query)
 JestResult result = client.execute(search);
 List<Test> articleResult = result.getSourceAsObjectList(Test.class);

List of Test objects' id values will be auto generated values.

Does it cover your case ?

Ferhat

@agindre
Copy link
Author

agindre commented Nov 29, 2012

Ferhat,

That's exactly what I was looking for !

Thank you !

@ferhatsb
Copy link
Member

Excellent! You are welcome.

We are getting close to release 0.0.2.

ferhatsb pushed a commit that referenced this issue Apr 17, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants