【发布时间】:2013-09-04 13:12:30
【问题描述】:
我正在尝试使用 Search API 在 Google App Engine 上执行简单的日期查询。我想要实现的只是获取字段等于某个日期的所有文档的列表...
// Prepare the Query
String searchString = "date = 2013-08-26";
Query query = Query.newBuilder().build(searchString);
// Get the Date_Search Index
IndexSpec indexSpec = IndexSpec.newBuilder().setName("Date_Search").build();
Index index = SearchServiceFactory.getSearchService().getIndex(indexSpec);
// Perform the search
Results<ScoredDocument> results = index.search(query);
当我运行上面的代码时,没有返回任何结果。但是,如果我将查询更改为 date >= 2013-08-26 && date < 2013-08-27,则会返回预期结果。似乎日期相等逻辑不起作用。
这是我的索引中的文档列表...
DocId OrderId date event_key
3d4e5691-8fb9-42de-bf09-f2303236f091 84288787 2013-08-25 641.0
7e4700fe-dee1-4579-87c5-69b1b1929f39 84288787 2013-08-25 650.0
8c9ca43d-7673-4f12-9f0b-e3328cbdaa85 84288787 2013-08-26 659.0
04fa8025-e01b-42d3-9bf9-21c3d9618ca7 84288787 2013-08-26 668.0
41465d1f-6d8a-431f-b226-141c8d72c064 84288787 2013-08-26 676.0
1ba01a6b-0890-43b3-8225-0ed196b6ee80 84288787 2013-08-27 676.0
a8ef18b1-ffa5-4823-8442-852f7142cad1 84288786 2013-08-25 633.0
我预计会在 2013 年 8 月 26 日返回 3 份文件。当使用相等 date = 2013-08-26 运行时,没有结果,但查询 date >= 2013-08-26 && date < 2013-08-27 返回预期的 3 个结果。
使用以下代码将文档添加到索引中...
// Build the Date
Calendar calendar = Calendar.getInstance();
Date dateObject = calendar.getTime();
// Create the Document
Builder builder = Document.newBuilder();
builder.addField(Field.newBuilder().setName("event_key").setNumber(eventKey));
builder.addField(Field.newBuilder().setName("date").setDate(dateObject));
Document document = builder.build();
// Get the Date_Search Index
IndexSpec indexSpec = IndexSpec.newBuilder().setName("Date_Search").build();
Index index = SearchServiceFactory.getSearchService().getIndex(indexSpec);
// Store the Document
index.put(document);
根据Search API Documentation,日期字段被存储并且只能查询到 YYYY-MM-DD 精度(存储和查询时会去除时间信息)。此外,Query on Date Fields Documentation 表明支持日期相等查询。
有人可以帮助我了解问题所在。
【问题讨论】:
标签: java google-app-engine gae-search