首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >thinking-sphinx分页查询0错误:偏移超出界限,max_matches

thinking-sphinx分页查询0错误:偏移超出界限,max_matches
EN

Stack Overflow用户
提问于 2014-05-15 07:12:44
回答 1查看 756关注 0票数 0

我有TS 3.0.6,will_paginate 3.0.5和斯芬克斯2.0.4

我使用页面选项对搜索结果进行分页:

代码语言:javascript
复制
@products = Product.search(query_string, {
  order: ordering,
  ranker: :bm25,
  sql: { include: :images },
  with: {
    price_type_for_search => min_price_conditions..max_price_conditions,
    kind_cd: @params[:kind_cd],
    brand_id: [*@params[:brand_ids]],
    category_ids: categories_for_search,
    property_value_names: [*params[:property_value_names]].map{ |s| Zlib.crc32(s) },
  },
  page: @params[:page],
  per_page: @params[:per]
})

我的错误就像

代码语言:javascript
复制
query 0 error: offset out of bounds (offset=12, max_matches=12); query 1 error: offset out of bounds (offset=12, max_matches=12); query 2 error: offset out of bounds (offset=12, max_matches=12); query 3 error: offset out of bounds (offset=12, max_matches=12)

其中12是@params:params。

否则,我会在我的观点中使用will_paginate @products,但我认为这并不重要。为什么我会出错?我怎么才能避免呢?

我将这些参数发送到搜索:

代码语言:javascript
复制
order: :created_at
ranker: :bm25,
sql: { include: :images },
with: {
  # no matter
},
page: 2,
per_page: 12

如果我发送per_page == 18和页面== 2,那么如果我发送per_page == 12和页面== 3,我得到的是query 0 error: offset out of bounds (offset=18, max_matches=18) et.c.,在我的错误中得到(offset=36, max_matches=36)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-22 10:36:05

好吧,我想我已经搞清楚了。这里的问题是,在上面的示例中,facets方法是在@products上调用的。Facet调用不喜欢:page:per_page选项,因为facet调用只有一个页面。因此,查看页面两个或多个搜索结果会引发错误。

解决方法(我在本地测试过)是将大多数这些搜索选项放在一个共享方法中:

代码语言:javascript
复制
def common_search_options
  {
    order: ordering,
    ranker: :bm25,
    with: {
      price_type_for_search => min_price_conditions..max_price_conditions,
      kind_cd: @params[:kind_cd],
      brand_id: [*@params[:brand_ids]],
      category_ids: categories_for_search,
      property_value_ids: [*params[:property_value_ids]],
    }
  }
end

然后为搜索结果和facet结果创建单独的对象:

代码语言:javascript
复制
@products = Product.search(query_string, common_search_options.merge({
  sql: { include: :images },
  page: @params[:page],
  per_page: @params[:per]
}))

@product_facets = Product.facets(query_string, common_search_options)

当然,您需要将@products.facets的任何引用更改为@product_facets。这将确保方面搜索没有分页选项,现在一切都应该顺利进行。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23671541

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档