gpt4 book ai didi

java - 如何使用 key 将数据存储与文档链接

转载 作者:行者123 更新时间:2023-12-02 05:44:54 27 4
gpt4 key购买 nike

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);
}

}

最佳答案

  1. 保存员工实体。获取 ID。
  2. 将此 ID 设置为文档 ID,为文档建立索引。

这两个步骤可以在同一个服务器调用中完成。只需在创建文档之前移动 makePersistent() 即可。

关于java - 如何使用 key 将数据存储与文档链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24165748/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com