GAE搜索API:自定义代码段长度

2024-03-29 11:57:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我似乎无法获取自定义代码段长度:

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))))

我想要一个50个字符长的代码段,而不是默认的160个字符。根据这篇文档,snippet函数可以获得3个参数:搜索项、要对其进行分段的字段和可选的代码段长度。在

似乎它完全忽略了我传递的第三个参数。。。我做错什么了吗?在


Tags: namesearch参数indextime代码段contentquery
2条回答

似乎代码片段在devserver上不起作用。请参阅文档: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()

正如advoretsky所说,returned_expressions必须是iterable。从文件中:

returned_expressions

An iterable of FieldExpression to evaluate and return in search results.

此外,请确保导入正确的Query和{},这两个都可以在google.appengine.ext.ndb中找到。我更喜欢导入它们,这样它们就有了前缀,这样更易于阅读;-)

from google.appengine.ext import ndb
from google.appengine.api import search

# Notice the nice distinction
ndb.Query
search.Query

我还没有亲自尝试过,但是根据引用返回了\u表达式should be iterable。另请参见Query and Sorting Options上的示例并将其更改为

returned_expressions=[FieldExpression('content_snippet', snippet)]

相关问题 更多 >