I can't seem to get a custom snippet length:
snippet = 'snippet("%s", content, 50)' % search_query
index = search.Index(name='index', namespace='namespace')
start_time = time.time()
results = index.search(
query=Query(
query_string=search_query,
options=QueryOptions(
limit=10,
cursor=Cursor(),
sort_options=SortOptions(
match_scorer=search.RescoringMatchScorer()),
returned_expressions=FieldExpression('content_snippet', snippet))))
I want a snippet which is 50 characters long, instead of the default 160 characters. According to this documentation, the snippet function can get 3 arguments: the search term, the field to snippet over and an optional snippet length.
It seems as if it's completely ignoring the third parameter I pass... Am I doing something wrong?
It seems that snippets don't work on the devserver. See the documentation: https://developers.google.com/appengine/docs/python/search/devserver
Using the Python Development Server
The Python development server runs on your local machine and emulates most of the Search API's capabilities. However, a few features are not currently available on the server. For the moment, you should not attempt to use the following features when you run on the development server:
Functions in expressions
These functions are not available:
- snippet()
- geopoint()
- distance()
- pow()
As advoretsky said, returned_expressions
needs to be an iterable. From the docs:
returned_expressions
An iterable of FieldExpression to evaluate and return in search results.
Further, be sure you import the right Query
and QueryOptions
, both are also found in google.appengine.ext.ndb
. I prefer to import them so they have a prefix, things are better readable then ;-)
from google.appengine.ext import ndb
from google.appengine.api import search
# Notice the nice distinction
ndb.Query
search.Query