Search code examples
solrelasticsearchsunspot

How to search for documents matching X out of Y conditions in Solr, with Sunspot


I want to be able to run a query equivalent to this ElasticSearch query, using Solr + Sunspot. The example is using Tire, but it's pretty close to the raw query in json:

query do
  boolean do
    must do
      boolean minimum_number_should_match: 1 do
        should { term :mod_id, 1 }
        should { term :mod_id, 2 }
        should { term :mod_id, 3 }
      end
    end
  end
end

In my apps, users can tag themselves. Projects are also tagged with the same tags. I want to recommend projects to users, projects that have at least 2 tags in common with a given user for instance.

How would I do that with Sunspot? I only see minimum_match for full text search.


Solution

  • In solr, you can do that with edismax query parser by specifying the mm (Minimum should Match) parameter. It works on a multi-valued field mod_id like this:

    http://HOST:PORT/solr/CORE/select?defType=edismax&mm=2&q=mod_id:(1 2 3)
    

    That would translate to this query with the lucene query parser:

    /select?q=(mod_id:(1 AND 2) OR mod_id:(1 AND 3) OR mod_id:(2 AND 3))
    

    Hope you can translate that to sunspot.