ruby-on-railsrubysunspotsunspot-railssunspot-solr

How to search text only in selected records using sunspot


How can i search text in selected records, not in all records of the model?

nodes_res = Node.where(id: ids)
search = nodes_res.search { keywords "query" }

but sunspot searches in all records of the model :(


Solution

  • I have not tested this but it looks you you want https://github.com/sunspot/sunspot#scoping-scalar-fields

    See this example:

    # Posts with a category of 1, 3, or 5
    Post.search do
      with(:category_ids, [1, 3, 5])
    end
    

    So something like this:

    Node.search do
      keywords "query"
      with(:ids, [ids])
    end