When querying the search index in the Python version of the GAE search API , what is the best practice for finding elements where documents with words matching names are returned first and then documents where words match the body?
For example:
body = """This is the body of the document, with a set of words""" my_document = search.Document( fields=[ search.TextField(name='title', value='A Set Of Words'), search.TextField(name='body', value=body), ])
If possible, how can I search the Document index of the above form with the results returned in this priority, where the search for the search phrase is in the qs variable:
- Documents whose
title matches qs ; then - Documents whose body matches
qs .
It seems like the right solution is to use MatchScorer , but I might be aloof from this since I haven't used this search feature before. The documentation does not show how to use MatchScorer , but I assume that one of the subclasses overloads some function, but since it is not documented, and I did not delve into the code, I canβt say for sure.
Is there something here that I'm missing, or is this the right strategy? Did I miss when this is documented?
For clarity, here is a more detailed example of the desired result:
documents = [ dict(title="Alpha", body="A"), # "Alpha" dict(title="Beta", body="B Two"), # "Beta" dict(title="Alpha Two", body="A"), # "Alpha2" ] for doc in documents: search.Document( fields=[ search.TextField(name="title", value=doc.title), search.TextField(name="body", value=doc.body), ] ) index.put(doc) # for some search.Index # Then when we search, we search the Title and Body. index.search("Alpha") # returns [Alpha, Alpha2] # Results where the search is found in the Title are given higher weight. index.search("Two") # returns [Alpha2, Beta] -- note Alpha2 has 'Two' in the title.
python google-app-engine gae-search
Brian M. hunt
source share