Question

I'm having trouble getting a specific document field out of a ScoredDocument. This must be very simple, but the documentation doesn't seem to cover it.

I've properly created an index, Documents in the index and searched the documents with results. The Documents just have a title property and a note property. How do I get the title or the note out? This is the server code:

class SearchHandler(webapp2.RequestHandler):
def get(self):
    index = search.Index(name="myIndex")
    query_string = "dog" 
    try:
        results = index.search(query_string) 
        logging.info(results)
        self.response.out.write("""<html><body>
        Here are the results from the search for "dog":""")
        # Iterate over the documents in the results
        for note in results:
            self.response.out.write(note.fields)
            self.response.out.write(note.fields.title) # HERE IS PROBLEM
        self.response.out.write("</body></html>")
    except search.Error:
        logging.exception('Search failed')

The output without trying to get the title is correct and I get a ScoredDocument field:

[search.TextField(name=u'title', value=u'A note with a dog'),     search.TextField(name=u'note', value=u'hamster'), search.DateField(name=u'updated', value=datetime.datetime(2014, 4, 10, 0, 0)), search.DateField(name=u'created', value=datetime.datetime(2014, 4, 10, 0, 0))] 

The error for trying to get the title this way is this: in get self.response.out.write(note.fields.title) AttributeError: 'list' object has no attribute 'title'

Can anyone help with this? Thanks

Was it helpful?

Solution

i believe it should be

note.field('title').value
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top