【问题标题】:Solr search not giving results for intermediate keywordsSolr 搜索没有给出中间关键字的结果
【发布时间】:2014-11-11 06:04:10
【问题描述】:

我也有类似的问题

What part of this Solr-Sunspot setup am I missing?

但是,问题是,我已经应用了选定的解决方案,并且我的可搜索字段为text,但它仍然不适用于中间字符串。它只适用于完整的字符串。

例如:- User.search (1, "adam") 会给我 Adam 的结果,但这不起作用:

User.search (1, "ad")

我的代码

用户模型

  searchable do
    text :firstname
    text :lastname
    text :email
    integer :some_id
  end

搜索方式

  def self.search(obj, search_text)
    solr_search do
      keywords search_text
      with(:some_id, obj.id)
    end
  end

除此之外,我还尝试了以下代码:

  def self.search(obj, search_text)
    solr_search do
      keywords ''
      any_of do
        with(:email, search_text)
        with(:firstname, search_text)
        with(:lastname, search_text)
      end
      with(:some_id, obj.id)
    end
  end

但是,我在 Rails 控制台中收到以下错误:

> User.search(obj, "ad")

Sunspot::UnrecognizedFieldError:没有为名为“email”的用户配置字段

【问题讨论】:

    标签: ruby-on-rails solr


    【解决方案1】:

    配置为text 的字段将是full-text 可搜索的。其他(Scalar)字段integer, boolean, time and string可用于scope queries

    在您的可搜索块中,您只配置了文本字段。

      searchable do
        text :firstname
        text :lastname
        text :email
        integer :some_id
      end
    

    以及您的搜索方法对未在可搜索块中定义的标量字段执行搜索。

      any_of do
        with(:email, search_text)
        with(:firstname, search_text)
        with(:lastname, search_text)
      end
    

    为了完成这项工作,您必须将电子邮件、名字和姓氏字段定义为可搜索块中的字符串。

      searchable do
        text   :firstname
        text   :lastname
        text   :email
        string :firstname
        string :lastname
        string :email
        integer :some_id
      end
    

    Sunspot Search

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 2021-02-14
      • 1970-01-01
      • 1970-01-01
      • 2011-12-05
      • 2010-09-16
      相关资源
      最近更新 更多