如何对祖先查询应用日期过滤器 [英] how to apply date filter on ancestor query

查看:115
本文介绍了如何对祖先查询应用日期过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体模型Student和attendance,每个考勤实体都有关联的学生家长。

考勤模式:

  @Entity 
public class Attendance {

@Id
Long id;
@Index
日期日期;
@Parent
@Index
@ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
Ref< Student> studentRef;

public Long getId(){
return id;
}

public Date getDate(){
return date;
}

public void setDate(Date date){
this.date = date;
}
public String getWebsafeKey(){
return Key.create(studentRef.getKey(),Attendance.class,id).getString();


学生模式:

  @Entity 
public class Student {

@Id
Long id;
@Index
字符串名称;

String student_id;

长手机号码;
字符串电子邮件;

@Index
@ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
List< Ref< Shift>> shiftRef = new ArrayList< Ref< Shift>>();

public Long getId(){
return id;
}

public String getStudent_id(){
return student_id;
}

public void setStudent_id(String student_id){
this.student_id = student_id;
}

public String getName(){
return name;
}

public void setName(String name){
this.name = name;
}

public Long getMobile_number(){
return mobile_number;
}

public void setMobile_number(Long mobile_number){
this.mobile_number = mobile_number;
}

public String getEmail(){
return email;
}

public void setEmail(String email){
this.email = email;
}

public String getWebsafeKey(){
return Key.create(Student.class,id).getString();




$ b $ p
$ b

这就是我将考勤实体插入数据存储区的方式:

 关键< Student> studentKey = Key.create(student_web_safe_key); 
日期日期=新日期();
date.setTime(System.currentTimeMillis());
attendance.setDate(date);
attendance.studentRef = Ref.create(studentKey);
ofy()。save()。entity(attendance).now();

问题:


  1. 以上查询将数据存储在数据存储中的格式为: 2016-06-15(18:01:18.845)IST

    但是,当我检索相同没有指定其父项的实体,那么它给我的日期为:date:2016-06-15T12:31:18.845Z。请解释一下这个问题?


  2. 我能够存储与每个学生相对应的出勤率,并且还能够检索学生的所有出勤率。
    但是如何检索学生在指定日期或日期范围内的出勤率?
    我试过查询:
    Query query = ofy().load().type(Attendance.class).ancestor(studentKey).filter(date =,date);

但它给了我以下异常:

  com.google.api.client.googleapis.json.GoogleJsonResponseException:

503服务不可用
{
code:503 ,
errors:[
{
domain:global,
message:com.google.appengine.api.datastore.DatastoreNeedIndexException:no matching index found。推荐索引是:\ n-kind:出席情况\\\
祖先:yes \\\
属性:\\\
- name:date \\\
\\\
此查询的建议索引是:\\\
< datastore-index kind = \Attendance \ancestor = \true \source = \manual \> \\\
< property name = \date \direction = \\ \\asc \/> \\\
< / datastore-index> \\\
\\\

r eason:backendError
}
],
message:com.google.appengine.api.datastore.DatastoreNeedIndexException:找不到匹配的索引。建议的索引是:\ n - 种类:出勤率\祖先:yes \\\
属性:\\\
- 名称:date \\\
\\\
此查询的建议索引是:\\\
< datastore-index kind = \Attendance \ancestor = \true \source = \manual \> \\\
< property name = \date \direction = \asc \/> \\\
< / datastore-index> \\\
\\\

}


Appengine使用UTC时间戳,因此您的时间戳会从您的时区转换为UTC,但仍然是相同的日期和时间。在输出期间,您需要考虑时间戳可能包含时区和格式/相应计算当地时间。



回答问题2:



如果您添加了

 < datastore -index kind =参加ancestor =truesource =manual> 
< property name =datedirection =asc/>
< / datastore-index>

添加到您的 datastore-indexes.xml 你的查询应该可以工作。
如果文件还不存在,请在 下创建它,在 src / main / webapp / WEB-INF / datastore-indexes.xml > web.xml appengine-web.xml 文件。
您可以在此页面找到示例



至于原因:出现此错误的答案总是:因为数据存储需要此查询的复合索引。


I have two entity model Student and attendance such that each attendance entity has associated student parent.

Attendance model:

@Entity
public class Attendance {

 @Id
 Long id;
 @Index
 Date date;
 @Parent
 @Index
 @ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
 Ref<Student> studentRef;

 public Long getId() {
 return id;
 }

 public Date getDate() {
 return date;
 }

 public void setDate(Date date) {
 this.date = date;
 }
 public String getWebsafeKey() {
 return Key.create(studentRef.getKey(), Attendance.class, id).getString();
 }
}

Student model:

@Entity
public class Student {

 @Id
 Long id;
 @Index
 String name;

 String student_id;

 Long mobile_number;
 String email;

 @Index
 @ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
 List<Ref<Shift>> shiftRef = new ArrayList<Ref<Shift>>();

 public Long getId() {
 return id;
 }

 public String getStudent_id() {
 return student_id;
 }

 public void setStudent_id(String student_id) {
 this.student_id = student_id;
 }

 public String getName() {
 return name;
 }

 public void setName(String name) {
 this.name = name;
 }

 public Long getMobile_number() {
 return mobile_number;
 }

 public void setMobile_number(Long mobile_number) {
 this.mobile_number = mobile_number;
 }

 public String getEmail() {
 return email;
 }

 public void setEmail(String email) {
 this.email = email;
 }

 public String getWebsafeKey() {
 return Key.create(Student.class, id).getString();
 }
}

This how I am inserting an attendance entity into datastore:

Key<Student> studentKey = Key.create(student_web_safe_key);
Date date = new Date();
date.setTime(System.currentTimeMillis());
attendance.setDate(date);
attendance.studentRef = Ref.create(studentKey);
ofy().save().entity(attendance).now();

Questions:

  1. The above query store the date in datastore in format:2016-06-15 (18:01:18.845)IST
    But when I retrieve the same entity without specifying its parent then its gives me date as: "date ": "2016-06-15T12:31:18.845Z". please explain this?

  2. I am able to store attendance corresponds to each student and also able to retrieve all the attendance of a student. but how can retrieve attendance of student on specified date or range of dates? I tried blow query: Query query = ofy().load().type(Attendance.class).ancestor(studentKey).filter("date =",date);

but it gives me following exception:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 

503 Service Unavailable
    {
      "code": 503,
      "errors": [
        {
          "domain": "global",
          "message": "com.google.appengine.api.datastore.DatastoreNeedIndexException: no matching index found. recommended index is:\n- kind: Attendance\n  ancestor: yes\n  properties:\n  - name: date\n\nThe suggested index for this query is:\n    <datastore-index kind=\"Attendance\" ancestor=\"true\" source=\"manual\">\n        <property name=\"date\" direction=\"asc\"/>\n    </datastore-index>\n\n",
          "reason": "backendError"
        }
      ],
      "message": "com.google.appengine.api.datastore.DatastoreNeedIndexException: no matching index found. recommended index is:\n- kind: Attendance\n  ancestor: yes\n  properties:\n  - name: date\n\nThe suggested index for this query is:\n    <datastore-index kind=\"Attendance\" ancestor=\"true\" source=\"manual\">\n        <property name=\"date\" direction=\"asc\"/>\n    </datastore-index>\n\n"
    }

解决方案

Answering Question 1:

Appengine uses UTC timestamps. Therefor your timestamp is converted from your time zone to UTC. It is still the same date and time though. During output you need to consider that the timestamp may contain the timezone and format / calculate the local time accordingly.

Answering Question 2:

You have all the information you need in your error. If you add

<datastore-index kind="Attendance" ancestor="true" source="manual">
    <property name="date" direction="asc"/>
</datastore-index>

to your datastore-indexes.xml your query should work. If the file doesn't exist yet, create it under src/main/webapp/WEB-INF/datastore-indexes.xml, next to your web.xml and appengine-web.xml files. You can find an example on this page.

As for why: The answer with this error is always: Because datastore requires a composite index for this query.

这篇关于如何对祖先查询应用日期过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