pravs
pravs

Reputation: 27

Sunspot search: Searching with negative keyword

Hello I am using sunspot for searching: when I say:

keywords '"India" -"mountain"' do
minimum_match 1 end

It finds all the posts related to "India" keyword and which does not include "mountain" keyword. But when I want all the posts globally which should not include "mountain" keyword how should i do that? When tried with

keywords '-"mountain"' do
minimum_match 1 end

It returned 0 results.

Is there any way that we will pass only negative filter value to sunspot and retrieve all the records except the passed keyword?

Upvotes: 1

Views: 719

Answers (1)

pravs
pravs

Reputation: 27

This seems to be a feature/limitation of solr/sunspot but I have a workaround for this problem.

  1. Index a field with string data type along with text as well
  2. Indexing with text will help for keyword searching (as usual)
  3. Add AND condition in the sunspot solr block which will have without method to escape the unwanted string in the search.

This will search with conditions (keyword specified AND without the escape string)

Upvotes: 0

Related Questions