Rizvi
Rizvi

Reputation: 33

How to link datastore with document using key

I will be very thankful if someone could help me to solve this problem as I have already spent a lot of time on it.

The frontend of my application sends employee data to server end, which creates Emmployee object and saves the data on datastore. My application provides the keyword search functionality on title, company and jobDesc so I am using Search Api.

The problem is that I want to use datastore for storing the complete data and document for storing searchable data. How can I link datastore with document? I know it can be achieved if I set employee’s key as document id but the problem is how will I get the key of the data which is being store. If I try to get key using e.getKey() that obviously returns nullPointerException because it does have the key at that time.

I can achieve this by reading all employee data stored on datastore and creating document with it and setting employee’s key as document id but I want to create document as the data is received from frontend of application.

//EmployeeServlet
    PersistenceManager pm = PMF.get().getPersistenceManager();

    Employee e = new Employee(title, company, location, category,
                    jobType, gender,
                    careerLevel, salaryRange,
                    sector, jobDesc);

            Document newDoc = Document.newBuilder().setId(???)
                    .addField(Field.newBuilder().setName("title").setText(title))
                    .addField(Field.newBuilder().setName("company").setText(company))
                    .addField(Field.newBuilder().setName("jobDesc").setText(jobDesc)).build();

    SearchIndexManager.INSTANCE.indexDocument("Employee", newDoc);


                pm.makePersistent(e);




 //Employee   
    @PersistenceCapable
    public class Employee {
        @PrimaryKey
        @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
        private Key key;
        @Persistent
        private String title;
        @Persistent
        private String company;
        @Persistent
        private String location;
        @Persistent
        private String category;
        @Persistent
        private String jobType;
        @Persistent
        private String gender;
        @Persistent
        private String careerLevel;
        @Persistent
        private String salaryRange;
        @Persistent
        private String sector;
        @Persistent
        private Text jobDescription;


        public Employee(String title, String company, String location,
                String category, 
                String jobType, String gender, 
                String careerLevel, String salaryRange,
                String sector,
                String jobDescription) {
            super();
            this.title = title;
            this.company = company;
            this.location = location;
            this.category = category;
            this.jobType = jobType;
            this.gender = gender;
            this.careerLevel = careerLevel;
            this.salaryRange = salaryRange;
            this.sector = sector;
            this.jobDescription = new Text(jobDescription);
        }

        }

Upvotes: 0

Views: 62

Answers (1)

Andrei Volgin
Andrei Volgin

Reputation: 41089

  1. Save employee entity. Get the id.
  2. Set this id as a document id, index the document.

Both steps can be done in the same server call. Just move your makePersistent() before you create a document.

Upvotes: 2

Related Questions