如何在太阳黑子中动态构建搜索块?

发布于 2025-01-02 12:29:57 字数 592 浏览 1 评论 0原文

我正在将 Rails 应用程序从使用acts_as_solr 转换为sunspot。

该应用程序使用在acts_as_solr中公开的solr中的字段搜索功能。您可以给它一个像这样的查询字符串:

title:"The thing to search"

它会在标题字段中搜索该字符串。

在转换为太阳黑子时,我正在解析查询字符串的字段特定部分,并且我需要动态生成搜索块。像这样的事情:

Sunspot.search(table_clazz) do
  keywords(first_string, :fields => :title)
  keywords(second_string, :fields => :description)

  ...
  paginate(:page => page, :per_page => per_page)      
end

如果查询需要的话,还需要执行持续时间(秒,整数)范围和求反,这很复杂。

在当前系统上,用户可以搜索标题中的某些内容,排除另一个字段中包含其他内容的记录,并按持续时间确定范围。

简而言之,如何动态生成这些块?

I am converting a Rails app from using acts_as_solr to sunspot.

The app uses the field search capability in solr that was exposed in acts_as_solr. You could give it a query string like this:

title:"The thing to search"

and it would search for that string in the title field.

In converting to sunspot I am parsing out field specific portions of the query string and I need to dynamically generate the search block. Something like this:

Sunspot.search(table_clazz) do
  keywords(first_string, :fields => :title)
  keywords(second_string, :fields => :description)

  ...
  paginate(:page => page, :per_page => per_page)      
end

This is complicated by also needing to do duration (seconds, integer) ranges and negation if the query requires it.

On the current system users can search for something in the title, excluding records with something else in another field and scoping by duration.

In a nutshell, how do I generate these blocks dynamically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里的微风 2025-01-09 12:29:57

我最近做了这样的事情,使用 instance_eval 在太阳黑子搜索块的上下文中评估过程(在其他地方创建)。

优点是这些过程可以在应用程序中的任何位置创建,而且您可以使用与在太阳黑子搜索块中相同的语法编写它们

这是一个帮助您开始处理特定情况的简单示例:

def build_sunspot_query(conditions)
  condition_procs = conditions.map{|c| build_condition c}

  Sunspot.search(table_clazz) do
    condition_procs.each{|c| instance_eval &c}

    paginate(:page => page, :per_page => per_page)
  end
end

def build_condition(condition)
  Proc.new do
    # write this code as if it was inside the sunspot search block

    keywords condition['words'], :fields => condition[:field].to_sym
  end
end

conditions = [{words: "tasty pizza", field: "title"},
              {words: "cheap",       field: "description"}]

build_sunspot_query conditions

顺便说一下,如果需要,您甚至可以在另一个过程中实例化一个过程(在我的例子中,我编写了任意嵌套的“和”/“或”条件) 。

I recently did this kind of thing using instance_eval to evaluate procs (created elsewhere) in the context of the Sunspot search block.

The advantage is that these procs can be created anywhere in your application yet you can write them with the same syntax as if you were inside a sunspot search block.

Here's a quick example to get you started for your particular case:

def build_sunspot_query(conditions)
  condition_procs = conditions.map{|c| build_condition c}

  Sunspot.search(table_clazz) do
    condition_procs.each{|c| instance_eval &c}

    paginate(:page => page, :per_page => per_page)
  end
end

def build_condition(condition)
  Proc.new do
    # write this code as if it was inside the sunspot search block

    keywords condition['words'], :fields => condition[:field].to_sym
  end
end

conditions = [{words: "tasty pizza", field: "title"},
              {words: "cheap",       field: "description"}]

build_sunspot_query conditions

By the way, if you need to, you can even instance_eval a proc inside of another proc (in my case I composed arbitrarily-nested 'and'/'or' conditions).

只等公子 2025-01-09 12:29:57

Sunspot 提供了一个名为 Sunspot.new_search 的方法,它允许您逐步构建搜索条件并按需执行。

提供的示例:

search = Sunspot.new_search do
  with(:blog_id, 1)
end
search.build do
  keywords('some keywords')
end
search.build do
  order_by(:published_at, :desc)
end
search.execute

# This is equivalent to:
Sunspot.search do
  with(:blog_id, 1)
  keywords('some keywords')
  order_by(:published_at, :desc)
end

Sunspot 源代码 灵活性,您应该能够动态构建查询。此外,您可以将常见条件提取到方法中,如下所示:

