Hello,
I'm currently facing some problems when I want to sort Documents by given
field using Google's
*SearchAPI*<https://developers.google.com/appengine/docs/java/search/>
( *appengine-java-sdk-1.8.6 *)
Lets say that I'm having a Document with two fields - *firstName* with type
TEXT, and *birthDate* *BUT *instead of setting the type of the field to
Date I want to set it to number,
because of the limitation of the SearchAPI which allows to search date
without specifying the time (for example "birthDate > 1989-12-20")
And here is an example:
I'm saving the following Document with fields *firstName -> John, birthDate
-> **630115200* (this is 1989-12-20 but converted to seconds,
because *Field.Builder.setNumber(double value) *takes double as parameter
and if I pass the date in miliseconds it will exceed it's limit.
Document document = Document.newBuilder().setId("1")
.addField(Field.newBuilder().
setName("firstName").setText("John"))
.addField(Field.newBuilder().
setName("birthDate").setNumber(630115200))
.build();
As a result I end up with the following stored document
IDfirstNamebirthDate
1John630115200
So lets say that I've already stored the following two documents
IDfirstNamebirthDate
1
2John
Adam630115200
629251200
I want to find all documents where *birthDate > 0*, sorted by birthDate in
ascending order.
And here is the code that I'm executing
SortOptions sortOptions = SortOptions.newBuilder()
.addSortExpression(SortExpression.newBuilder()
.setExpression("birthDate")
.setDirection(SortExpression.SortDirection.ASCENDING)
.setDefaultValueNumeric(0))
.build();
QueryOptions options = QueryOptions.newBuilder()
.setSortOptions(sortOptions)
.build();
Query query = Query.newBuilder().setOptions(options).build();
IndexSpec indexSpec = IndexSpec.newBuilder().setName("DocumentIndex").build();
Index index = SearchServiceFactory.getSearchService().getIndex(indexSpec);
Result<ScoredDocument> result = index.search(query);
As a result I'm receiving the following exception: *Failed to parse sort
expression 'birthDate': Expected single type for **birthDate**; found
[TEXT, NUMERIC]*
In the documentation it was said that when using
*SortExpression*<https://developers.google.com/appengine/docs/java/search/options>you
should provide a default value depending of the document's field type (
*setDefaultValue(String
value) , setDefaultNumericValue(double value)* ).
I though that there was my problem. I tried with both of the default
values. Also tried to remove the default value but then I received the
error that at least one default value should be provided.
I managed to make it work only once and what I did was to create a new
index and store the same document but without setting value for the
*birthDate* field.
I ended up with the following document
IDfirstNamebirthDate
1JohnNone
After executing the same query I end up with zero results (what I was
expecting). After that I stored value to the document's birthDate field and
again received the same error.
So has anyone faced the same problem? Is it a problem with GAE or I'm
missing something else?
Thank you in advance!
--
You received this message because you are subscribed to the Google Groups
"Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-appengine.
For more options, visit https://groups.google.com/groups/opt_out.