Baskaran
Baskaran

Reputation: 39

GAE search query using date?

how to write GAE search query using date?

This is my data store.

Class Property(db.Model):
    createdDate=db.DateTimeProperty(auto_now_add=True)

i want to display the records in last sevendays. am tring this,

sevendays = datetime.now() - timedelta(hours=168)

getting correct result for date(7days), and i passed search query in my URL like this,

<a href="/search?search=PropertyCaseType+%3D+Enquiry+AND+PropertyStatus+%3D+Open+AND+createdDate+%3E+sevendays" class="list-group-item">
                                <i class="fa fa-question-circle fa-fw"></i> New Enquires
                                <span class="pull-right text-muted small"><em>
                                    {% if countEnquiryOpen %}
                                        {{ countEnquiryOpen }}
                                    {% else %}
                                        0
                                    {% endif %}

                                </em>
                                </span>
                            </a>

above href= encode query is : PropertyCaseType = Enquiry AND PropertyStatus = Open AND createdDate > sevendays

createdDate > sevendays is not working , otherwise query working fine and getting correct results.

how to do this , friends help me

Upvotes: 1

Views: 70

Answers (3)

Baskaran
Baskaran

Reputation: 39

I got the solution.. The problem is Time. So i remove time filter only date. like this,

sevendays = datetime.now() - timedelta(hours=168)
sevendays = sevendays.date()

Now i get correct result. :)

Upvotes: 1

nik
nik

Reputation: 746

It is a bit hard to work out what is wrong with your app, as you don't show how are using the query string to construct a query.

However, a likely source of the problem could be that you are trying to compare a date string, rather than a datetime object to the createdDate property.

E.g.

def get(self):

    self.response.headers['Content-Type'] = 'text/plain'             
    all_props = Property.all().run()
    self.response.out.write("ALL Properties\n")
    for d in all_props:
        self.response.out.write('Prop: %s\n' % d.createdDate)

    sevendays = datetime.now() - timedelta(hours=168)
    res = Property.gql('where createdDate > :1', sevendays)        

    self.response.out.write("MATCHING Properties compared by datetime\n")
    for d in res:
        self.response.out.write('date: %s\n' % d.createdDate)

    self.response.out.write("MATCHING Properties compared by string\n")
    sds = str(sevendays)
    res_bad = Property.gql('where createdDate > :1', sds)
    for d in res_bad:
        self.response.out.write('date: %s\n' % d.createdDate)

will print out the correct properties under MATCHING Properties compared by datetime, but not under MATCHING Properties compared by string. Unfortunately it doesn't raise any errors either, to tell you that you are using the wrong kind of object in the query.

Therefore, if you are using the query string straight as it is passed in the url, you may need to parse it into a datetime object first using e.g. datetime.strptime().

Upvotes: 0

Niklas Rosencrantz
Niklas Rosencrantz

Reputation: 26652

You should build an index using only the elements from the last 7 days, then query that index and rebuild index accordingly. You can read about indexing in google app engine here.

Upvotes: 0

Related Questions