def blog_facets
  lambda { |s|
    s.facet(:published_year)
    s.facet(:author)
  }
end

search = Sunspot.new_search(Blog)
search.build(&blog_facets)
search.execute

Sunspot provides a method called Sunspot.new_search which lets you build the search conditions incrementally and execute it on demand.

An example provided by the Sunspot's source code:

search = Sunspot.new_search do
  with(:blog_id, 1)
end
search.build do
  keywords('some keywords')
end
search.build do
  order_by(:published_at, :desc)
end
search.execute

# This is equivalent to:
Sunspot.search do
  with(:blog_id, 1)
  keywords('some keywords')
  order_by(:published_at, :desc)
end

With this flexibility, you should be able to build your query dynamically. Also, you can extract common conditions to a method, like so:

def blog_facets
  lambda { |s|
    s.facet(:published_year)
    s.facet(:author)
  }
end

search = Sunspot.new_search(Blog)
search.build(&blog_facets)
search.execute
看春风乍起 2025-01-09 12:29:57

我自己已经解决了这个问题。我使用的解决方案是将所需的范围编译为字符串,连接它们,然后在搜索块内评估它们。

这需要一个单独的查询构建器库来询问 solr 索引,以确保不会为不存在的索引字段创建范围。

该代码非常适合我的项目,而且太长,无法完整发布,但这就是我所做的:

1。拆分搜索词

这为我提供了一组词或词加字段:

['field:term', 'non field terms']

2.这将传递给查询生成器。

生成器根据可用索引将数组转换为范围。此方法是一个示例,它采用模型类、字段和值,并在字段已建立索引时返回范围。

def convert_text_query_to_search_scope(model_clazz, field, value)
  if field_is_indexed?(model_clazz, field)
    escaped_value = value.gsub(/'/, "\\\\'")
    "keywords('#{escaped_value}', :fields => [:#{field}])"
  else
    ""
  end
end

3.连接所有范围

生成的范围被连接join("\n") 并进行evaled。

这种方法允许用户选择他们想要搜索的模型,并且可以选择进行特定领域的搜索。然后,系统将仅搜索具有任何指定字段(或公共字段)的模型,而忽略其余字段。

检查字段是否已索引的方法是:

# based on http://blog.locomotivellc.com/post/6321969631/sunspot-introspection
def field_is_indexed?(model_clazz, field)
  # first part returns an array of all indexed fields - text and other types - plus ':class'
  Sunspot::Setup.for(model_clazz).all_field_factories.map(&:name).include?(field.to_sym)
end

如果有人需要它,请检查可排序性:

def field_is_sortable?(classes_to_check, field)
  if field.present?
    classes_to_check.each do |table_clazz|
      return false if ! Sunspot::Setup.for(table_clazz).field_factories.map(&:name).include?(field.to_sym)
    end
    return true
  end
  false
end

I have solved this myself. The solution I used was to compiled the required scopes as strings, concatenate them, and then eval them inside the search block.

This required a separate query builder library that interrogates the solr indexes to ensure that a scope is not created for a non existent index field.

The code is very specific to my project, and too long to post in full, but this is what I do:

1. Split the search terms

this gives me an array of the terms or terms plus fields:

['field:term', 'non field terms']

2. This is passed to the query builder.

The builder converts the array to scopes, based on what indexes are available. This method is an example that takes the model class, field and value and returns the scope if the field is indexed.

def convert_text_query_to_search_scope(model_clazz, field, value)
  if field_is_indexed?(model_clazz, field)
    escaped_value = value.gsub(/'/, "\\\\'")
    "keywords('#{escaped_value}', :fields => [:#{field}])"
  else
    ""
  end
end

3. Join all the scopes

The generated scopes are joined join("\n") and that is evaled.

This approach allows the user to selected the models they want to search, and optionally to do field specific searching. The system will then only search the models with any specified fields (or common fields), ignoring the rest.

The method to check if the field is indexed is:

# based on http://blog.locomotivellc.com/post/6321969631/sunspot-introspection
def field_is_indexed?(model_clazz, field)
  # first part returns an array of all indexed fields - text and other types - plus ':class'
  Sunspot::Setup.for(model_clazz).all_field_factories.map(&:name).include?(field.to_sym)
end

And if anyone needs it, a check for sortability:

def field_is_sortable?(classes_to_check, field)
  if field.present?
    classes_to_check.each do |table_clazz|
      return false if ! Sunspot::Setup.for(table_clazz).field_factories.map(&:name).include?(field.to_sym)
    end
    return true
  end
  false
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文